Skip to content

Commit

Permalink
fix: add locking around keepalive timer (#526)
Browse files Browse the repository at this point in the history
Fixes #525
  • Loading branch information
agaffney authored Mar 11, 2024
1 parent 8dc0b05 commit dee4863
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions protocol/keepalive/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (

type Client struct {
*protocol.Protocol
config *Config
timer *time.Timer
onceStart sync.Once
config *Config
timer *time.Timer
timerMutex sync.Mutex
onceStart sync.Once
}

func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
Expand Down Expand Up @@ -61,9 +62,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
go func() {
<-c.Protocol.DoneChan()
// Stop any existing timer
c.timerMutex.Lock()
if c.timer != nil {
c.timer.Stop()
}
c.timerMutex.Unlock()
}()
return c
}
Expand All @@ -85,6 +88,8 @@ func (c *Client) sendKeepAlive() {
}

func (c *Client) startTimer() {
c.timerMutex.Lock()
defer c.timerMutex.Unlock()
// Stop any existing timer
if c.timer != nil {
c.timer.Stop()
Expand Down

0 comments on commit dee4863

Please sign in to comment.