Reestablish peer connection on silent stream destruction (#288)

This commit is contained in:
Felicio Mununga 2022-06-30 15:12:26 +02:00 committed by GitHub
parent 2f3ac73e5e
commit 1f19d61193
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
1 changed files with 35 additions and 2 deletions

View File

@ -18,14 +18,32 @@ export interface ClientOptions {
class Client {
public waku: Waku
public readonly wakuMessages: Set<string>
/**
* Tracks open connections which had their streams silently destroyed
* and closes them so new connections can be automatically created.
*
* For example, in case of a websocket in browser which did not
* have its close event emitted.
*
* Note: Detection of the stream removal depends on active (by user)
* or pasive (with `Waku.relayKeepAlive`) message sending.
*
* Note: This is only a workaround (@see https://github.com/libp2p/js-libp2p/issues/939).
*/
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
public account?: Account
public community: Community
constructor(waku: Waku, options: ClientOptions) {
constructor(
waku: Waku,
wakuDisconnectionTimer: ReturnType<typeof setInterval>,
options: ClientOptions
) {
// Waku
this.waku = waku
this.wakuMessages = new Set()
this.#wakuDisconnectionTimer = wakuDisconnectionTimer
// Community
this.community = new Community(this, options.publicKey)
@ -41,12 +59,26 @@ class Client {
// '/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
],
},
relayKeepAlive: 15,
libp2p: { config: { pubsub: { enabled: true, emitSelf: true } } },
})
await waku.waitForRemotePeer()
const wakuDisconnectionTimer = setInterval(async () => {
const connectionsToClose: Promise<void>[] = []
for (const connections of waku.libp2p.connectionManager.connections.values()) {
for (const connection of connections) {
if (!connection.streams.length) {
connectionsToClose.push(connection.close())
}
}
}
await Promise.allSettled(connectionsToClose)
}, 10 * 1000)
// Client
const client = new Client(waku, options)
const client = new Client(waku, wakuDisconnectionTimer, options)
// Community
await client.community.start()
@ -55,6 +87,7 @@ class Client {
}
public async stop() {
clearInterval(this.#wakuDisconnectionTimer)
await this.waku.stop()
}