Reestablish peer connection on silent stream destruction (#288)
This commit is contained in:
parent
2f3ac73e5e
commit
1f19d61193
|
@ -18,14 +18,32 @@ export interface ClientOptions {
|
||||||
class Client {
|
class Client {
|
||||||
public waku: Waku
|
public waku: Waku
|
||||||
public readonly wakuMessages: Set<string>
|
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 account?: Account
|
||||||
public community: Community
|
public community: Community
|
||||||
|
|
||||||
constructor(waku: Waku, options: ClientOptions) {
|
constructor(
|
||||||
|
waku: Waku,
|
||||||
|
wakuDisconnectionTimer: ReturnType<typeof setInterval>,
|
||||||
|
options: ClientOptions
|
||||||
|
) {
|
||||||
// Waku
|
// Waku
|
||||||
this.waku = waku
|
this.waku = waku
|
||||||
this.wakuMessages = new Set()
|
this.wakuMessages = new Set()
|
||||||
|
this.#wakuDisconnectionTimer = wakuDisconnectionTimer
|
||||||
|
|
||||||
// Community
|
// Community
|
||||||
this.community = new Community(this, options.publicKey)
|
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',
|
// '/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
relayKeepAlive: 15,
|
||||||
libp2p: { config: { pubsub: { enabled: true, emitSelf: true } } },
|
libp2p: { config: { pubsub: { enabled: true, emitSelf: true } } },
|
||||||
})
|
})
|
||||||
await waku.waitForRemotePeer()
|
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
|
// Client
|
||||||
const client = new Client(waku, options)
|
const client = new Client(waku, wakuDisconnectionTimer, options)
|
||||||
|
|
||||||
// Community
|
// Community
|
||||||
await client.community.start()
|
await client.community.start()
|
||||||
|
@ -55,6 +87,7 @@ class Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async stop() {
|
public async stop() {
|
||||||
|
clearInterval(this.#wakuDisconnectionTimer)
|
||||||
await this.waku.stop()
|
await this.waku.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue