This commit is contained in:
2025-02-20 20:19:49 +08:00
parent f105b7fd61
commit 28b5406028

View File

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