*Make heartbeat configurable with a default time of 15 seconds

This commit is contained in:
Derrick Hammer 2022-12-05 15:13:50 -05:00
parent 5b2d1a3c7c
commit 35c6a0e314
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 2 deletions

View File

@ -29,6 +29,7 @@ export default class DHTCache extends EventEmitter {
private bootstrapped: boolean; private bootstrapped: boolean;
private graph: any; private graph: any;
private connectedTo: Set<any>; private connectedTo: Set<any>;
private heartBeatInterval: number;
protected flood: DHTFlood; protected flood: DHTFlood;
@ -36,6 +37,7 @@ export default class DHTCache extends EventEmitter {
swarm: any, swarm: any,
{ {
id = swarm.keyPair.publicKey, id = swarm.keyPair.publicKey,
heartBeatInterval = 15,
...opts ...opts
}: { id?: Buffer; [key: string]: any } = {} }: { id?: Buffer; [key: string]: any } = {}
) { ) {
@ -46,6 +48,7 @@ export default class DHTCache extends EventEmitter {
this.bootstrapped = false; this.bootstrapped = false;
this.graph = new DiGraph(); this.graph = new DiGraph();
this.connectedTo = new Set(); this.connectedTo = new Set();
this.heartBeatInterval = heartBeatInterval;
this._cache = new Set(); this._cache = new Set();
this._online = new Set([this._maybeHexify(this.id)]); this._online = new Set([this._maybeHexify(this.id)]);
this.swarm = swarm; this.swarm = swarm;
@ -70,7 +73,7 @@ export default class DHTCache extends EventEmitter {
this._ensurePeer(this.id); this._ensurePeer(this.id);
setInterval(() => this._heartbeatCheck(), 5 * 1000); setInterval(() => this._heartbeatCheck(), 5 * 1000);
setInterval(() => this._emitHeartbeat(), 60 * 1000); setInterval(() => this._emitHeartbeat(), this.heartBeatInterval * 1000);
} }
private _cache: Set<string>; private _cache: Set<string>;
@ -482,7 +485,9 @@ export default class DHTCache extends EventEmitter {
const conn = this.swarm._allConnections.get(pubkey); const conn = this.swarm._allConnections.get(pubkey);
const online = const online =
conn && heartbeat > 0 && Date.now() - heartbeat <= 60 * 1000; conn &&
heartbeat > 0 &&
Date.now() - heartbeat <= this.heartBeatInterval * 1000;
if (node?.online !== online) { if (node?.online !== online) {
changed = true; changed = true;