diff --git a/services/excservice/binancesocketmanager.go b/services/excservice/binancesocketmanager.go index 45b428a..ae94624 100644 --- a/services/excservice/binancesocketmanager.go +++ b/services/excservice/binancesocketmanager.go @@ -79,13 +79,17 @@ func (wm *BinanceWebSocketManager) Start() { // 重启连接 func (wm *BinanceWebSocketManager) Restart(apiKey, apiSecret, proxyType, proxyAddress string) *BinanceWebSocketManager { + wm.mu.Lock() + defer wm.mu.Unlock() + wm.apiKey = apiKey wm.apiSecret = apiSecret wm.proxyType = proxyType wm.proxyAddress = proxyAddress if wm.isStopped { - wm.run() + wm.isStopped = false + utility.SafeGo(wm.run) } else { wm.reconnect <- struct{}{} } @@ -385,7 +389,12 @@ func (wm *BinanceWebSocketManager) Stop() { } wm.isStopped = true - close(wm.stopChannel) + // 关闭 stopChannel(确保已经关闭,避免 panic) + select { + case <-wm.stopChannel: + default: + close(wm.stopChannel) + } if wm.cancelFunc != nil { wm.cancelFunc() @@ -398,6 +407,9 @@ func (wm *BinanceWebSocketManager) Stop() { log.Info(fmt.Sprintf("key【%s】close", wm.apiKey)) } } + + // **重新创建 stopChannel,避免 Restart() 时无效** + wm.stopChannel = make(chan struct{}) } // 重连机制