From 70c53fc058f0d9ad4c3a800792784cbd3aa62047 Mon Sep 17 00:00:00 2001 From: Vaclav Pavlin Date: Tue, 26 Mar 2024 12:04:48 +0100 Subject: [PATCH] fix(networkmanager): regularly disconnect from random peers (#2553) --- apps/networkmonitor/networkmonitor.nim | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/networkmonitor/networkmonitor.nim b/apps/networkmonitor/networkmonitor.nim index 2e5d33bfc1..12ca3dd591 100644 --- a/apps/networkmonitor/networkmonitor.nim +++ b/apps/networkmonitor/networkmonitor.nim @@ -4,7 +4,7 @@ else: {.push raises: [].} import - std/[tables, strutils, times, sequtils], + std/[tables, strutils, times, sequtils, random], stew/results, stew/shims/net, chronicles, @@ -27,6 +27,7 @@ import ../../waku/waku_enr, ../../waku/waku_discv5, ../../waku/waku_dnsdisc, + ../../waku/waku_relay, ../../waku/waku_rln_relay, ../../waku/factory/builder, ../wakunode2/networks_config, @@ -41,6 +42,7 @@ const ReconnectTime = 60 const MaxConnectionRetries = 5 const ResetRetriesAfter = 1200 const AvgPingWindow = 10.0 +const MaxConnectedPeers = 150 const git_version* {.strdefine.} = "n/a" @@ -128,6 +130,16 @@ proc setConnectedPeersMetrics( var analyzeFuts: seq[Future[Result[string, string]]] + var (inConns, outConns) = node.peer_manager.connectedPeers(WakuRelayCodec) + info "connected peers", inConns=inConns.len, outConns=outConns.len + + shuffle(outConns) + + if outConns.len >= toInt(MaxConnectedPeers/2): + for p in outConns[0 ..< toInt(outConns.len/2)]: + trace "Pruning Peer", Peer = $p + asyncSpawn(node.switch.disconnect(p)) + # iterate all newly discovered nodes for discNode in discoveredNodes: let typedRecord = discNode.record.toTypedRecord() @@ -429,7 +441,8 @@ proc initAndStartApp( nodeBuilder.withNodeKey(key) nodeBuilder.withRecord(record) - nodeBuilder.withPeerManagerConfig(maxRelayPeers = none(int), shardAware = true) + nodeBUilder.withSwitchConfiguration(maxConnections = some(MaxConnectedPeers)) + nodeBuilder.withPeerManagerConfig(maxRelayPeers = some(20), shardAware = true) let res = nodeBuilder.withNetworkConfigurationDetails(bindIp, nodeTcpPort) if res.isErr(): return err("node building error" & $res.error)