Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reestablish peer connection on silent stream destruction #288

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions packages/status-js/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Comment on lines +21 to +33
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cc @D4nte

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this would help? libp2p/js-libp2p#1221

Let's see once I finish the upgrade. We can also do a PR upstream if we know how to fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this would help? libp2p/js-libp2p#1221

By the sound of it, I'm afraid not. In that case, it seams like when a connection "ends" the stream doesn't. While in our case, when a stream, or even a socket, ends it is the connection that doesn't end nor recreates it.

The chain of events is backwards, I believe.

Let's see once I finish the upgrade.

[– https://github.com/waku-org/js-waku/pull/803]

I agree. Great job on that one, looking forward 💪.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pulled into xmtp/xmtp-js#128.


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)
Expand All @@ -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()
Expand All @@ -55,6 +87,7 @@ class Client {
}

public async stop() {
clearInterval(this.#wakuDisconnectionTimer)
await this.waku.stop()
}

Expand Down