From 1781fc7a1ecd7c8ce91238cfce51b8a961fd1934 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 1 Nov 2023 13:48:01 +0100 Subject: [PATCH 1/3] dont consider peers stable if they havent been connected for less than MIN_CONNECTION_TIME --- lib/peer-info.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/peer-info.js b/lib/peer-info.js index 2afa735..7ec2a94 100644 --- a/lib/peer-info.js +++ b/lib/peer-info.js @@ -1,6 +1,8 @@ const { EventEmitter } = require('events') const b4a = require('b4a') +const MIN_CONNECTION_TIME = 15000 + const VERY_LOW_PRIORITY = 0 const LOW_PRIORITY = 1 const NORMAL_PRIORITY = 2 @@ -16,6 +18,7 @@ module.exports = class PeerInfo extends EventEmitter { this.reconnecting = true this.proven = false + this.connected = -1 this.banned = false this.tried = false this.explicit = false @@ -59,10 +62,14 @@ module.exports = class PeerInfo extends EventEmitter { _connected () { this.proven = true - this.attempts = 0 + this.connected = Date.now() } _disconnected () { + if (this.connected > -1 && (Date.now() - this.connected) >= MIN_CONNECTION_TIME) { + this.connected = -1 + this.attempts = 0 + } this.attempts++ } From b4473e7a07e3b67b7f7b1676c9cfe67ec07c7879 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 1 Nov 2023 14:00:19 +0100 Subject: [PATCH 2/3] clarify and always reset connectedTime to -1 --- lib/peer-info.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/peer-info.js b/lib/peer-info.js index 7ec2a94..8ac79e5 100644 --- a/lib/peer-info.js +++ b/lib/peer-info.js @@ -18,7 +18,7 @@ module.exports = class PeerInfo extends EventEmitter { this.reconnecting = true this.proven = false - this.connected = -1 + this.connectedTime = -1 this.banned = false this.tried = false this.explicit = false @@ -62,13 +62,13 @@ module.exports = class PeerInfo extends EventEmitter { _connected () { this.proven = true - this.connected = Date.now() + this.connectedTime = Date.now() } _disconnected () { - if (this.connected > -1 && (Date.now() - this.connected) >= MIN_CONNECTION_TIME) { - this.connected = -1 - this.attempts = 0 + if (this.connectedTime > -1) { + if ((Date.now() - this.connectedTime) >= MIN_CONNECTION_TIME) this.attempts = 0 // fast retry + this.connectedTime = -1 } this.attempts++ } From 26eed0e4897a1b318a8d7491e8200742c4fb2e31 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 1 Nov 2023 14:02:11 +0100 Subject: [PATCH 3/3] attach listeners in natural order --- index.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 58f107d..98456ec 100644 --- a/index.js +++ b/index.js @@ -171,6 +171,25 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false + conn.on('open', () => { + opened = true + this._connectDone() + this.connections.add(conn) + conn.removeListener('error', noop) + peerInfo._connected() + peerInfo.client = true + this.emit('connection', conn, peerInfo) + this._flushMaybe(peerInfo) + + this.emit('update') + }) + conn.on('error', err => { + if (this.relayThrough && shouldForceRelaying(err.code)) { + peerInfo.forceRelaying = true + // Reset the attempts in order to fast connect to relay + peerInfo.attempts = 0 + } + }) conn.on('close', () => { if (!opened) this._connectDone() this.connections.delete(conn) @@ -187,25 +206,6 @@ module.exports = class Hyperswarm extends EventEmitter { this.emit('update') }) - conn.on('error', err => { - if (this.relayThrough && shouldForceRelaying(err.code)) { - peerInfo.forceRelaying = true - // Reset the attempts in order to fast connect to relay - peerInfo.attempts = 0 - } - }) - conn.on('open', () => { - opened = true - this._connectDone() - this.connections.add(conn) - conn.removeListener('error', noop) - peerInfo._connected() - peerInfo.client = true - this.emit('connection', conn, peerInfo) - this._flushMaybe(peerInfo) - - this.emit('update') - }) this.emit('update') }