binance: check connCancel only when new context is allocated

This commit is contained in:
c9s 2021-05-30 15:53:01 +08:00
parent f9d4068145
commit d112dbb1a4

View File

@ -300,11 +300,6 @@ func (s *Stream) reconnector(ctx context.Context) {
return return
case <-s.ReconnectC: case <-s.ReconnectC:
// ensure the previous context is cancelled
if s.connCancel != nil {
s.connCancel()
}
log.Warnf("received reconnect signal, reconnecting...") log.Warnf("received reconnect signal, reconnecting...")
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
@ -342,6 +337,11 @@ func (s *Stream) connect(ctx context.Context) error {
// should only start one connection one time, so we lock the mutex // should only start one connection one time, so we lock the mutex
s.ConnLock.Lock() s.ConnLock.Lock()
// ensure the previous context is cancelled
if s.connCancel != nil {
s.connCancel()
}
// create a new context // create a new context
s.connCtx, s.connCancel = context.WithCancel(ctx) s.connCtx, s.connCancel = context.WithCancel(ctx)
conn.SetReadDeadline(time.Now().Add(readTimeout)) conn.SetReadDeadline(time.Now().Add(readTimeout))