rank peers by their score instead of their memory address (#6149)
The `<` function to compare peers was not exported, leading to the same peer be acquired over and over again until kick. `mixin` doesn't pull it into `peerCmp` without `*` export, and with the export no mixin needed.
This commit is contained in:
parent
e7dede344a
commit
a561024d88
|
@ -456,15 +456,15 @@ func netKbps*(peer: Peer): float {.inline.} =
|
||||||
## Returns current network throughput average value in Kbps for peer ``peer``.
|
## Returns current network throughput average value in Kbps for peer ``peer``.
|
||||||
round(((peer.netThroughput.average / 1024) * 10_000) / 10_000)
|
round(((peer.netThroughput.average / 1024) * 10_000) / 10_000)
|
||||||
|
|
||||||
func `<`(a, b: Peer): bool =
|
# /!\ Must be exported to be seen by `peerCmp`
|
||||||
## Comparison function, which first checks peer's scores, and if the peers'
|
func `<`*(a, b: Peer): bool =
|
||||||
## score is equal it compares peers' network throughput.
|
## Comparison function indicating `true` if peer `a` ranks worse than peer `b`
|
||||||
if a.score < b.score:
|
if a.score != b.score:
|
||||||
true
|
a.score < b.score
|
||||||
elif a.score == b.score:
|
elif a.netThroughput.average != b.netThroughput.average:
|
||||||
(a.netThroughput.average < b.netThroughput.average)
|
a.netThroughput.average < b.netThroughput.average
|
||||||
else:
|
else:
|
||||||
false
|
system.`<`(a, b)
|
||||||
|
|
||||||
const
|
const
|
||||||
maxRequestQuota = 1000000
|
maxRequestQuota = 1000000
|
||||||
|
|
|
@ -502,7 +502,8 @@ proc acquireItemImpl[A, B](pool: PeerPool[A, B],
|
||||||
let pindex =
|
let pindex =
|
||||||
if filter == {PeerType.Incoming, PeerType.Outgoing}:
|
if filter == {PeerType.Incoming, PeerType.Outgoing}:
|
||||||
if len(pool.outQueue) > 0 and len(pool.incQueue) > 0:
|
if len(pool.outQueue) > 0 and len(pool.incQueue) > 0:
|
||||||
# Don't think `<` is actually `<` here.
|
# `<` here is the `PeerIndex` implementation (`HeapQueue` uses `<`),
|
||||||
|
# which then flips the arguments to rank `>` on `A` using `pool.cmp`
|
||||||
if pool.incQueue[0] < pool.outQueue[0]:
|
if pool.incQueue[0] < pool.outQueue[0]:
|
||||||
inc(pool.acqIncPeersCount)
|
inc(pool.acqIncPeersCount)
|
||||||
let item = pool.incQueue.pop()
|
let item = pool.incQueue.pop()
|
||||||
|
|
Loading…
Reference in New Issue