From 2173fe224314976babcad6c72c6d3824ab6b0d39 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:07:03 +0100 Subject: [PATCH] fix: peer_manager - extend the number of connection requests to known peers (#2534) * peer_manager: limit the max num out conns from within the conn loop --- waku/node/peer_manager/peer_manager.nim | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index c2e245a0e..2ef5112a2 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -696,7 +696,7 @@ proc pruneInRelayConns(pm: PeerManager, amount: int) {.async.} = asyncSpawn(pm.switch.disconnect(p)) proc connectToRelayPeers*(pm: PeerManager) {.async.} = - let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec) + var (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec) let maxConnections = pm.switch.connManager.inSema.size let totalRelayPeers = inRelayPeers.len + outRelayPeers.len let inPeersTarget = maxConnections - pm.outRelayPeersTarget @@ -710,10 +710,23 @@ proc connectToRelayPeers*(pm: PeerManager) {.async.} = let notConnectedPeers = pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs)) - let outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId)) - let numPeersToConnect = min(outsideBackoffPeers.len, MaxParallelDials) - await pm.connectToNodes(outsideBackoffPeers[0 ..< numPeersToConnect]) + var outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId)) + + shuffle(outsideBackoffPeers) + + var index = 0 + var numPendingConnReqs = outsideBackoffPeers.len + ## number of outstanding connection requests + + while numPendingConnReqs > 0 and outRelayPeers.len < pm.outRelayPeersTarget: + let numPeersToConnect = min(numPendingConnReqs, MaxParallelDials) + await pm.connectToNodes(outsideBackoffPeers[index ..< (index + numPeersToConnect)]) + + (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec) + + index += numPeersToConnect + numPendingConnReqs -= numPeersToConnect proc manageRelayPeers*(pm: PeerManager) {.async.} = if pm.wakuMetadata.shards.len == 0: