Skip to content

Commit

Permalink
Improve heartbeat check
Browse files Browse the repository at this point in the history
The connection is considered disconnected if three timeouts occur

Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jan 5, 2021
1 parent 9783273 commit ca8087b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (dev *Device) logout(sid byte) {
func (dev *Device) keepAlive() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
ninactive := 0

lastHeartbeat := time.Now()

Expand All @@ -98,9 +99,13 @@ func (dev *Device) keepAlive() {
case <-ticker.C:
now := time.Now()
if now.Sub(dev.active) > HeartbeatInterval*3/2 {
log.Error().Msgf("Inactive device in long time, now kill it: %s", dev.id)
dev.close()
return
log.Error().Msgf("Inactive device in long time: %s", dev.id)
if ninactive > 1 {
log.Error().Msgf("Inactive 3 times, now kill it: %s", dev.id)
dev.close()
return
}
ninactive = ninactive + 1
}

if now.Sub(lastHeartbeat) > HeartbeatInterval-1 {
Expand Down

0 comments on commit ca8087b

Please sign in to comment.