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",
"ihaves",
"ineed",
"ipfs",
"iwant",
"jdev",
"keccak",

View File

@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 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.
- Upgrade libp2p to 0.32.0.
- **Breaking**: Rename `keepAlive` option to `pingKeepAlive`.
### Fixed
- Align `WakuMessage` readme example with actual code behaviour.

View File

@ -37,12 +37,12 @@ export interface CreateOptions {
*/
pubsubTopic?: string;
/**
* Set keep alive frequency in seconds: Waku will send a ping request to each peer
* after the set number of seconds. Set to 0 to disable the keep alive feature
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
* request to each peer after the set number of seconds. Set to 0 to disable.
*
* @default 0
*/
keepAlive?: number;
pingKeepAlive?: number;
/**
* 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)
@ -67,7 +67,7 @@ export class Waku {
public store: WakuStore;
public lightPush: WakuLightPush;
private keepAliveTimers: {
private pingKeepAliveTimers: {
[peer: string]: ReturnType<typeof setInterval>;
};
@ -81,19 +81,19 @@ export class Waku {
this.relay = libp2p.pubsub as unknown as WakuRelay;
this.store = store;
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) => {
this.startKeepAlive(connection.remotePeer, keepAlive);
this.startPingKeepAlive(connection.remotePeer, pingKeepAlive);
});
libp2p.connectionManager.on(
'peer:disconnect',
(connection: Connection) => {
this.stopKeepAlive(connection.remotePeer);
this.stopPingKeepAlive(connection.remotePeer);
}
);
}
@ -214,21 +214,21 @@ export class Waku {
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
this.stopKeepAlive(peerId);
this.stopPingKeepAlive(peerId);
const peerIdStr = peerId.toB58String();
this.keepAliveTimers[peerIdStr] = setInterval(() => {
this.pingKeepAliveTimers[peerIdStr] = setInterval(() => {
Ping(this.libp2p, peerId);
}, periodSecs * 1000);
}
private stopKeepAlive(peerId: PeerId): void {
private stopPingKeepAlive(peerId: PeerId): void {
const peerIdStr = peerId.toB58String();
if (this.keepAliveTimers[peerIdStr]) {
clearInterval(this.keepAliveTimers[peerIdStr]);
delete this.keepAliveTimers[peerIdStr];
if (this.pingKeepAliveTimers[peerIdStr]) {
clearInterval(this.pingKeepAliveTimers[peerIdStr]);
delete this.pingKeepAliveTimers[peerIdStr];
}
}
}