Skip to content

Commit

Permalink
Fix heart beat problem in server/client/conn.go
Browse files Browse the repository at this point in the history
Whenever the reading loop was reading data it had the side effect of reseting the *writeTimeout* timer used to send heart beat to the client (this was due to variable scope).
If the timer was rested too much in a row the heart beat will not occurred and it will end up in the client disconnecting.

Fix #66
  • Loading branch information
anthonyraymond authored Jan 9, 2021
1 parent 9c81a64 commit dd67e27
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ func (c *Conn) processLoop() {

c.writer = frame.NewWriter(c.rw)
c.stateFunc = connecting
for {
var timerChannel <-chan time.Time
var timer *time.Timer

if c.writeTimeout > 0 {
var timerChannel <-chan time.Time
var timer *time.Timer
for {
if c.writeTimeout > 0 && timer == nil {
timer = time.NewTimer(c.writeTimeout)
timerChannel = timer.C
}
Expand Down Expand Up @@ -310,6 +310,11 @@ func (c *Conn) processLoop() {
}

case _ = <-timerChannel:
// stop the heart-beat timer
if timer != nil {
timer.Stop()
timer = nil
}
// write a heart-beat
err := c.writer.Write(nil)
if err != nil {
Expand Down

0 comments on commit dd67e27

Please sign in to comment.