From 0ecd44aa0af47236e8cf9c4dd81b5ff59aac7adc Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 13:46:12 +0200 Subject: [PATCH 1/7] clear timers on `stop` --- src/lib/waku.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index bd82fe3e76..abe62816f0 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -98,7 +98,12 @@ export interface CreateOptions { decryptionKeys?: Array; } +// TODO: listen to and emit libp2p (error) events +// TODO?: do "started" check in all methods export class Waku { + private static started = false; + private static stopping = false; + public libp2p: Libp2p; public relay: WakuRelay; public store: WakuStore; @@ -134,6 +139,7 @@ export class Waku { }); libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { + // TODO: recconnect or stop; this event will only be triggered when the last connection is closed. this.stopKeepAlive(connection.remotePeer); }); @@ -141,11 +147,17 @@ export class Waku { this.addDecryptionKey(key); }); } - + // TODO?: rename to start or createAndStart /** * Create and start new waku node. */ static async create(options?: CreateOptions): Promise { + if (Waku.started) { + throw "Already started"; + } + + Waku.started = true; + // Get an object in case options or libp2p are undefined const libp2pOpts = Object.assign({}, options?.libp2p); @@ -281,7 +293,26 @@ export class Waku { } async stop(): Promise { - return this.libp2p.stop(); + if (Waku.stopping) { + // FIXME?: do not throw + throw "Already stopping"; + } + + Waku.stopping = true; + + for (const timer of [ + ...Object.values(this.pingKeepAliveTimers), + ...Object.values(this.relayKeepAliveTimers), + ]) { + clearInterval(timer); + } + this.pingKeepAliveTimers = {}; + this.relayKeepAliveTimers = {}; + + await this.libp2p.stop(); + + Waku.stopping = false; + Waku.started = false; } /** @@ -413,6 +444,7 @@ export class Waku { if (relayPeriodSecs !== 0) { this.relayKeepAliveTimers[peerIdStr] = setInterval(() => { + // TODO?: try..catch WakuMessage.fromBytes(new Uint8Array(), RelayPingContentTopic).then( (wakuMsg) => this.relay.send(wakuMsg) ); From 911ce4a8364f70342adb200a7c0298e8c0221628 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 14:50:30 +0200 Subject: [PATCH 2/7] fix typo --- src/lib/waku.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index abe62816f0..9aca8a60cf 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -139,7 +139,7 @@ export class Waku { }); libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { - // TODO: recconnect or stop; this event will only be triggered when the last connection is closed. + // TODO: reconnect or stop; this event will only be triggered when the last connection is closed. this.stopKeepAlive(connection.remotePeer); }); From 606a2da96c2c06afbecb692741c18c9718f2ebd7 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 15:01:36 +0200 Subject: [PATCH 3/7] remove static props used for instance state --- src/lib/waku.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 9aca8a60cf..7f3b473db8 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -101,9 +101,6 @@ export interface CreateOptions { // TODO: listen to and emit libp2p (error) events // TODO?: do "started" check in all methods export class Waku { - private static started = false; - private static stopping = false; - public libp2p: Libp2p; public relay: WakuRelay; public store: WakuStore; @@ -152,12 +149,6 @@ export class Waku { * Create and start new waku node. */ static async create(options?: CreateOptions): Promise { - if (Waku.started) { - throw "Already started"; - } - - Waku.started = true; - // Get an object in case options or libp2p are undefined const libp2pOpts = Object.assign({}, options?.libp2p); @@ -293,13 +284,6 @@ export class Waku { } async stop(): Promise { - if (Waku.stopping) { - // FIXME?: do not throw - throw "Already stopping"; - } - - Waku.stopping = true; - for (const timer of [ ...Object.values(this.pingKeepAliveTimers), ...Object.values(this.relayKeepAliveTimers), @@ -310,9 +294,6 @@ export class Waku { this.relayKeepAliveTimers = {}; await this.libp2p.stop(); - - Waku.stopping = false; - Waku.started = false; } /** From 3d67946fb0872d764264cf3ec81b3d3d68975443 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 17:39:51 +0200 Subject: [PATCH 4/7] move clearing to func --- src/lib/waku.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 7f3b473db8..bdd477190c 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -135,8 +135,18 @@ export class Waku { this.startKeepAlive(connection.remotePeer, pingKeepAlive, relayKeepAlive); }); + /** + * NOTE: Event is not being emitted on closing nor loosing a connection. + * @see https://github.com/libp2p/js-libp2p/issues/939 + * + * >This event will be triggered anytime we are disconnected from another peer, + * >regardless of the circumstances of that disconnection. + * >If we happen to have multiple connections to a peer, + * >this event will **only** be triggered when the last connection is closed. + * @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100 + */ libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { - // TODO: reconnect or stop; this event will only be triggered when the last connection is closed. + // TODO: (retry) reconnect or stop completely and emit own event this.stopKeepAlive(connection.remotePeer); }); @@ -284,15 +294,7 @@ export class Waku { } async stop(): Promise { - for (const timer of [ - ...Object.values(this.pingKeepAliveTimers), - ...Object.values(this.relayKeepAliveTimers), - ]) { - clearInterval(timer); - } - this.pingKeepAliveTimers = {}; - this.relayKeepAliveTimers = {}; - + this.stopAllKeepAlives(); await this.libp2p.stop(); } @@ -446,6 +448,18 @@ export class Waku { delete this.relayKeepAliveTimers[peerIdStr]; } } + + private stopAllKeepAlives(): void { + for (const timer of [ + ...Object.values(this.pingKeepAliveTimers), + ...Object.values(this.relayKeepAliveTimers), + ]) { + clearInterval(timer); + } + + this.pingKeepAliveTimers = {}; + this.relayKeepAliveTimers = {}; + } } const awaitTimeout = (ms: number, rejectReason: string): Promise => From 598a6a120eeb902f996a5881d305bf05ee3ef320 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 17:41:55 +0200 Subject: [PATCH 5/7] add note --- src/lib/waku.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index bdd477190c..df146afb80 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -138,6 +138,7 @@ export class Waku { /** * NOTE: Event is not being emitted on closing nor loosing a connection. * @see https://github.com/libp2p/js-libp2p/issues/939 + * @see https://github.com/status-im/js-waku/issues/252 * * >This event will be triggered anytime we are disconnected from another peer, * >regardless of the circumstances of that disconnection. From 59bb9257c696fc4b1616349ca5fa972445fb09d4 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 17:46:26 +0200 Subject: [PATCH 6/7] remove notes --- src/lib/waku.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index df146afb80..ef86785e6f 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -98,8 +98,6 @@ export interface CreateOptions { decryptionKeys?: Array; } -// TODO: listen to and emit libp2p (error) events -// TODO?: do "started" check in all methods export class Waku { public libp2p: Libp2p; public relay: WakuRelay; @@ -136,7 +134,7 @@ export class Waku { }); /** - * NOTE: Event is not being emitted on closing nor loosing a connection. + * NOTE: Event is not being emitted on closing nor losing a connection. * @see https://github.com/libp2p/js-libp2p/issues/939 * @see https://github.com/status-im/js-waku/issues/252 * @@ -147,7 +145,6 @@ export class Waku { * @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100 */ libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { - // TODO: (retry) reconnect or stop completely and emit own event this.stopKeepAlive(connection.remotePeer); }); @@ -155,7 +152,7 @@ export class Waku { this.addDecryptionKey(key); }); } - // TODO?: rename to start or createAndStart + /** * Create and start new waku node. */ @@ -428,7 +425,6 @@ export class Waku { if (relayPeriodSecs !== 0) { this.relayKeepAliveTimers[peerIdStr] = setInterval(() => { - // TODO?: try..catch WakuMessage.fromBytes(new Uint8Array(), RelayPingContentTopic).then( (wakuMsg) => this.relay.send(wakuMsg) ); From 793699ac738de0907aa06ebbea58dc3c250390b5 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Sun, 29 May 2022 18:32:18 +0200 Subject: [PATCH 7/7] update .cspell.json --- .cspell.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.cspell.json b/.cspell.json index 10a73c7821..ad88a31fff 100644 --- a/.cspell.json +++ b/.cspell.json @@ -100,7 +100,8 @@ "webfonts", "websockets", "wifi", - "xsalsa20" + "xsalsa20", + "Alives" ], "flagWords": [], "ignorePaths": [