From dd67e2759ac4285640180e45f0be06b262797209 Mon Sep 17 00:00:00 2001 From: Anthony Raymond Date: Sat, 9 Jan 2021 04:20:42 +0100 Subject: [PATCH] Fix heart beat problem in server/client/conn.go 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 --- server/client/conn.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/server/client/conn.go b/server/client/conn.go index f5db7c1..a80c380 100644 --- a/server/client/conn.go +++ b/server/client/conn.go @@ -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 } @@ -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 {