Skip to content

Commit

Permalink
chore: add logic for service peer connection so make room for every one
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Dec 4, 2024
1 parent 1b532e8 commit fa97a4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions waku/factory/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
17 changes: 17 additions & 0 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -1011,6 +1027,7 @@ proc new*(
outRelayPeersTarget: outRelayPeersTarget,
inRelayPeersTarget: maxRelayPeersValue - outRelayPeersTarget,
maxRelayPeers: maxRelayPeersValue,
maxServicePeers: maxServicePeersValue,
maxFailedAttempts: maxFailedAttempts,
colocationLimit: colocationLimit,
shardedPeerManagement: shardedPeerManagement,
Expand Down

0 comments on commit fa97a4f

Please sign in to comment.