From fa97a4ffbd3277e251ff58d2be53767714ca0ee9 Mon Sep 17 00:00:00 2001 From: DarshanBPatel Date: Thu, 28 Nov 2024 11:50:10 +0530 Subject: [PATCH] chore: add logic for service peer connection so make room for every one --- waku/factory/external_config.nim | 4 ++++ waku/node/peer_manager/peer_manager.nim | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/waku/factory/external_config.nim b/waku/factory/external_config.nim index 01db15fcb2..f053d80849 100644 --- a/waku/factory/external_config.nim +++ b/waku/factory/external_config.nim @@ -210,6 +210,10 @@ type WakuNodeConf* = object desc: "Maximum allowed number of relay peers.", name: "max-relay-peers" .}: Option[int] + maxServicePeers* {. + desc: "Maximum allowed number of service peers.", name: "max-service-peers" + .}: Option[int] + peerStoreCapacity* {. desc: "Maximum stored peers in the peerstore.", name: "peer-store-capacity" .}: Option[int] diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index e17e9fd0d1..9f484624fa 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -81,6 +81,7 @@ type PeerManager* = ref object of RootObj storage*: PeerStorage serviceSlots*: Table[string, RemotePeerInfo] maxRelayPeers*: int + maxServicePeers*: int outRelayPeersTarget: int inRelayPeersTarget: int ipTable*: Table[string, seq[PeerId]] @@ -959,6 +960,7 @@ proc new*( switch: Switch, wakuMetadata: WakuMetadata = nil, maxRelayPeers: Option[int] = none(int), + maxServicePeers: Option[int] = none(int), storage: PeerStorage = nil, initialBackoffInSec = InitialBackoffInSec, backoffFactor = BackoffFactor, @@ -993,6 +995,20 @@ proc new*( # Leave by default 20% of connections for service peers maxRelayPeersValue = maxConnections - (maxConnections div 5) + var maxServicePeersValue = 0 + var d_low = 4 + + if maxServicePeers.isSome(): + if maxServicePeers.get() > maxConnections - d_low: + error "Max number of service peers can't be greater than the max amount of connections minus d_low", + maxConnections = maxConnections, maxServicePeers = maxServicePeers.get() + raise newException( + Defect, "Max number of service peers can't be greater than the max amount of connections minus d_low" + ) + maxServicePeersValue = min(maxServicePeers.get(), maxConnections - d_low) + else: + maxServicePeersValue = max(maxConnections - maxRelayPeersValue, maxConnections div 5) + # attempt to calculate max backoff to prevent potential overflows or unreasonably high values let backoff = calculateBackoff(initialBackoffInSec, backoffFactor, maxFailedAttempts) if backoff.weeks() > 1: @@ -1011,6 +1027,7 @@ proc new*( outRelayPeersTarget: outRelayPeersTarget, inRelayPeersTarget: maxRelayPeersValue - outRelayPeersTarget, maxRelayPeers: maxRelayPeersValue, + maxServicePeers: maxServicePeersValue, maxFailedAttempts: maxFailedAttempts, colocationLimit: colocationLimit, shardedPeerManagement: shardedPeerManagement,