mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-03 22:13:12 +00:00
Implement load-balanced peer selection for block requests
This commit is contained in:
parent
6891824226
commit
7cb4b31557
@ -836,8 +836,19 @@ proc blockexcTaskRunner(self: BlockExcEngine) {.async: (raises: []).} =
|
||||
|
||||
info "Exiting blockexc task runner"
|
||||
|
||||
proc selectRandom*(peers: seq[BlockExcPeerCtx]): BlockExcPeerCtx =
|
||||
Rng.instance.sample(peers)
|
||||
proc selectRandom*(peers: seq[BlockExcPeerCtx]): BlockExcPeerCtx {.gcsafe, raises: [].} =
|
||||
if peers.len == 1:
|
||||
return peers[0]
|
||||
|
||||
try:
|
||||
let sortedPeers = peers.sortedByIt(it.blocksRequested.len)
|
||||
|
||||
# Take top 40% of least loaded peers (minimum 3, maximum 8)
|
||||
let selectionPoolSize = max(3, min(8, int(float(peers.len) * 0.4)))
|
||||
let candidates = sortedPeers[0 ..< min(selectionPoolSize, sortedPeers.len)]
|
||||
return Rng.instance.sample(candidates)
|
||||
except CatchableError:
|
||||
return Rng.instance.sample(peers)
|
||||
|
||||
proc new*(
|
||||
T: type BlockExcEngine,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user