2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-03-26 06:52:01 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2020-09-16 10:00:11 +00:00
|
|
|
import std/[tables, heapqueue]
|
2019-11-25 20:18:02 +00:00
|
|
|
import chronos
|
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
export tables
|
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
type
|
|
|
|
PeerType* = enum
|
2020-09-21 16:02:27 +00:00
|
|
|
Incoming, Outgoing
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
PeerFlags = enum
|
|
|
|
Acquired, DeleteOnRelease
|
|
|
|
|
2020-07-07 09:49:08 +00:00
|
|
|
EventType = enum
|
|
|
|
NotEmptyEvent, NotFullEvent
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
PeerStatus* = enum
|
|
|
|
Success, ## Peer was successfully added to PeerPool.
|
|
|
|
DuplicateError, ## Peer is already present in PeerPool.
|
|
|
|
NoSpaceError, ## There no space for the peer in PeerPool.
|
|
|
|
LowScoreError, ## Peer has too low score.
|
|
|
|
DeadPeerError ## Peer is already dead.
|
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
PeerItem[T] = object
|
|
|
|
data: T
|
|
|
|
peerType: PeerType
|
|
|
|
flags: set[PeerFlags]
|
|
|
|
index: int
|
|
|
|
|
|
|
|
PeerIndex = object
|
|
|
|
data: int
|
2023-08-25 09:29:07 +00:00
|
|
|
cmp: proc(a, b: PeerIndex): bool {.gcsafe, raises: [].}
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
PeerScoreCheckCallback*[T] = proc(peer: T): bool {.gcsafe, raises: [].}
|
2020-04-23 15:31:00 +00:00
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
PeerCounterCallback* = proc() {.gcsafe, raises: [].}
|
2020-09-16 10:00:11 +00:00
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
PeerOnDeleteCallback*[T] = proc(peer: T) {.gcsafe, raises: [].}
|
2020-10-27 09:25:28 +00:00
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
PeerPool*[A, B] = ref object
|
2020-09-21 16:02:27 +00:00
|
|
|
incNotEmptyEvent*: AsyncEvent
|
|
|
|
outNotEmptyEvent*: AsyncEvent
|
|
|
|
incNotFullEvent*: AsyncEvent
|
|
|
|
outNotFullEvent*: AsyncEvent
|
2019-11-25 20:18:02 +00:00
|
|
|
incQueue: HeapQueue[PeerIndex]
|
|
|
|
outQueue: HeapQueue[PeerIndex]
|
|
|
|
registry: Table[B, PeerIndex]
|
|
|
|
storage: seq[PeerItem[A]]
|
2023-08-25 09:29:07 +00:00
|
|
|
cmp: proc(a, b: PeerIndex): bool {.gcsafe, raises: [].}
|
2020-04-23 15:31:00 +00:00
|
|
|
scoreCheck: PeerScoreCheckCallback[A]
|
2020-10-27 09:25:28 +00:00
|
|
|
onDeletePeer: PeerOnDeleteCallback[A]
|
2020-09-16 10:00:11 +00:00
|
|
|
peerCounter: PeerCounterCallback
|
2019-11-25 20:18:02 +00:00
|
|
|
maxPeersCount: int
|
|
|
|
maxIncPeersCount: int
|
|
|
|
maxOutPeersCount: int
|
|
|
|
curIncPeersCount: int
|
|
|
|
curOutPeersCount: int
|
|
|
|
acqIncPeersCount: int
|
|
|
|
acqOutPeersCount: int
|
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
PeerPoolError* = object of CatchableError
|
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
proc `<`*(a, b: PeerIndex): bool =
|
2020-09-21 16:02:27 +00:00
|
|
|
## PeerIndex ``a`` holds reference to ``cmp()`` procedure which has captured
|
|
|
|
## PeerPool instance.
|
2020-09-16 10:00:11 +00:00
|
|
|
a.cmp(b, a)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-01-24 01:35:38 +00:00
|
|
|
proc fireNotEmptyEvent[A, B](pool: PeerPool[A, B],
|
2020-11-20 10:00:22 +00:00
|
|
|
item: PeerItem[A]) =
|
2020-09-21 16:02:27 +00:00
|
|
|
case item.peerType:
|
|
|
|
of PeerType.Incoming:
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.incNotEmptyEvent.fire()
|
2020-09-21 16:02:27 +00:00
|
|
|
of PeerType.Outgoing:
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.outNotEmptyEvent.fire()
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-01-24 01:35:38 +00:00
|
|
|
proc fireNotFullEvent[A, B](pool: PeerPool[A, B],
|
2020-11-20 10:00:22 +00:00
|
|
|
item: PeerItem[A]) =
|
2020-09-21 16:02:27 +00:00
|
|
|
case item.peerType:
|
|
|
|
of PeerType.Incoming:
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.incNotFullEvent.fire()
|
2020-09-21 16:02:27 +00:00
|
|
|
of PeerType.Outgoing:
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.outNotFullEvent.fire()
|
|
|
|
|
2020-03-16 22:28:54 +00:00
|
|
|
iterator pairs*[A, B](pool: PeerPool[A, B]): (B, A) =
|
|
|
|
for peerId, peerIdx in pool.registry:
|
|
|
|
yield (peerId, pool.storage[peerIdx.data].data)
|
|
|
|
|
2020-07-07 09:49:08 +00:00
|
|
|
template incomingEvent(eventType: EventType): AsyncEvent =
|
|
|
|
case eventType
|
|
|
|
of EventType.NotEmptyEvent:
|
|
|
|
pool.incNotEmptyEvent
|
|
|
|
of EventType.NotFullEvent:
|
|
|
|
pool.incNotFullEvent
|
|
|
|
|
|
|
|
template outgoingEvent(eventType: EventType): AsyncEvent =
|
|
|
|
case eventType
|
|
|
|
of EventType.NotEmptyEvent:
|
|
|
|
pool.outNotEmptyEvent
|
|
|
|
of EventType.NotFullEvent:
|
|
|
|
pool.outNotFullEvent
|
|
|
|
|
|
|
|
proc waitForEvent[A, B](pool: PeerPool[A, B], eventType: EventType,
|
|
|
|
filter: set[PeerType]) {.async.} =
|
2019-11-25 20:18:02 +00:00
|
|
|
if filter == {PeerType.Incoming, PeerType.Outgoing} or filter == {}:
|
2020-07-07 09:49:08 +00:00
|
|
|
var fut1 = incomingEvent(eventType).wait()
|
|
|
|
var fut2 = outgoingEvent(eventType).wait()
|
2019-11-25 20:18:02 +00:00
|
|
|
try:
|
|
|
|
discard await one(fut1, fut2)
|
2023-09-22 11:06:27 +00:00
|
|
|
if fut1.finished():
|
|
|
|
if not(fut2.finished()):
|
|
|
|
await fut2.cancelAndWait()
|
2020-07-07 09:49:08 +00:00
|
|
|
incomingEvent(eventType).clear()
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
2023-09-22 11:06:27 +00:00
|
|
|
if not(fut1.finished()):
|
|
|
|
await fut1.cancelAndWait()
|
2020-07-07 09:49:08 +00:00
|
|
|
outgoingEvent(eventType).clear()
|
2020-08-06 18:47:39 +00:00
|
|
|
except CancelledError as exc:
|
2023-09-22 11:06:27 +00:00
|
|
|
var pending: seq[FutureBase]
|
|
|
|
if not(fut1.finished()):
|
|
|
|
pending.add(fut1.cancelAndWait())
|
|
|
|
if not(fut2.finished()):
|
|
|
|
pending.add(fut2.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2020-08-06 18:47:39 +00:00
|
|
|
raise exc
|
2019-11-25 20:18:02 +00:00
|
|
|
elif PeerType.Incoming in filter:
|
2020-07-07 09:49:08 +00:00
|
|
|
await incomingEvent(eventType).wait()
|
|
|
|
incomingEvent(eventType).clear()
|
2019-11-25 20:18:02 +00:00
|
|
|
elif PeerType.Outgoing in filter:
|
2020-07-07 09:49:08 +00:00
|
|
|
await outgoingEvent(eventType).wait()
|
|
|
|
outgoingEvent(eventType).clear()
|
|
|
|
|
|
|
|
proc waitNotEmptyEvent[A, B](pool: PeerPool[A, B],
|
|
|
|
filter: set[PeerType]): Future[void] =
|
|
|
|
pool.waitForEvent(EventType.NotEmptyEvent, filter)
|
2020-01-24 01:35:38 +00:00
|
|
|
|
|
|
|
proc waitNotFullEvent[A, B](pool: PeerPool[A, B],
|
2020-07-07 09:49:08 +00:00
|
|
|
filter: set[PeerType]): Future[void] =
|
|
|
|
pool.waitForEvent(EventType.NotFullEvent, filter)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-04-23 15:31:00 +00:00
|
|
|
proc newPeerPool*[A, B](maxPeers = -1, maxIncomingPeers = -1,
|
|
|
|
maxOutgoingPeers = -1,
|
2020-10-09 13:44:51 +00:00
|
|
|
scoreCheckCb: PeerScoreCheckCallback[A] = nil,
|
2020-10-27 09:25:28 +00:00
|
|
|
peerCounterCb: PeerCounterCallback = nil,
|
|
|
|
onDeleteCb: PeerOnDeleteCallback[A] = nil): PeerPool[A, B] =
|
2019-11-25 20:18:02 +00:00
|
|
|
## Create new PeerPool.
|
|
|
|
##
|
|
|
|
## ``maxPeers`` - maximum number of peers allowed. All the peers which
|
|
|
|
## exceeds this number will be rejected (``addPeer()`` procedure will return
|
|
|
|
## ``false``). By default this number is infinite.
|
|
|
|
##
|
|
|
|
## ``maxIncomingPeers`` - maximum number of incoming peers allowed. All the
|
|
|
|
## incoming peers exceeds this number will be rejected. By default this
|
|
|
|
## number is infinite.
|
|
|
|
##
|
|
|
|
## ``maxOutgoingPeers`` - maximum number of outgoing peers allowed. All the
|
|
|
|
## outgoing peers exceeds this number will be rejected. By default this
|
|
|
|
## number if infinite.
|
|
|
|
##
|
2020-04-23 15:31:00 +00:00
|
|
|
## ``scoreCheckCb`` - callback which will be called for all released peers.
|
|
|
|
## If callback procedure returns ``false`` peer will be removed from
|
|
|
|
## PeerPool.
|
|
|
|
##
|
2020-09-16 10:00:11 +00:00
|
|
|
## ``peerCountCb`` - callback to be called when number of peers in PeerPool
|
|
|
|
## has been changed.
|
|
|
|
##
|
2020-10-27 09:25:28 +00:00
|
|
|
## ``onDeleteCb`` - callback to be called when peer is leaving PeerPool.
|
|
|
|
##
|
2019-11-25 20:18:02 +00:00
|
|
|
## Please note, that if ``maxPeers`` is positive non-zero value, then equation
|
|
|
|
## ``maxPeers >= maxIncomingPeers + maxOutgoingPeers`` must be ``true``.
|
|
|
|
var res = PeerPool[A, B]()
|
|
|
|
if maxPeers != -1:
|
|
|
|
doAssert(maxPeers >= maxIncomingPeers + maxOutgoingPeers)
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
res.maxPeersCount = if maxPeers < 0: high(int) else: maxPeers
|
|
|
|
res.maxIncPeersCount =
|
|
|
|
if maxIncomingPeers < 0:
|
|
|
|
high(int)
|
|
|
|
else:
|
|
|
|
maxIncomingPeers
|
|
|
|
res.maxOutPeersCount =
|
|
|
|
if maxOutgoingPeers < 0:
|
|
|
|
high(int)
|
|
|
|
else:
|
|
|
|
maxOutgoingPeers
|
|
|
|
|
2020-01-24 01:35:38 +00:00
|
|
|
res.incNotEmptyEvent = newAsyncEvent()
|
|
|
|
res.outNotEmptyEvent = newAsyncEvent()
|
|
|
|
res.incNotFullEvent = newAsyncEvent()
|
|
|
|
res.outNotFullEvent = newAsyncEvent()
|
2019-11-25 20:18:02 +00:00
|
|
|
res.incQueue = initHeapQueue[PeerIndex]()
|
|
|
|
res.outQueue = initHeapQueue[PeerIndex]()
|
|
|
|
res.registry = initTable[B, PeerIndex]()
|
2020-04-23 15:31:00 +00:00
|
|
|
res.scoreCheck = scoreCheckCb
|
2020-09-16 10:00:11 +00:00
|
|
|
res.peerCounter = peerCounterCb
|
2020-10-27 09:25:28 +00:00
|
|
|
res.onDeletePeer = onDeleteCb
|
2019-11-25 20:18:02 +00:00
|
|
|
res.storage = newSeq[PeerItem[A]]()
|
|
|
|
|
|
|
|
proc peerCmp(a, b: PeerIndex): bool {.closure, gcsafe.} =
|
|
|
|
let p1 = res.storage[a.data].data
|
|
|
|
let p2 = res.storage[b.data].data
|
2020-09-16 10:00:11 +00:00
|
|
|
p1 < p2
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
res.cmp = peerCmp
|
2020-09-16 10:00:11 +00:00
|
|
|
res
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc len*[A, B](pool: PeerPool[A, B]): int =
|
|
|
|
## Returns number of registered peers in PeerPool ``pool``. This number
|
|
|
|
## includes all the peers (acquired and available).
|
2020-09-16 10:00:11 +00:00
|
|
|
len(pool.registry)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
proc lenCurrent*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): int {.inline.} =
|
|
|
|
## Returns number of registered peers in PeerPool ``pool`` which satisfies
|
|
|
|
## filter ``filter``.
|
|
|
|
(if PeerType.Incoming in filter: pool.curIncPeersCount else: 0) +
|
|
|
|
(if PeerType.Outgoing in filter: pool.curOutPeersCount else: 0)
|
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
proc lenAvailable*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): int {.inline.} =
|
|
|
|
## Returns number of available peers in PeerPool ``pool`` which satisfies
|
|
|
|
## filter ``filter``.
|
2020-09-16 10:00:11 +00:00
|
|
|
(if PeerType.Incoming in filter: len(pool.incQueue) else: 0) +
|
|
|
|
(if PeerType.Outgoing in filter: len(pool.outQueue) else: 0)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc lenAcquired*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): int {.inline.} =
|
|
|
|
## Returns number of acquired peers in PeerPool ``pool`` which satisifies
|
|
|
|
## filter ``filter``.
|
2020-09-16 10:00:11 +00:00
|
|
|
(if PeerType.Incoming in filter: pool.acqIncPeersCount else: 0) +
|
|
|
|
(if PeerType.Outgoing in filter: pool.acqOutPeersCount else: 0)
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
proc lenSpace*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): int {.inline.} =
|
|
|
|
## Returns number of available space for peers in PeerPool ``pool`` which
|
|
|
|
## satisfies filter ``filter``.
|
|
|
|
let curPeersCount = pool.curIncPeersCount + pool.curOutPeersCount
|
|
|
|
let totalSpace = pool.maxPeersCount - curPeersCount
|
|
|
|
let incoming = min(totalSpace, pool.maxIncPeersCount - pool.curIncPeersCount)
|
|
|
|
let outgoing = min(totalSpace, pool.maxOutPeersCount - pool.curOutPeersCount)
|
|
|
|
if filter == {PeerType.Incoming, PeerType.Outgoing}:
|
|
|
|
# To avoid overflow check we need to check by ourself.
|
|
|
|
if uint64(incoming) + uint64(outgoing) > uint64(high(int)):
|
|
|
|
min(totalSpace, high(int))
|
|
|
|
else:
|
|
|
|
min(totalSpace, incoming + outgoing)
|
|
|
|
elif PeerType.Incoming in filter:
|
|
|
|
incoming
|
|
|
|
else:
|
|
|
|
outgoing
|
|
|
|
|
2020-09-16 10:00:11 +00:00
|
|
|
proc shortLogAvailable*[A, B](pool: PeerPool[A, B]): string =
|
|
|
|
$len(pool.incQueue) & "/" & $len(pool.outQueue)
|
|
|
|
|
|
|
|
proc shortLogAcquired*[A, B](pool: PeerPool[A, B]): string =
|
|
|
|
$pool.acqIncPeersCount & "/" & $pool.acqOutPeersCount
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
proc shortLogSpace*[A, B](pool: PeerPool[A, B]): string =
|
|
|
|
$pool.lenSpace({PeerType.Incoming}) & "/" &
|
|
|
|
$pool.lenSpace({PeerType.Outgoing})
|
|
|
|
|
|
|
|
proc shortLogCurrent*[A, B](pool: PeerPool[A, B]): string =
|
|
|
|
$pool.curIncPeersCount & "/" & $pool.curOutPeersCount
|
|
|
|
|
2020-10-09 13:44:51 +00:00
|
|
|
proc checkPeerScore*[A, B](pool: PeerPool[A, B], peer: A): bool {.inline.} =
|
2020-04-23 15:31:00 +00:00
|
|
|
## Returns ``true`` if peer passing score check.
|
|
|
|
if not(isNil(pool.scoreCheck)):
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.scoreCheck(peer)
|
2020-04-23 15:31:00 +00:00
|
|
|
else:
|
2020-09-16 10:00:11 +00:00
|
|
|
true
|
|
|
|
|
2020-11-20 10:00:22 +00:00
|
|
|
proc peerCountChanged[A, B](pool: PeerPool[A, B]) =
|
2020-09-16 10:00:11 +00:00
|
|
|
## Call callback when number of peers changed.
|
|
|
|
if not(isNil(pool.peerCounter)):
|
|
|
|
pool.peerCounter()
|
2020-04-23 15:31:00 +00:00
|
|
|
|
2020-11-20 10:00:22 +00:00
|
|
|
proc peerDeleted[A, B](pool: PeerPool[A, B], peer: A) =
|
2020-10-27 09:25:28 +00:00
|
|
|
## Call callback when peer is leaving PeerPool.
|
|
|
|
if not(isNil(pool.onDeletePeer)):
|
|
|
|
pool.onDeletePeer(peer)
|
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
proc deletePeer*[A, B](pool: PeerPool[A, B], peer: A, force = false): bool =
|
|
|
|
## Remove ``peer`` from PeerPool ``pool``.
|
|
|
|
##
|
|
|
|
## Deletion occurs immediately only if peer is available, otherwise it will
|
|
|
|
## be deleted only when peer will be released. You can change this behavior
|
|
|
|
## with ``force`` option.
|
|
|
|
mixin getKey
|
2020-09-16 10:00:11 +00:00
|
|
|
let key = getKey(peer)
|
2019-11-25 20:18:02 +00:00
|
|
|
if pool.registry.hasKey(key):
|
2021-03-24 16:20:55 +00:00
|
|
|
let pindex = try: pool.registry[key].data
|
|
|
|
except KeyError: raiseAssert "checked with hasKey"
|
2019-11-25 20:18:02 +00:00
|
|
|
var item = addr(pool.storage[pindex])
|
|
|
|
if (PeerFlags.Acquired in item[].flags):
|
|
|
|
if not(force):
|
|
|
|
item[].flags.incl(PeerFlags.DeleteOnRelease)
|
|
|
|
else:
|
|
|
|
if item[].peerType == PeerType.Incoming:
|
|
|
|
dec(pool.curIncPeersCount)
|
|
|
|
dec(pool.acqIncPeersCount)
|
|
|
|
elif item[].peerType == PeerType.Outgoing:
|
|
|
|
dec(pool.curOutPeersCount)
|
|
|
|
dec(pool.acqOutPeersCount)
|
2020-01-24 01:35:38 +00:00
|
|
|
|
2020-09-16 10:00:11 +00:00
|
|
|
# Indicate that we have an empty space
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.fireNotFullEvent(item[])
|
2019-11-25 20:18:02 +00:00
|
|
|
# Cleanup storage with default item, and removing key from hashtable.
|
|
|
|
pool.storage[pindex] = PeerItem[A]()
|
|
|
|
pool.registry.del(key)
|
2020-10-27 09:25:28 +00:00
|
|
|
pool.peerDeleted(peer)
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.peerCountChanged()
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
|
|
|
if item[].peerType == PeerType.Incoming:
|
|
|
|
# If peer is available, then its copy present in heapqueue, so we need
|
|
|
|
# to remove it.
|
|
|
|
for i in 0 ..< len(pool.incQueue):
|
|
|
|
if pool.incQueue[i].data == pindex:
|
|
|
|
pool.incQueue.del(i)
|
|
|
|
break
|
|
|
|
dec(pool.curIncPeersCount)
|
|
|
|
elif item[].peerType == PeerType.Outgoing:
|
|
|
|
# If peer is available, then its copy present in heapqueue, so we need
|
|
|
|
# to remove it.
|
|
|
|
for i in 0 ..< len(pool.outQueue):
|
|
|
|
if pool.outQueue[i].data == pindex:
|
|
|
|
pool.outQueue.del(i)
|
|
|
|
break
|
|
|
|
dec(pool.curOutPeersCount)
|
2020-01-24 01:35:38 +00:00
|
|
|
|
2020-09-16 10:00:11 +00:00
|
|
|
# Indicate that we have an empty space
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.fireNotFullEvent(item[])
|
2019-11-25 20:18:02 +00:00
|
|
|
# Cleanup storage with default item, and removing key from hashtable.
|
|
|
|
pool.storage[pindex] = PeerItem[A]()
|
|
|
|
pool.registry.del(key)
|
2020-10-27 09:25:28 +00:00
|
|
|
pool.peerDeleted(peer)
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.peerCountChanged()
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-01-24 01:35:38 +00:00
|
|
|
proc addPeerImpl[A, B](pool: PeerPool[A, B], peer: A, peerKey: B,
|
2020-09-21 16:02:27 +00:00
|
|
|
peerType: PeerType) =
|
2023-06-08 13:42:19 +00:00
|
|
|
proc onPeerClosed(udata: pointer) {.gcsafe, raises: [].} =
|
2020-01-24 01:35:38 +00:00
|
|
|
discard pool.deletePeer(peer)
|
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
let item = PeerItem[A](data: peer, peerType: peerType,
|
2020-01-24 01:35:38 +00:00
|
|
|
index: len(pool.storage))
|
|
|
|
pool.storage.add(item)
|
|
|
|
var pitem = addr(pool.storage[^1])
|
|
|
|
let pindex = PeerIndex(data: item.index, cmp: pool.cmp)
|
|
|
|
pool.registry[peerKey] = pindex
|
|
|
|
pitem[].data.getFuture().addCallback(onPeerClosed)
|
2020-09-21 16:02:27 +00:00
|
|
|
if peerType == PeerType.Incoming:
|
|
|
|
inc(pool.curIncPeersCount)
|
|
|
|
pool.incQueue.push(pindex)
|
|
|
|
pool.incNotEmptyEvent.fire()
|
|
|
|
elif peerType == PeerType.Outgoing:
|
|
|
|
inc(pool.curOutPeersCount)
|
|
|
|
pool.outQueue.push(pindex)
|
|
|
|
pool.outNotEmptyEvent.fire()
|
|
|
|
pool.peerCountChanged()
|
|
|
|
|
|
|
|
proc checkPeer*[A, B](pool: PeerPool[A, B], peer: A): PeerStatus {.inline.} =
|
|
|
|
## Checks if peer could be added to PeerPool, e.g. it has:
|
|
|
|
##
|
|
|
|
## * Positive value of peer's score - (PeerStatus.LowScoreError)
|
|
|
|
## * Peer's key is not present in PeerPool - (PeerStatus.DuplicateError)
|
|
|
|
## * Peer's lifetime future is not finished yet - (PeerStatus.DeadPeerError)
|
|
|
|
##
|
|
|
|
## If peer could be added to PeerPool procedure returns (PeerStatus.Success)
|
|
|
|
mixin getKey, getFuture
|
|
|
|
if not(pool.checkPeerScore(peer)):
|
|
|
|
PeerStatus.LowScoreError
|
|
|
|
else:
|
|
|
|
let peerKey = getKey(peer)
|
|
|
|
if not(pool.registry.hasKey(peerKey)):
|
|
|
|
if not(peer.getFuture().finished):
|
|
|
|
PeerStatus.Success
|
|
|
|
else:
|
|
|
|
PeerStatus.DeadPeerError
|
|
|
|
else:
|
|
|
|
PeerStatus.DuplicateError
|
2020-01-24 01:35:38 +00:00
|
|
|
|
|
|
|
proc addPeerNoWait*[A, B](pool: PeerPool[A, B],
|
2020-09-21 16:02:27 +00:00
|
|
|
peer: A, peerType: PeerType): PeerStatus =
|
2019-11-25 20:18:02 +00:00
|
|
|
## Add peer ``peer`` of type ``peerType`` to PeerPool ``pool``.
|
|
|
|
##
|
2020-09-21 16:02:27 +00:00
|
|
|
## Procedure returns ``PeerStatus``
|
|
|
|
## * if ``peer`` is already closed - (PeerStatus.DeadPeerError)
|
|
|
|
## * if ``pool`` already has peer ``peer`` - (PeerStatus.DuplicateError)
|
2020-01-21 18:30:21 +00:00
|
|
|
## * if ``pool`` currently has a maximum of peers.
|
2020-09-21 16:02:27 +00:00
|
|
|
## (PeerStatus.NoSpaceError)
|
2020-01-21 18:30:21 +00:00
|
|
|
## * if ``pool`` currently has a maximum of `Incoming` or `Outgoing` peers.
|
2020-09-21 16:02:27 +00:00
|
|
|
## (PeerStatus.NoSpaceError)
|
2020-01-21 18:30:21 +00:00
|
|
|
##
|
2020-09-21 16:02:27 +00:00
|
|
|
## Procedure returns (PeerStatus.Success) on success.
|
2019-11-25 20:18:02 +00:00
|
|
|
mixin getKey, getFuture
|
2020-09-21 16:02:27 +00:00
|
|
|
let res = pool.checkPeer(peer)
|
|
|
|
if res != PeerStatus.Success:
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
let peerKey = peer.getKey()
|
|
|
|
case peerType:
|
|
|
|
of PeerType.Incoming:
|
|
|
|
if pool.lenSpace({PeerType.Incoming}) > 0:
|
|
|
|
pool.addPeerImpl(peer, peerKey, peerType)
|
|
|
|
PeerStatus.Success
|
|
|
|
else:
|
|
|
|
PeerStatus.NoSpaceError
|
|
|
|
of PeerType.Outgoing:
|
|
|
|
if pool.lenSpace({PeerType.Outgoing}) > 0:
|
|
|
|
pool.addPeerImpl(peer, peerKey, peerType)
|
|
|
|
PeerStatus.Success
|
|
|
|
else:
|
|
|
|
PeerStatus.NoSpaceError
|
|
|
|
|
|
|
|
proc getPeerSpaceMask[A, B](pool: PeerPool[A, B],
|
|
|
|
peerType: PeerType): set[PeerType] {.inline.} =
|
|
|
|
## This procedure returns set of events which you need to wait to get empty
|
|
|
|
## space for peer type ``peerType``. This set can be used for call to
|
|
|
|
## ``waitNotFullEvent()``.
|
|
|
|
case peerType:
|
|
|
|
of PeerType.Incoming:
|
|
|
|
if pool.maxIncPeersCount >= pool.maxPeersCount:
|
|
|
|
# If maximum number of `incoming` peers is only limited by
|
|
|
|
# maximum number of peers, then we could wait for both events.
|
|
|
|
# It means that we do not care about what peer will left pool.
|
|
|
|
{PeerType.Incoming, PeerType.Outgoing}
|
|
|
|
else:
|
|
|
|
# Otherwise we could wait only for `incoming` event
|
|
|
|
{PeerType.Incoming}
|
|
|
|
of PeerType.Outgoing:
|
|
|
|
if pool.maxOutPeersCount >= pool.maxPeersCount:
|
|
|
|
# If maximum number of `outgoing` peers is only limited by
|
|
|
|
# maximum number of peers, then we could wait for both events.
|
|
|
|
# It means that we do not care about what peer will left pool.
|
|
|
|
{PeerType.Incoming, PeerType.Outgoing}
|
|
|
|
else:
|
|
|
|
# Otherwise we could wait only for `outgoing` event
|
|
|
|
{PeerType.Outgoing}
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-09-21 16:02:27 +00:00
|
|
|
proc waitForEmptySpace*[A, B](pool: PeerPool[A, B],
|
|
|
|
peerType: PeerType) {.async.} =
|
|
|
|
## This procedure will block until ``pool`` will have an empty space for peer
|
|
|
|
## of type ``peerType``.
|
|
|
|
let mask = pool.getPeerSpaceMask(peerType)
|
|
|
|
while pool.lenSpace({peerType}) == 0:
|
|
|
|
await pool.waitNotFullEvent(mask)
|
2020-01-24 01:35:38 +00:00
|
|
|
|
|
|
|
proc addPeer*[A, B](pool: PeerPool[A, B],
|
2020-09-21 16:02:27 +00:00
|
|
|
peer: A, peerType: PeerType): Future[PeerStatus] {.async.} =
|
2020-01-24 01:35:38 +00:00
|
|
|
## Add peer ``peer`` of type ``peerType`` to PeerPool ``pool``.
|
|
|
|
##
|
|
|
|
## This procedure will wait for an empty space in PeerPool ``pool``, if
|
|
|
|
## PeerPool ``pool`` is full.
|
|
|
|
##
|
2020-09-21 16:02:27 +00:00
|
|
|
## Procedure returns ``PeerStatus``
|
|
|
|
## * if ``peer`` is already closed - (PeerStatus.DeadPeerError)
|
|
|
|
## * if ``pool`` already has peer ``peer`` - (PeerStatus.DuplicateError)
|
2020-01-24 01:35:38 +00:00
|
|
|
##
|
2020-09-21 16:02:27 +00:00
|
|
|
## Procedure returns (PeerStatus.Success) on success.
|
|
|
|
mixin getKey
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
let res1 = pool.checkPeer(peer)
|
|
|
|
if res1 != PeerStatus.Success:
|
|
|
|
res1
|
|
|
|
else:
|
|
|
|
let mask = pool.getPeerSpaceMask(peerType)
|
|
|
|
# We going to block here until ``pool`` will not have free space,
|
|
|
|
# for our type of peer.
|
|
|
|
while pool.lenSpace({peerType}) == 0:
|
|
|
|
await pool.waitNotFullEvent(mask)
|
|
|
|
# Because we could wait for a long time we need to check peer one more
|
|
|
|
# time to avoid race condition.
|
|
|
|
let res2 = pool.checkPeer(peer)
|
|
|
|
if res2 == PeerStatus.Success:
|
|
|
|
let peerKey = peer.getKey()
|
|
|
|
pool.addPeerImpl(peer, peerKey, peerType)
|
|
|
|
PeerStatus.Success
|
|
|
|
else:
|
|
|
|
res2
|
|
|
|
return res
|
2020-09-16 10:00:11 +00:00
|
|
|
|
|
|
|
proc acquireItemImpl[A, B](pool: PeerPool[A, B],
|
|
|
|
filter: set[PeerType]): A {.inline.} =
|
|
|
|
doAssert((len(pool.outQueue) > 0) or (len(pool.incQueue) > 0))
|
|
|
|
let pindex =
|
|
|
|
if filter == {PeerType.Incoming, PeerType.Outgoing}:
|
|
|
|
if len(pool.outQueue) > 0 and len(pool.incQueue) > 0:
|
|
|
|
# Don't think `<` is actually `<` here.
|
|
|
|
if pool.incQueue[0] < pool.outQueue[0]:
|
|
|
|
inc(pool.acqIncPeersCount)
|
|
|
|
let item = pool.incQueue.pop()
|
|
|
|
item.data
|
|
|
|
else:
|
|
|
|
inc(pool.acqOutPeersCount)
|
|
|
|
let item = pool.outQueue.pop()
|
|
|
|
item.data
|
|
|
|
else:
|
|
|
|
if len(pool.outQueue) > 0:
|
|
|
|
inc(pool.acqOutPeersCount)
|
|
|
|
let item = pool.outQueue.pop()
|
|
|
|
item.data
|
|
|
|
else:
|
|
|
|
inc(pool.acqIncPeersCount)
|
|
|
|
let item = pool.incQueue.pop()
|
|
|
|
item.data
|
|
|
|
else:
|
|
|
|
if PeerType.Outgoing in filter:
|
|
|
|
inc(pool.acqOutPeersCount)
|
|
|
|
let item = pool.outQueue.pop()
|
|
|
|
item.data
|
|
|
|
else:
|
|
|
|
inc(pool.acqIncPeersCount)
|
|
|
|
let item = pool.incQueue.pop()
|
|
|
|
item.data
|
|
|
|
var pitem = addr(pool.storage[pindex])
|
|
|
|
doAssert(PeerFlags.Acquired notin pitem[].flags)
|
|
|
|
pitem[].flags.incl(PeerFlags.Acquired)
|
|
|
|
pitem[].data
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc acquire*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): Future[A] {.async.} =
|
|
|
|
## Acquire peer from PeerPool ``pool``, which match the filter ``filter``.
|
2020-09-16 10:00:11 +00:00
|
|
|
mixin getKey
|
2019-11-25 20:18:02 +00:00
|
|
|
doAssert(filter != {}, "Filter must not be empty")
|
|
|
|
while true:
|
2020-09-16 10:00:11 +00:00
|
|
|
if pool.lenAvailable(filter) == 0:
|
2020-01-24 01:35:38 +00:00
|
|
|
await pool.waitNotEmptyEvent(filter)
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
2020-09-16 10:00:11 +00:00
|
|
|
return pool.acquireItemImpl(filter)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
proc acquireNoWait*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
2022-05-31 10:45:37 +00:00
|
|
|
PeerType.Outgoing}
|
2023-08-25 09:29:07 +00:00
|
|
|
): A {.raises: [PeerPoolError].} =
|
2020-01-21 18:30:21 +00:00
|
|
|
doAssert(filter != {}, "Filter must not be empty")
|
2020-09-16 10:00:11 +00:00
|
|
|
if pool.lenAvailable(filter) < 1:
|
2020-01-21 18:30:21 +00:00
|
|
|
raise newException(PeerPoolError, "Not enough peers in pool")
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.acquireItemImpl(filter)
|
2020-01-21 18:30:21 +00:00
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
proc release*[A, B](pool: PeerPool[A, B], peer: A) =
|
|
|
|
## Release peer ``peer`` back to PeerPool ``pool``
|
|
|
|
mixin getKey
|
2020-09-16 10:00:11 +00:00
|
|
|
let key = getKey(peer)
|
2019-11-25 20:18:02 +00:00
|
|
|
var titem = pool.registry.getOrDefault(key, PeerIndex(data: -1))
|
|
|
|
if titem.data >= 0:
|
|
|
|
let pindex = titem.data
|
|
|
|
var item = addr(pool.storage[pindex])
|
|
|
|
if PeerFlags.Acquired in item[].flags:
|
2020-04-23 15:31:00 +00:00
|
|
|
if not(pool.checkPeerScore(peer)):
|
|
|
|
item[].flags.incl(DeleteOnRelease)
|
2019-11-25 20:18:02 +00:00
|
|
|
if PeerFlags.DeleteOnRelease in item[].flags:
|
2020-09-16 10:00:11 +00:00
|
|
|
# We do not care about result here because peer is present in registry
|
|
|
|
# and has all proper flags set.
|
|
|
|
discard pool.deletePeer(peer, force = true)
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
2020-09-16 10:00:11 +00:00
|
|
|
item[].flags.excl(PeerFlags.Acquired)
|
2020-09-21 16:02:27 +00:00
|
|
|
case item[].peerType
|
|
|
|
of PeerType.Incoming:
|
2019-11-25 20:18:02 +00:00
|
|
|
pool.incQueue.push(titem)
|
|
|
|
dec(pool.acqIncPeersCount)
|
2020-09-21 16:02:27 +00:00
|
|
|
of PeerType.Outgoing:
|
2019-11-25 20:18:02 +00:00
|
|
|
pool.outQueue.push(titem)
|
|
|
|
dec(pool.acqOutPeersCount)
|
2020-01-24 01:35:38 +00:00
|
|
|
pool.fireNotEmptyEvent(item[])
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
proc release*[A, B](pool: PeerPool[A, B], peers: openArray[A]) {.inline.} =
|
2019-11-25 20:18:02 +00:00
|
|
|
## Release array of peers ``peers`` back to PeerPool ``pool``.
|
|
|
|
for item in peers:
|
|
|
|
pool.release(item)
|
|
|
|
|
|
|
|
proc acquire*[A, B](pool: PeerPool[A, B],
|
|
|
|
number: int,
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): Future[seq[A]] {.async.} =
|
|
|
|
## Acquire ``number`` number of peers from PeerPool ``pool``, which match the
|
|
|
|
## filter ``filter``.
|
|
|
|
doAssert(filter != {}, "Filter must not be empty")
|
|
|
|
var peers = newSeq[A]()
|
|
|
|
try:
|
|
|
|
if number > 0:
|
|
|
|
while true:
|
|
|
|
if len(peers) >= number:
|
|
|
|
break
|
2020-09-16 10:00:11 +00:00
|
|
|
if pool.lenAvailable(filter) == 0:
|
2020-01-24 01:35:38 +00:00
|
|
|
await pool.waitNotEmptyEvent(filter)
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
2020-09-16 10:00:11 +00:00
|
|
|
peers.add(pool.acquireItemImpl(filter))
|
2020-08-06 18:47:39 +00:00
|
|
|
except CancelledError as exc:
|
2019-11-25 20:18:02 +00:00
|
|
|
# If we got cancelled, we need to return all the acquired peers back to
|
|
|
|
# pool.
|
|
|
|
for item in peers:
|
|
|
|
pool.release(item)
|
|
|
|
peers.setLen(0)
|
2020-08-06 18:47:39 +00:00
|
|
|
raise exc
|
2020-09-16 10:00:11 +00:00
|
|
|
return peers
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2020-01-21 18:30:21 +00:00
|
|
|
proc acquireNoWait*[A, B](pool: PeerPool[A, B],
|
|
|
|
number: int,
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): seq[A] =
|
|
|
|
## Acquire ``number`` number of peers from PeerPool ``pool``, which match the
|
|
|
|
## filter ``filter``.
|
|
|
|
doAssert(filter != {}, "Filter must not be empty")
|
|
|
|
var peers = newSeq[A]()
|
2020-09-16 10:00:11 +00:00
|
|
|
if pool.lenAvailable(filter) < number:
|
2020-01-21 18:30:21 +00:00
|
|
|
raise newException(PeerPoolError, "Not enough peers in pool")
|
|
|
|
for i in 0 ..< number:
|
2020-09-16 10:00:11 +00:00
|
|
|
peers.add(pool.acquireItemImpl(filter))
|
|
|
|
return peers
|
2020-01-21 18:30:21 +00:00
|
|
|
|
2019-11-25 20:18:02 +00:00
|
|
|
proc acquireIncomingPeer*[A, B](pool: PeerPool[A, B]): Future[A] {.inline.} =
|
|
|
|
## Acquire single incoming peer from PeerPool ``pool``.
|
|
|
|
pool.acquire({PeerType.Incoming})
|
|
|
|
|
|
|
|
proc acquireOutgoingPeer*[A, B](pool: PeerPool[A, B]): Future[A] {.inline.} =
|
|
|
|
## Acquire single outgoing peer from PeerPool ``pool``.
|
|
|
|
pool.acquire({PeerType.Outgoing})
|
|
|
|
|
|
|
|
proc acquireIncomingPeers*[A, B](pool: PeerPool[A, B],
|
|
|
|
number: int): Future[seq[A]] {.inline.} =
|
|
|
|
## Acquire ``number`` number of incoming peers from PeerPool ``pool``.
|
|
|
|
pool.acquire(number, {PeerType.Incoming})
|
|
|
|
|
|
|
|
proc acquireOutgoingPeers*[A, B](pool: PeerPool[A, B],
|
|
|
|
number: int): Future[seq[A]] {.inline.} =
|
|
|
|
## Acquire ``number`` number of outgoing peers from PeerPool ``pool``.
|
|
|
|
pool.acquire(number, {PeerType.Outgoing})
|
|
|
|
|
|
|
|
iterator peers*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): A =
|
|
|
|
## Iterate over sorted list of peers.
|
|
|
|
##
|
|
|
|
## All peers will be sorted by equation `>`(Peer1, Peer2), so biggest values
|
|
|
|
## will be first.
|
|
|
|
var sorted = initHeapQueue[PeerIndex]()
|
|
|
|
for i in 0 ..< len(pool.storage):
|
|
|
|
if pool.storage[i].peerType in filter:
|
|
|
|
sorted.push(PeerIndex(data: i, cmp: pool.cmp))
|
|
|
|
while len(sorted) > 0:
|
|
|
|
let pindex = sorted.pop().data
|
|
|
|
yield pool.storage[pindex].data
|
|
|
|
|
|
|
|
iterator availablePeers*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): A =
|
|
|
|
## Iterate over sorted list of available peers.
|
|
|
|
##
|
|
|
|
## All peers will be sorted by equation `>`(Peer1, Peer2), so biggest values
|
|
|
|
## will be first.
|
|
|
|
var sorted = initHeapQueue[PeerIndex]()
|
|
|
|
for i in 0 ..< len(pool.storage):
|
|
|
|
if (PeerFlags.Acquired notin pool.storage[i].flags) and
|
|
|
|
(pool.storage[i].peerType in filter):
|
|
|
|
sorted.push(PeerIndex(data: i, cmp: pool.cmp))
|
|
|
|
while len(sorted) > 0:
|
|
|
|
let pindex = sorted.pop().data
|
|
|
|
yield pool.storage[pindex].data
|
|
|
|
|
|
|
|
iterator acquiredPeers*[A, B](pool: PeerPool[A, B],
|
|
|
|
filter = {PeerType.Incoming,
|
|
|
|
PeerType.Outgoing}): A =
|
|
|
|
## Iterate over sorted list of acquired (non-available) peers.
|
|
|
|
##
|
|
|
|
## All peers will be sorted by equation `>`(Peer1, Peer2), so biggest values
|
|
|
|
## will be first.
|
|
|
|
var sorted = initHeapQueue[PeerIndex]()
|
|
|
|
for i in 0 ..< len(pool.storage):
|
|
|
|
if (PeerFlags.Acquired in pool.storage[i].flags) and
|
|
|
|
(pool.storage[i].peerType in filter):
|
|
|
|
sorted.push(PeerIndex(data: i, cmp: pool.cmp))
|
|
|
|
while len(sorted) > 0:
|
|
|
|
let pindex = sorted.pop().data
|
|
|
|
yield pool.storage[pindex].data
|
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
proc `[]`*[A, B](pool: PeerPool[A, B], key: B): A {.inline, raises: [KeyError].} =
|
2019-11-25 20:18:02 +00:00
|
|
|
## Retrieve peer with key ``key`` from PeerPool ``pool``.
|
|
|
|
let pindex = pool.registry[key]
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.storage[pindex.data]
|
2019-11-25 20:18:02 +00:00
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
proc `[]`*[A, B](pool: var PeerPool[A, B], key: B): var A {.inline, raises: [KeyError].} =
|
2019-11-25 20:18:02 +00:00
|
|
|
## Retrieve peer with key ``key`` from PeerPool ``pool``.
|
|
|
|
let pindex = pool.registry[key]
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.storage[pindex.data].data
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc hasPeer*[A, B](pool: PeerPool[A, B], key: B): bool {.inline.} =
|
|
|
|
## Returns ``true`` if peer with ``key`` present in PeerPool ``pool``.
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.registry.hasKey(key)
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc getOrDefault*[A, B](pool: PeerPool[A, B], key: B): A {.inline.} =
|
|
|
|
## Retrieves the peer from PeerPool ``pool`` using key ``key``. If peer is
|
|
|
|
## not present, default initialization value for type ``A`` is returned
|
|
|
|
## (e.g. 0 for any integer type).
|
|
|
|
let pindex = pool.registry.getOrDefault(key, PeerIndex(data: -1))
|
|
|
|
if pindex.data >= 0:
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.storage[pindex.data].data
|
|
|
|
else:
|
|
|
|
A()
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc getOrDefault*[A, B](pool: PeerPool[A, B], key: B,
|
|
|
|
default: A): A {.inline.} =
|
|
|
|
## Retrieves the peer from PeerPool ``pool`` using key ``key``. If peer is
|
|
|
|
## not present, default value ``default`` is returned.
|
|
|
|
let pindex = pool.registry.getOrDefault(key, PeerIndex(data: -1))
|
|
|
|
if pindex.data >= 0:
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.storage[pindex.data].data
|
2019-11-25 20:18:02 +00:00
|
|
|
else:
|
2020-09-16 10:00:11 +00:00
|
|
|
default
|
2019-11-25 20:18:02 +00:00
|
|
|
|
|
|
|
proc clear*[A, B](pool: PeerPool[A, B]) =
|
|
|
|
## Performs PeerPool's ``pool`` storage and counters reset.
|
|
|
|
pool.incQueue.clear()
|
|
|
|
pool.outQueue.clear()
|
|
|
|
pool.registry.clear()
|
|
|
|
for i in 0 ..< len(pool.storage):
|
|
|
|
pool.storage[i] = PeerItem[A]()
|
|
|
|
pool.storage.setLen(0)
|
|
|
|
pool.curIncPeersCount = 0
|
|
|
|
pool.curOutPeersCount = 0
|
|
|
|
pool.acqIncPeersCount = 0
|
|
|
|
pool.acqOutPeersCount = 0
|
|
|
|
|
|
|
|
proc clearSafe*[A, B](pool: PeerPool[A, B]) {.async.} =
|
|
|
|
## Performs "safe" clear. Safe means that it first acquires all the peers
|
|
|
|
## in PeerPool, and only after that it will reset storage.
|
|
|
|
var acquired = newSeq[A]()
|
|
|
|
while len(pool.registry) > len(acquired):
|
|
|
|
var peers = await pool.acquire(len(pool.registry) - len(acquired))
|
|
|
|
for item in peers:
|
|
|
|
acquired.add(item)
|
|
|
|
pool.clear()
|
2020-04-23 15:31:00 +00:00
|
|
|
|
|
|
|
proc setScoreCheck*[A, B](pool: PeerPool[A, B],
|
|
|
|
scoreCheckCb: PeerScoreCheckCallback[A]) =
|
2020-10-27 09:25:28 +00:00
|
|
|
## Sets ScoreCheck callback.
|
2020-04-23 15:31:00 +00:00
|
|
|
pool.scoreCheck = scoreCheckCb
|
2020-09-16 10:00:11 +00:00
|
|
|
|
2020-10-27 09:25:28 +00:00
|
|
|
proc setOnDeletePeer*[A, B](pool: PeerPool[A, B],
|
|
|
|
deletePeerCb: PeerOnDeleteCallback[A]) =
|
|
|
|
## Sets DeletePeer callback.
|
|
|
|
pool.onDeletePeer = deletePeerCb
|
|
|
|
|
2020-09-16 10:00:11 +00:00
|
|
|
proc setPeerCounter*[A, B](pool: PeerPool[A, B],
|
|
|
|
peerCounterCb: PeerCounterCallback) =
|
2020-10-27 09:25:28 +00:00
|
|
|
## Sets PeerCounter callback.
|
2020-09-16 10:00:11 +00:00
|
|
|
pool.peerCounter = peerCounterCb
|