Skip to content

Commit

Permalink
adding a dynamic sleep interval in the connectivity loop (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Sep 12, 2024
1 parent 1713f56 commit 51391aa
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,21 @@ proc relayConnectivityLoop*(pm: PeerManager) {.async.} =
await pm.manageRelayPeers()
else:
await pm.connectToRelayPeers()
await sleepAsync(ConnectivityLoopInterval)
let
(inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
excessInConns = max(inRelayPeers.len - pm.inRelayPeersTarget, 0)

# One minus the percentage of excess connections relative to the target, limited to 100%
# We calculate one minus this percentage because we want the factor to be inversely proportional to the number of excess peers
inFactor = 1 - min(excessInConns / pm.inRelayPeersTarget, 1)
# Percentage of out relay peers relative to the target
outFactor = min(outRelayPeers.len / pm.outRelayPeersTarget, 1)
factor = min(outFactor, inFactor)
dynamicSleepInterval =
chronos.seconds(int(float(ConnectivityLoopInterval.seconds()) * factor))

# Shorten the connectivity loop interval dynamically based on percentage of peers to fill or connections to prune
await sleepAsync(dynamicSleepInterval)

proc logAndMetrics(pm: PeerManager) {.async.} =
heartbeat "Scheduling log and metrics run", LogAndMetricsInterval:
Expand Down

0 comments on commit 51391aa

Please sign in to comment.