fix: Fix the device heartbeat detection mechanism

Abnormal logs appear in some environments.
```
Inactive device in long time
```

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-06-05 14:47:30 +08:00
parent d76b400fe7
commit b6cbf55760
+5 -3
View File
@@ -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 {