-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: wait for gossipsub heartbeat before sending message (#94)
Only wait for the sending peer to see the remote in their topic subscriber list but also wait a little bit longer for gossipsub to rebalance the mesh to increase the chances of the publisher actually sending a pubsub message to the subscriber.
- Loading branch information
1 parent
3b14ad0
commit dffdff0
Showing
5 changed files
with
23 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import pWaitFor from 'p-wait-for' | ||
import type { Daemon } from '..' | ||
import delay from 'delay' | ||
import type { Daemon } from '../index.js' | ||
|
||
export async function waitForBothSubscribed (topic: string, a: Daemon, b: Daemon): Promise<void> { | ||
await a.client.pubsub.subscribe(topic) | ||
await b.client.pubsub.subscribe(topic) | ||
|
||
const idA = await a.client.identify() | ||
/** | ||
* Wait for daemon a to see daemon b in it's subscriber list | ||
* for the passed topic | ||
*/ | ||
export async function waitForSubscribed (topic: string, a: Daemon, b: Daemon): Promise<void> { | ||
const idB = await b.client.identify() | ||
|
||
// wait for subscription stream | ||
await Promise.all([ | ||
await pWaitFor(async () => { | ||
const peers = await a.client.pubsub.getSubscribers(topic) | ||
return peers.map(p => p.toString()).includes(idB.peerId.toString()) | ||
}, { | ||
interval: 500 | ||
}), | ||
await pWaitFor(async () => { | ||
const peers = await b.client.pubsub.getSubscribers(topic) | ||
return peers.map(p => p.toString()).includes(idA.peerId.toString()) | ||
}, { | ||
interval: 500 | ||
}) | ||
]) | ||
await pWaitFor(async () => { | ||
const peers = await a.client.pubsub.getSubscribers(topic) | ||
return peers.map(p => p.toString()).includes(idB.peerId.toString()) | ||
}, { | ||
interval: 500 | ||
}) | ||
|
||
// wait for the gossipsub heartbeat to rebalance the mesh | ||
await delay(2000) | ||
} |