Add active waitForMesh proc.

This commit is contained in:
Alejandro Cabeza Romero 2024-08-26 19:36:17 +02:00
parent e199782ca1
commit b32badfe9a
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
1 changed files with 22 additions and 0 deletions

View File

@ -147,6 +147,12 @@ proc subscribeRandom*(nodes: seq[PubSub]) {.async.} =
await dialer.switch.connect(node.peerInfo.peerId, node.peerInfo.addrs) await dialer.switch.connect(node.peerInfo.peerId, node.peerInfo.addrs)
dialed.add(node.peerInfo.peerId) dialed.add(node.peerInfo.peerId)
proc activeWait(
interval: Duration, maximum: Moment, timeoutErrorMessage = "Timeout on activeWait"
) {.async.} =
await sleepAsync(interval)
doAssert Moment.now() < maximum, timeoutErrorMessage
proc waitSub*(sender, receiver: auto, key: string) {.async.} = proc waitSub*(sender, receiver: auto, key: string) {.async.} =
if sender == receiver: if sender == receiver:
return return
@ -201,3 +207,19 @@ proc waitSubGraph*(nodes: seq[PubSub], key: string) {.async.} =
await sleepAsync(5.milliseconds) await sleepAsync(5.milliseconds)
doAssert Moment.now() < timeout, "waitSubGraph timeout!" doAssert Moment.now() < timeout, "waitSubGraph timeout!"
proc waitForMesh*(
sender: auto, receiver: auto, key: string, timeoutDuration = 5.seconds
) {.async.} =
if sender == receiver:
return
let
timeoutMoment = Moment.now() + timeoutDuration
gossipsubSender = GossipSub(sender)
receiverPeerId = receiver.peerInfo.peerId
while not gossipsubSender.mesh.hasPeerId(key, receiverPeerId):
trace "waitForMesh sleeping..."
await activeWait(5.milliseconds, timeoutMoment, "waitForMesh timeout!")
echo "waitForMesh done!"