Skip to content

Commit

Permalink
Merge pull request #470 from blinklabs-io/fix/keepalive-double-schedule
Browse files Browse the repository at this point in the history
fix: don't send double keep-alives
  • Loading branch information
agaffney authored Dec 31, 2023
2 parents 1a2ae72 + d7d70b6 commit 8b7dcb8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions protocol/keepalive/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
if c.timer != nil {
// Stop timer and drain channel
if ok := c.timer.Stop(); !ok {
<-c.timer.C
// Read item from channel, if available
select {
case <-c.timer.C:
default:
}
}
}
}()
Expand All @@ -82,11 +86,22 @@ func (c *Client) sendKeepAlive() {
if err := c.SendMessage(msg); err != nil {
c.SendError(err)
}
// Reschedule timer
// Schedule timer
c.startTimer()
}

func (c *Client) startTimer() {
// Stop any existing timer
if c.timer != nil {
if ok := c.timer.Stop(); !ok {
// Read item from channel, if available
select {
case <-c.timer.C:
default:
}
}
}
// Create new timer
c.timer = time.AfterFunc(c.config.Period, c.sendKeepAlive)
}

Expand All @@ -107,10 +122,6 @@ func (c *Client) messageHandler(msg protocol.Message) error {

func (c *Client) handleKeepAliveResponse(msgGeneric protocol.Message) error {
msg := msgGeneric.(*MsgKeepAliveResponse)
// Start the timer again if we had one previously
if c.timer != nil {
defer c.startTimer()
}
if c.config != nil && c.config.KeepAliveResponseFunc != nil {
// Call the user callback function
return c.config.KeepAliveResponseFunc(msg.Cookie)
Expand Down

0 comments on commit 8b7dcb8

Please sign in to comment.