From 29478e9a5a5e0e953e81d6cee77fe41b9ec2c530 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 5 Dec 2022 14:08:15 -0500 Subject: [PATCH] *Instead of disconnecting dead peers, just use an "online" property on the node graph, and filter out dead peers in _recalculate --- src/index.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2eec873..f580ea5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -422,6 +422,13 @@ export default class DHTCache extends EventEmitter { this.graph.removeNode(id); } + for (const id of online) { + const item = this.graph.node.get(id); + if (!item?.online) { + online.delete(id); + } + } + this._online = online; this.emit("online", online); @@ -467,14 +474,10 @@ export default class DHTCache extends EventEmitter { const heartbeat = this.graph.node.get(peer)?.heartbeat; const conn = this.swarm._allConnections.get(pubkey); - if (!conn) { - this.onRemovePeer({ remotePublicKey: pubkey }); - continue; - } + const online = + conn && heartbeat > 0 && Date.now() - heartbeat > 60 * 1000; - if (heartbeat > 0 && Date.now() - heartbeat > 60 * 1000) { - conn.end(); - } + this._setEntity(peer, { online }); } }