Skip to content

Commit

Permalink
chore: more simplify maxConnection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Dec 11, 2024
1 parent 7924c19 commit 02fd08c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
18 changes: 8 additions & 10 deletions waku/factory/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,6 @@ type WakuNodeConf* = object
name: "ext-multiaddr-only"
.}: bool

maxConnections* {.
desc: "Maximum allowed number of libp2p connections.",
defaultValue: 50,
name: "max-connections"
.}: uint16

colocationLimit* {.
desc:
"Max num allowed peers from the same IP. Set it to 0 to remove the limitation.",
Expand All @@ -207,12 +201,16 @@ type WakuNodeConf* = object
.}: int

maxRelayPeers* {.
desc: "Maximum allowed number of relay peers.", name: "max-relay-peers"
.}: Option[int]
desc: "Maximum allowed number of relay peers.",
name: "max-relay-peers"
defaultValue: 50
.}: uint64

maxServicePeers* {.
desc: "Maximum allowed number of service peers.", name: "max-service-peers"
.}: Option[int]
desc: "Maximum allowed number of service peers.",
name: "max-service-peers"
defaultValue: 50
.}: uint64

peerStoreCapacity* {.
desc: "Maximum stored peers in the peerstore.", name: "peer-store-capacity"
Expand Down
38 changes: 1 addition & 37 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -973,50 +973,14 @@ proc new*(
shardedPeerManagement = false,
): PeerManager {.gcsafe.} =
let capacity = switch.peerStore.capacity
let maxConnections = switch.connManager.inSema.size
let maxConnections = maxRelayPeers.get() + maxServicePeers.get()
if maxConnections > capacity:
error "Max number of connections can't be greater than PeerManager capacity",
capacity = capacity, maxConnections = maxConnections
raise newException(
Defect, "Max number of connections can't be greater than PeerManager capacity"
)

var maxRelayPeersValue = 0
if maxRelayPeers.isSome():
if maxRelayPeers.get() > maxConnections:
error "Max number of relay peers can't be greater than the max amount of connections",
maxConnections = maxConnections, maxRelayPeers = maxRelayPeers.get()
raise newException(
Defect,
"Max number of relay peers can't be greater than the max amount of connections",
)

if maxRelayPeers.get() == maxConnections:
warn "Max number of relay peers is equal to max amount of connections, peer won't be contributing to service peers",
maxConnections = maxConnections, maxRelayPeers = maxRelayPeers.get()
maxRelayPeersValue = maxRelayPeers.get()
else:
# 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 - maxRelayPeersValue)
# relay has higher priority then service peers
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 Down

0 comments on commit 02fd08c

Please sign in to comment.