From d112dbb1a4af438b98274593c5dc4f49ca76f759 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 30 May 2021 15:53:01 +0800 Subject: [PATCH] binance: check connCancel only when new context is allocated --- pkg/exchange/binance/stream.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/exchange/binance/stream.go b/pkg/exchange/binance/stream.go index a6f2bb486..df1c12d0e 100644 --- a/pkg/exchange/binance/stream.go +++ b/pkg/exchange/binance/stream.go @@ -300,11 +300,6 @@ func (s *Stream) reconnector(ctx context.Context) { return case <-s.ReconnectC: - // ensure the previous context is cancelled - if s.connCancel != nil { - s.connCancel() - } - log.Warnf("received reconnect signal, reconnecting...") 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 s.ConnLock.Lock() + // ensure the previous context is cancelled + if s.connCancel != nil { + s.connCancel() + } + // create a new context s.connCtx, s.connCancel = context.WithCancel(ctx) conn.SetReadDeadline(time.Now().Add(readTimeout))