From b6cbf55760930fc40bf1b72a2eea50308abcc18e Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 5 Jun 2025 14:47:30 +0800 Subject: [PATCH] fix: Fix the device heartbeat detection mechanism Abnormal logs appear in some environments. ``` Inactive device in long time ``` Signed-off-by: Jianhui Zhao --- device.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/device.go b/device.go index 40e1c9a..4546502 100644 --- a/device.go +++ b/device.go @@ -55,6 +55,7 @@ type device struct { token string conn net.Conn active time.Time + ninactive int registered bool closed uint32 send chan []byte // Buffered channel of outbound messages. @@ -235,6 +236,7 @@ func (dev *device) readLoop() { } dev.active = time.Now() + dev.ninactive = 0 switch typ { case msgTypeRegister: @@ -324,7 +326,6 @@ func (dev *device) writeLoop() { dev.br.unregister <- dev }() - ninactive := 0 lastHeartbeat := time.Now() for { @@ -348,11 +349,12 @@ func (dev *device) writeLoop() { } log.Error().Msgf("Inactive device in long time: %s", dev.id) - if ninactive > 1 { + if dev.ninactive > 1 { log.Error().Msgf("Inactive 3 times, now kill it: %s", dev.id) return } - ninactive = ninactive + 1 + dev.ninactive = dev.ninactive + 1 + dev.active = time.Now() } if now.Sub(lastHeartbeat) > heartbeatInterval-1 {