Rename `keepAlive` option to `pingKeepAlive`

In preparation for introducing a relay keep alive feature.
This commit is contained in:
Franck Royer 2021-07-22 16:34:27 +10:00
parent 664e5f56a1
commit ea33b9cd8a
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 18 additions and 16 deletions

View File

@ -35,6 +35,7 @@
"ihave", "ihave",
"ihaves", "ihaves",
"ineed", "ineed",
"ipfs",
"iwant", "iwant",
"jdev", "jdev",
"keccak", "keccak",

View File

@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- **Breaking**: Renamed `WakuRelay.(add|delete)PrivateDecryptionKey` to `WakuRelay.(add|delete)DecryptionKey` to make it clearer that it accepts both symmetric keys and asymmetric private keys. - **Breaking**: Renamed `WakuRelay.(add|delete)PrivateDecryptionKey` to `WakuRelay.(add|delete)DecryptionKey` to make it clearer that it accepts both symmetric keys and asymmetric private keys.
- Upgrade libp2p to 0.32.0. - Upgrade libp2p to 0.32.0.
- **Breaking**: Rename `keepAlive` option to `pingKeepAlive`.
### Fixed ### Fixed
- Align `WakuMessage` readme example with actual code behaviour. - Align `WakuMessage` readme example with actual code behaviour.

View File

@ -37,12 +37,12 @@ export interface CreateOptions {
*/ */
pubsubTopic?: string; pubsubTopic?: string;
/** /**
* Set keep alive frequency in seconds: Waku will send a ping request to each peer * Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
* after the set number of seconds. Set to 0 to disable the keep alive feature * request to each peer after the set number of seconds. Set to 0 to disable.
* *
* @default 0 * @default 0
*/ */
keepAlive?: number; pingKeepAlive?: number;
/** /**
* You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property. * You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property.
* This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) * This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
@ -67,7 +67,7 @@ export class Waku {
public store: WakuStore; public store: WakuStore;
public lightPush: WakuLightPush; public lightPush: WakuLightPush;
private keepAliveTimers: { private pingKeepAliveTimers: {
[peer: string]: ReturnType<typeof setInterval>; [peer: string]: ReturnType<typeof setInterval>;
}; };
@ -81,19 +81,19 @@ export class Waku {
this.relay = libp2p.pubsub as unknown as WakuRelay; this.relay = libp2p.pubsub as unknown as WakuRelay;
this.store = store; this.store = store;
this.lightPush = lightPush; this.lightPush = lightPush;
this.keepAliveTimers = {}; this.pingKeepAliveTimers = {};
const keepAlive = options.keepAlive || 0; const pingKeepAlive = options.pingKeepAlive || 0;
if (keepAlive !== 0) { if (pingKeepAlive !== 0) {
libp2p.connectionManager.on('peer:connect', (connection: Connection) => { libp2p.connectionManager.on('peer:connect', (connection: Connection) => {
this.startKeepAlive(connection.remotePeer, keepAlive); this.startPingKeepAlive(connection.remotePeer, pingKeepAlive);
}); });
libp2p.connectionManager.on( libp2p.connectionManager.on(
'peer:disconnect', 'peer:disconnect',
(connection: Connection) => { (connection: Connection) => {
this.stopKeepAlive(connection.remotePeer); this.stopPingKeepAlive(connection.remotePeer);
} }
); );
} }
@ -214,21 +214,21 @@ export class Waku {
return localMultiaddr + '/p2p/' + this.libp2p.peerId.toB58String(); return localMultiaddr + '/p2p/' + this.libp2p.peerId.toB58String();
} }
private startKeepAlive(peerId: PeerId, periodSecs: number): void { private startPingKeepAlive(peerId: PeerId, periodSecs: number): void {
// Just in case a timer already exist for this peer // Just in case a timer already exist for this peer
this.stopKeepAlive(peerId); this.stopPingKeepAlive(peerId);
const peerIdStr = peerId.toB58String(); const peerIdStr = peerId.toB58String();
this.keepAliveTimers[peerIdStr] = setInterval(() => { this.pingKeepAliveTimers[peerIdStr] = setInterval(() => {
Ping(this.libp2p, peerId); Ping(this.libp2p, peerId);
}, periodSecs * 1000); }, periodSecs * 1000);
} }
private stopKeepAlive(peerId: PeerId): void { private stopPingKeepAlive(peerId: PeerId): void {
const peerIdStr = peerId.toB58String(); const peerIdStr = peerId.toB58String();
if (this.keepAliveTimers[peerIdStr]) { if (this.pingKeepAliveTimers[peerIdStr]) {
clearInterval(this.keepAliveTimers[peerIdStr]); clearInterval(this.pingKeepAliveTimers[peerIdStr]);
delete this.keepAliveTimers[peerIdStr]; delete this.pingKeepAliveTimers[peerIdStr];
} }
} }
} }