mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-05 23:13:09 +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"
|
info "Exiting blockexc task runner"
|
||||||
|
|
||||||
proc selectRandom*(peers: seq[BlockExcPeerCtx]): BlockExcPeerCtx =
|
proc selectRandom*(peers: seq[BlockExcPeerCtx]): BlockExcPeerCtx {.gcsafe, raises: [].} =
|
||||||
Rng.instance.sample(peers)
|
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*(
|
proc new*(
|
||||||
T: type BlockExcEngine,
|
T: type BlockExcEngine,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user