Skip to content

Commit

Permalink
chore(networking): leave room for service peers
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Jun 29, 2023
1 parent 96aa59b commit 4e2f9cc
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions waku/v2/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const
PrunePeerStoreInterval = chronos.minutes(5)

# How often metrics and logs are shown/updated
LogAndMetricsInterval = chronos.seconds(60)
LogAndMetricsInterval = chronos.minutes(3)

# Max peers that we allow from the same IP
ColocationLimit = 5
Expand All @@ -71,7 +71,8 @@ type
storage: PeerStorage
serviceSlots*: Table[string, RemotePeerInfo]
maxRelayPeers*: int
outPeersTarget*: int
outRelayPeersTarget: int
inRelayPeersTarget: int
ipTable*: Table[string, seq[PeerId]]
colocationLimit*: int
started: bool
Expand Down Expand Up @@ -376,12 +377,15 @@ proc new*(T: type PeerManager,
maxBackoff=backoff
raise newException(Defect, "Max backoff time can't be over 1 week")

let outRelayPeersTarget = max(maxRelayPeers div 3, 10)

let pm = PeerManager(switch: switch,
peerStore: switch.peerStore,
storage: storage,
initialBackoffInSec: initialBackoffInSec,
backoffFactor: backoffFactor,
outPeersTarget: max(maxConnections div 2, 10),
outRelayPeersTarget: outRelayPeersTarget,
inRelayPeersTarget: maxRelayPeers - outRelayPeersTarget,
maxFailedAttempts: maxFailedAttempts,
colocationLimit: colocationLimit,
maxRelayPeers: maxRelayPeers)
Expand Down Expand Up @@ -571,16 +575,12 @@ proc connectToRelayPeers*(pm: PeerManager) {.async.} =
let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
let maxConnections = pm.switch.connManager.inSema.size
let totalRelayPeers = inRelayPeers.len + outRelayPeers.len
let inPeersTarget = maxConnections - pm.outPeersTarget

if inRelayPeers.len > inPeersTarget:
await pm.pruneInRelayConns(inRelayPeers.len-inPeersTarget)
let inPeersTarget = maxConnections - pm.outRelayPeersTarget

if outRelayPeers.len >= pm.outPeersTarget:
return
if inRelayPeers.len > pm.inRelayPeersTarget:
await pm.pruneInRelayConns(inRelayPeers.len - pm.inRelayPeersTarget)

# Leave some room for service peers
if totalRelayPeers >= (maxConnections - 5):
if outRelayPeers.len >= pm.outRelayPeersTarget:
return

let notConnectedPeers = pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs))
Expand Down Expand Up @@ -670,15 +670,14 @@ proc logAndMetrics(pm: PeerManager) {.async.} =
let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
let maxConnections = pm.switch.connManager.inSema.size
let totalRelayPeers = inRelayPeers.len + outRelayPeers.len
let inPeersTarget = maxConnections - pm.outPeersTarget
let notConnectedPeers = pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs))
let outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId))
let totalConnections = pm.switch.connManager.getConnections().len

info "Relay peer connections",
inRelayConns = $inRelayPeers.len & "/" & $inPeersTarget,
outRelayConns = $outRelayPeers.len & "/" & $pm.outPeersTarget,
totalRelayConns = totalRelayPeers,
maxConnections = maxConnections,
inRelayConns = $inRelayPeers.len & "/" & $pm.inRelayPeersTarget,
outRelayConns = $outRelayPeers.len & "/" & $pm.outRelayPeersTarget,
totalConnections = $totalConnections & "/" & $maxConnections,
notConnectedPeers = notConnectedPeers.len,
outsideBackoffPeers = outsideBackoffPeers.len

Expand Down

0 comments on commit 4e2f9cc

Please sign in to comment.