fix: force disconnect if keepAlive loop hasnt been executed recently

This commit is contained in:
Richard Ramos 2022-12-05 15:06:33 -04:00 committed by RichΛrd
parent b08ae4d7cc
commit 05e33105c4
1 changed files with 20 additions and 1 deletions

View File

@ -23,9 +23,26 @@ func (w *WakuNode) startKeepAlive(t time.Duration) {
w.log.Info("setting up ping protocol", zap.Duration("duration", t)) w.log.Info("setting up ping protocol", zap.Duration("duration", t))
ticker := time.NewTicker(t) ticker := time.NewTicker(t)
defer ticker.Stop() defer ticker.Stop()
lastTimeExecuted := <-ticker.C
sleepDetectionInterval := int64(t) * 3
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
difference := time.Now().UnixNano() - lastTimeExecuted.UnixNano()
if difference > sleepDetectionInterval {
w.log.Warn("keep alive hasnt been executed recently. Killing all connections to peers")
for _, p := range w.host.Network().Peers() {
err := w.host.Network().ClosePeer(p)
if err != nil {
w.log.Warn("while disconnecting peer", zap.Error(err))
}
}
lastTimeExecuted = time.Now()
continue
}
// Network's peers collection, // Network's peers collection,
// contains only currently active peers // contains only currently active peers
for _, p := range w.host.Network().Peers() { for _, p := range w.host.Network().Peers() {
@ -34,6 +51,8 @@ func (w *WakuNode) startKeepAlive(t time.Duration) {
go w.pingPeer(p) go w.pingPeer(p)
} }
} }
lastTimeExecuted = time.Now()
case <-w.quit: case <-w.quit:
return return
} }
@ -58,7 +77,7 @@ func (w *WakuNode) pingPeer(peer peer.ID) {
w.keepAliveFails[peer]++ w.keepAliveFails[peer]++
logger.Debug("could not ping", zap.Error(res.Error)) logger.Debug("could not ping", zap.Error(res.Error))
} else { } else {
w.keepAliveFails[peer] = 0 delete(w.keepAliveFails, peer)
} }
case <-ctx.Done(): case <-ctx.Done():
w.keepAliveFails[peer]++ w.keepAliveFails[peer]++