Skip to content

Commit

Permalink
Don't keep reconnecting if the websocket client has been stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbeaumont authored and lorenzodonini committed May 24, 2021
1 parent ee760ff commit 481c89c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ws/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ type Client struct {
onReconnected func()
mutex sync.Mutex
errC chan error
stopped chan struct{}
}

// Creates a new simple websocket client (the channel is not secured).
Expand Down Expand Up @@ -772,7 +773,11 @@ func (client *Client) handleReconnection() {
delay := client.timeoutConfig.ReconnectBackoff
for {
// Wait before reconnecting
time.Sleep(delay)
select {
case <-time.After(delay):
case <-client.stopped:
return
}
err := client.Start(client.webSocket.id)
if err == nil {
// Re-connection was successful
Expand Down Expand Up @@ -842,6 +847,7 @@ func (client *Client) Start(url string) error {
closeSignal: make(chan error, 1),
tlsConnectionState: resp.TLS,
}
client.stopped = make(chan struct{})
client.setConnected(true)
//Start reader and write routine
go client.writePump()
Expand All @@ -853,6 +859,7 @@ func (client *Client) Stop() {
client.setConnected(false)
close(client.webSocket.outQueue)

close(client.stopped)
if client.errC != nil {
close(client.errC)
client.errC = nil
Expand Down

0 comments on commit 481c89c

Please sign in to comment.