diff --git a/recws.go b/recws.go index 4faa7af..51fcd43 100644 --- a/recws.go +++ b/recws.go @@ -47,13 +47,14 @@ type RecConn struct { // NonVerbose suppress connecting/reconnecting messages. NonVerbose bool - isConnected bool - mu sync.RWMutex - url string - reqHeader http.Header - httpResp *http.Response - dialErr error - dialer *websocket.Dialer + isConnected bool + isConnectRunning bool + mu sync.RWMutex + url string + reqHeader http.Header + httpResp *http.Response + dialErr error + dialer *websocket.Dialer *websocket.Conn } @@ -418,6 +419,15 @@ func (rc *RecConn) keepAlive() { } func (rc *RecConn) connect() { + rc.mu.Lock() + canStart := !rc.isConnectRunning + rc.isConnectRunning = true + rc.mu.Unlock() + + if !canStart { + return + } + b := rc.getBackoff() rand.Seed(time.Now().UTC().UnixNano()) @@ -450,6 +460,10 @@ func (rc *RecConn) connect() { rc.keepAlive() } + rc.mu.Lock() + rc.isConnectRunning = false + rc.mu.Unlock() + return }