From ac9241b0776a81b0d5fa10e3aca20875b3316ec4 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Tue, 15 Oct 2024 19:21:07 +0530 Subject: [PATCH] feat: remove bad peers based on dial errors for lightmode --- waku/v2/peermanager/peer_manager.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/waku/v2/peermanager/peer_manager.go b/waku/v2/peermanager/peer_manager.go index 14cc100dd..4f40178ab 100644 --- a/waku/v2/peermanager/peer_manager.go +++ b/waku/v2/peermanager/peer_manager.go @@ -101,7 +101,8 @@ const ( const maxFailedAttempts = 5 const prunePeerStoreInterval = 10 * time.Minute const peerConnectivityLoopSecs = 15 -const maxConnsToPeerRatio = 5 +const maxConnsToPeerRatio = 3 +const badPeersCleanupInterval = 1 * time.Minute // 80% relay peers 20% service peers func relayAndServicePeers(maxConnections int) (int, int) { @@ -256,16 +257,31 @@ func (pm *PeerManager) Start(ctx context.Context) { } } +func (pm *PeerManager) removeBadPeers() { + if !pm.RelayEnabled { + for _, peerID := range pm.host.Peerstore().Peers() { + if pm.host.Peerstore().(wps.WakuPeerstore).ConnFailures(peerID) > 5 { + //delete peer from peerStore + pm.RemovePeer(peerID) + } + } + } +} + func (pm *PeerManager) peerStoreLoop(ctx context.Context) { defer utils.LogOnPanic() t := time.NewTicker(prunePeerStoreInterval) + t1 := time.NewTicker(badPeersCleanupInterval) defer t.Stop() + defer t1.Stop() for { select { case <-ctx.Done(): return case <-t.C: pm.prunePeerStore() + case <-t1.C: + pm.removeBadPeers() } } }