Skip to content

Commit 8b7dcb8

Browse files
authored
Merge pull request #470 from blinklabs-io/fix/keepalive-double-schedule
fix: don't send double keep-alives
2 parents 1a2ae72 + d7d70b6 commit 8b7dcb8

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

protocol/keepalive/client.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
6363
if c.timer != nil {
6464
// Stop timer and drain channel
6565
if ok := c.timer.Stop(); !ok {
66-
<-c.timer.C
66+
// Read item from channel, if available
67+
select {
68+
case <-c.timer.C:
69+
default:
70+
}
6771
}
6872
}
6973
}()
@@ -82,11 +86,22 @@ func (c *Client) sendKeepAlive() {
8286
if err := c.SendMessage(msg); err != nil {
8387
c.SendError(err)
8488
}
85-
// Reschedule timer
89+
// Schedule timer
8690
c.startTimer()
8791
}
8892

8993
func (c *Client) startTimer() {
94+
// Stop any existing timer
95+
if c.timer != nil {
96+
if ok := c.timer.Stop(); !ok {
97+
// Read item from channel, if available
98+
select {
99+
case <-c.timer.C:
100+
default:
101+
}
102+
}
103+
}
104+
// Create new timer
90105
c.timer = time.AfterFunc(c.config.Period, c.sendKeepAlive)
91106
}
92107

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

108123
func (c *Client) handleKeepAliveResponse(msgGeneric protocol.Message) error {
109124
msg := msgGeneric.(*MsgKeepAliveResponse)
110-
// Start the timer again if we had one previously
111-
if c.timer != nil {
112-
defer c.startTimer()
113-
}
114125
if c.config != nil && c.config.KeepAliveResponseFunc != nil {
115126
// Call the user callback function
116127
return c.config.KeepAliveResponseFunc(msg.Cookie)

0 commit comments

Comments
 (0)