Skip to content

Commit

Permalink
Fix heartbeat process
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyitc committed Jun 9, 2021
1 parent 12857a8 commit e41ac50
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions TCPProtocol/mnhv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,33 @@ func (s *mnhv1) keepalive(duration time.Duration, timeout time.Duration) {
defer s.worker.Done()

for {
buf := []byte("heartbeat\n")
_, err := s.conn.Write(buf)
if err != nil {
log.Error("send heartbeat fail:", err.Error())
return
}

t := time.Now()

s.conn.SetReadDeadline(t.Add(duration))
buf = make([]byte, 255)
_, err = s.conn.Read(buf)
if err == nil {
s.lastSeen = time.Now()
} else {
log.Debug("read heartbeat fail:", err.Error())
}

if time.Since(s.lastSeen) > timeout {
log.Debug("heartbeat timeout:", time.Since(s.lastSeen).String())
return
}

select {
case <-s.closingChan:
return
case <-time.After(duration):
buf := []byte("heartbeat\n")
_, err := s.conn.Write(buf)
if err != nil {
log.Error("send heartbeat fail:", err.Error())
return
}

s.conn.SetReadDeadline(time.Now().Add(duration))
buf = make([]byte, 255)
_, err = s.conn.Read(buf)
if err == nil {
s.lastSeen = time.Now()
} else {
log.Debug("read heartbeat fail:", err.Error())
}

if time.Since(s.lastSeen) > timeout {
log.Debug("heartbeat timeout:", time.Since(s.lastSeen).String())
return
}
case <-time.After(duration - time.Since(t)):
}
}
}
Expand Down

0 comments on commit e41ac50

Please sign in to comment.