Skip to content

Commit

Permalink
Reset lastReceivedTime before connecting websocket to resolve race co…
Browse files Browse the repository at this point in the history
…ndition
  • Loading branch information
eynzhang committed Sep 11, 2020
1 parent dba6b5f commit 73530f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 9 additions & 3 deletions pkg/client/websocketclientbase/websocketv1clientbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ func (p *WebSocketV1ClientBase) SetHandler(authHandler AuthenticationV1ResponseH
// Connect to websocket server
// if autoConnect is true, then the connection can be re-connect if no data received after the pre-defined timeout
func (p *WebSocketV1ClientBase) Connect(autoConnect bool) error {

// reset last received time as now
p.lastReceivedTime = time.Now()

// connect to websocket
err := p.connectWebSocket()
if err != nil {
return err
}

// start loop to read and handle message
p.startReadLoop()

// start ticker to manage connection
if autoConnect {
p.startTicker()
}
Expand Down Expand Up @@ -108,8 +117,6 @@ func (p *WebSocketV1ClientBase) connectWebSocket() error {
return err
}

p.startReadLoop()

return nil
}

Expand All @@ -134,7 +141,6 @@ func (p *WebSocketV1ClientBase) disconnectWebSocket() {
// initialize a ticker and start a goroutine tickerLoop()
func (p *WebSocketV1ClientBase) startTicker() {
p.ticker = time.NewTicker(TimerIntervalSecond * time.Second)
p.lastReceivedTime = time.Now()

go p.tickerLoop()
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/client/websocketclientbase/websocketv2clientbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ func (p *WebSocketV2ClientBase) SetHandler(authHandler AuthenticationV2ResponseH
// Connect to websocket server
// if autoConnect is true, then the connection can be re-connect if no data received after the pre-defined timeout
func (p *WebSocketV2ClientBase) Connect(autoConnect bool) {
// reset last received time as now
p.lastReceivedTime = time.Now()

// connect to websocket
p.connectWebSocket()

// start loop to read and handle message
p.startReadLoop()

// start ticker to manage connection
if autoConnect {
p.startTicker()
}
Expand Down Expand Up @@ -106,8 +114,6 @@ func (p *WebSocketV2ClientBase) connectWebSocket() {
}

p.Send(auth)

p.startReadLoop()
}

// disconnect with server
Expand All @@ -132,7 +138,6 @@ func (p *WebSocketV2ClientBase) disconnectWebSocket() {
// initialize a ticker and start a goroutine tickerLoop()
func (p *WebSocketV2ClientBase) startTicker() {
p.ticker = time.NewTicker(TimerIntervalSecond * time.Second)
p.lastReceivedTime = time.Now()

go p.tickerLoop()
}
Expand Down

0 comments on commit 73530f6

Please sign in to comment.