From 225537fc3902824f11e6259328abf21170ce3b64 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 17 Dec 2022 09:05:48 -0500 Subject: [PATCH] *Refactor so every call to addPeerHandler, we only add if it's not there, vs aborting completely if in connectedTo. This will ensure we stay alive via heartbeats even in edge cases --- src/index.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5fc7aed..4da6cd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -178,25 +178,26 @@ export default class DHTCache extends EventEmitter { protected addPeerHandler(peer: any) { const id = peer.remotePublicKey; const stringId = id.toString("hex"); - if (this.connectedTo.has(stringId)) { - return; - } // Already know we're connected here + if (!this.connectedTo.has(stringId)) { + this.connectedTo.add(stringId); + } - this.connectedTo.add(stringId); - this._ensurePeer(id); - this._addEntityConnection(this.id, id); - this.emit("peer-add", id); + if (!this._hasSeenEntity(id)) { + this._ensurePeer(id); + this._addEntityConnection(this.id, id); + this.emit("peer-add", id); - this._recalculate(); + this._recalculate(); - this._broadcastMessage({ - type: Type.CONNECTED, - id, - }); + this._broadcastMessage({ + type: Type.CONNECTED, + id, + }); - this._emitHeartbeat(peer); + this._emitHeartbeat(peer); - this.log.debug(`Relay peer connected: ${stringId}`); + this.log.debug(`Relay peer connected: ${stringId}`); + } if (this.bootstrapped) { return;