2021-12-08 14:21:30 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-26 20:08:27 +00:00
|
|
|
"sync"
|
2021-12-08 14:21:30 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2021-12-08 14:21:30 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/logging"
|
2022-05-27 13:25:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-08 14:21:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxAllowedPingFailures = 2
|
|
|
|
|
2023-08-08 14:44:38 +00:00
|
|
|
// If the difference between the last time the keep alive code was executed and now is greater
|
|
|
|
// than sleepDectectionIntervalFactor * keepAlivePeriod, force the ping verification to disconnect
|
|
|
|
// the peers if they don't reply back
|
|
|
|
const sleepDetectionIntervalFactor = 3
|
2023-01-08 18:33:30 +00:00
|
|
|
|
2021-12-08 14:21:30 +00:00
|
|
|
// startKeepAlive creates a go routine that periodically pings connected peers.
|
|
|
|
// This is necessary because TCP connections are automatically closed due to inactivity,
|
|
|
|
// and doing a ping will avoid this (with a small bandwidth cost)
|
2023-01-06 22:37:57 +00:00
|
|
|
func (w *WakuNode) startKeepAlive(ctx context.Context, t time.Duration) {
|
2023-01-08 18:33:30 +00:00
|
|
|
defer w.wg.Done()
|
|
|
|
w.log.Info("setting up ping protocol", zap.Duration("duration", t))
|
|
|
|
ticker := time.NewTicker(t)
|
|
|
|
defer ticker.Stop()
|
2022-12-05 19:06:33 +00:00
|
|
|
|
2023-01-08 18:33:30 +00:00
|
|
|
lastTimeExecuted := w.timesource.Now()
|
2022-12-08 19:48:16 +00:00
|
|
|
|
2023-08-08 14:44:38 +00:00
|
|
|
sleepDetectionInterval := int64(t) * sleepDetectionIntervalFactor
|
2022-12-05 19:06:33 +00:00
|
|
|
|
2023-01-08 18:33:30 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
difference := w.timesource.Now().UnixNano() - lastTimeExecuted.UnixNano()
|
2023-08-08 14:44:38 +00:00
|
|
|
forceDisconnectOnPingFailure := false
|
2023-01-08 18:33:30 +00:00
|
|
|
if difference > sleepDetectionInterval {
|
2023-08-08 14:44:38 +00:00
|
|
|
forceDisconnectOnPingFailure = true
|
2023-01-08 18:33:30 +00:00
|
|
|
lastTimeExecuted = w.timesource.Now()
|
2023-08-08 14:44:38 +00:00
|
|
|
w.log.Warn("keep alive hasnt been executed recently. Killing connections to peers if ping fails")
|
2023-01-08 18:33:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-12-05 19:06:33 +00:00
|
|
|
|
2023-01-08 18:33:30 +00:00
|
|
|
// Network's peers collection,
|
|
|
|
// contains only currently active peers
|
2023-01-26 20:08:27 +00:00
|
|
|
pingWg := sync.WaitGroup{}
|
|
|
|
peersToPing := w.host.Network().Peers()
|
|
|
|
pingWg.Add(len(peersToPing))
|
|
|
|
for _, p := range peersToPing {
|
2023-01-08 18:33:30 +00:00
|
|
|
if p != w.host.ID() {
|
2023-08-08 14:44:38 +00:00
|
|
|
go w.pingPeer(ctx, &pingWg, p, forceDisconnectOnPingFailure)
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-26 20:08:27 +00:00
|
|
|
pingWg.Wait()
|
2023-01-08 18:33:30 +00:00
|
|
|
|
|
|
|
lastTimeExecuted = w.timesource.Now()
|
|
|
|
case <-ctx.Done():
|
|
|
|
w.log.Info("stopping ping protocol")
|
|
|
|
return
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
2023-01-08 18:33:30 +00:00
|
|
|
}
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 14:44:38 +00:00
|
|
|
func (w *WakuNode) pingPeer(ctx context.Context, wg *sync.WaitGroup, peerID peer.ID, forceDisconnectOnFail bool) {
|
2023-01-26 20:08:27 +00:00
|
|
|
defer wg.Done()
|
2021-12-08 14:21:30 +00:00
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 7*time.Second)
|
2021-12-08 14:21:30 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2023-08-08 14:44:38 +00:00
|
|
|
logger := w.log.With(logging.HostID("peer", peerID))
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Debug("pinging")
|
2023-08-08 14:44:38 +00:00
|
|
|
pr := ping.Ping(ctx, w.host, peerID)
|
2021-12-08 14:21:30 +00:00
|
|
|
select {
|
|
|
|
case res := <-pr:
|
|
|
|
if res.Error != nil {
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Lock()
|
2023-08-08 14:44:38 +00:00
|
|
|
w.keepAliveFails[peerID]++
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Unlock()
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Debug("could not ping", zap.Error(res.Error))
|
2021-12-08 14:21:30 +00:00
|
|
|
} else {
|
2023-01-30 09:41:14 +00:00
|
|
|
w.keepAliveMutex.Lock()
|
2023-08-08 14:44:38 +00:00
|
|
|
delete(w.keepAliveFails, peerID)
|
2023-01-30 09:41:14 +00:00
|
|
|
w.keepAliveMutex.Unlock()
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Lock()
|
2023-08-08 14:44:38 +00:00
|
|
|
w.keepAliveFails[peerID]++
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Unlock()
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Debug("could not ping (context done)", zap.Error(ctx.Err()))
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Lock()
|
2023-08-08 14:44:38 +00:00
|
|
|
if (forceDisconnectOnFail || w.keepAliveFails[peerID] > maxAllowedPingFailures) && w.host.Network().Connectedness(peerID) == network.Connected {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Info("disconnecting peer")
|
2023-08-08 14:44:38 +00:00
|
|
|
if err := w.host.Network().ClosePeer(peerID); err != nil {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Debug("closing conn to peer", zap.Error(err))
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
2023-08-08 14:44:38 +00:00
|
|
|
w.keepAliveFails[peerID] = 0
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
2023-01-26 20:08:27 +00:00
|
|
|
w.keepAliveMutex.Unlock()
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|