From 8ff3a9bbfbe86dee5c2db18facd8ae0cdbad2140 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 8 Nov 2023 10:42:41 +0100 Subject: [PATCH 1/3] improve suspend/destroy --- index.js | 7 +++++-- lib/peer-discovery.js | 49 ++++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 98456ec..f70898c 100644 --- a/index.js +++ b/index.js @@ -474,16 +474,19 @@ module.exports = class Hyperswarm extends EventEmitter { async suspend () { if (this.suspended) return + const promises = [] for (const discovery of this._discovery.values()) { - discovery.suspend() + promises.push(discovery.suspend()) } for (const connection of this._allConnections) { connection.destroy() } - await this.dht.suspend() + promises.push(this.dht.suspend()) this.suspended = true + + await Promise.allSettled(promises) } async resume () { diff --git a/lib/peer-discovery.js b/lib/peer-discovery.js index cc01f87..6a47c18 100644 --- a/lib/peer-discovery.js +++ b/lib/peer-discovery.js @@ -56,15 +56,20 @@ module.exports = class PeerDiscovery { }, delay) } + _isActive () { + return !this.destroyed && !this.suspended + } + // TODO: Allow announce to be an argument to this // TODO: Maybe announce should be a setter? async _refresh () { + if (this.suspended) return const clock = ++this._refreshes if (this._wait) { await this._wait this._wait = null - if (clock !== this._refreshes) return + if (clock !== this._refreshes || !this._isActive()) return } const clear = this.isServer && this._firstAnnounce @@ -78,7 +83,7 @@ module.exports = class PeerDiscovery { if (this.isServer) { await this.swarm.listen() // if a parallel refresh is happening, yield to the new one - if (clock !== this._refreshes) return + if (clock !== this._refreshes || !this._isActive()) return this._needsUnannounce = true } @@ -91,15 +96,17 @@ module.exports = class PeerDiscovery { try { for await (const data of this._activeQuery) { - if (!this.isClient) continue + if (!this.isClient || !this._isActive()) continue for (const peer of data.peers) { this._onpeer(peer, data) } } + } catch (err) { + if (this._isActive()) throw err } finally { if (this._activeQuery === query) { this._activeQuery = null - if (!this.destroyed) this._refreshLater(false) + if (!this.destroyed && !this.suspended) this._refreshLater(false) } } @@ -168,10 +175,7 @@ module.exports = class PeerDiscovery { return this.destroying } - async _destroy () { - if (this.destroyed) return - this.destroyed = true - + async _abort () { if (this._wait) await this._wait if (this._activeQuery) { @@ -183,6 +187,8 @@ module.exports = class PeerDiscovery { this._timer = null } + const nodes = this._closestNodes + if (this._currentRefresh) { try { await this._currentRefresh @@ -191,20 +197,33 @@ module.exports = class PeerDiscovery { } } + if (this._isActive()) return + + if (this._closestNodes !== nodes) { + for (const node of this._closestNodes) { + for (const n of nodes) { + if (!n.id || !node.id || b4a.equals(n.id, node.id)) continue + nodes.push(n) + } + } + } + if (this._needsUnannounce) { - await this.swarm.dht.unannounce(this.topic, this.swarm.keyPair) + await this.swarm.dht.unannounce(this.topic, this.swarm.keyPair, { closestNodes: nodes, onlyClosestNodes: true }) this._needsUnannounce = false } } - suspend () { + _destroy () { + if (this.destroyed) return + this.destroyed = true + return this._abort() + } + + async suspend () { if (this.suspended) return this.suspended = true - - if (this._timer) { - clearTimeout(this._timer) - this._timer = null - } + return this._abort() } resume () { From 606fdddcd9ddeaf819a06ac388f616f1557934e1 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 8 Nov 2023 13:11:42 +0100 Subject: [PATCH 2/3] use server suspend and fix bug --- index.js | 3 +++ lib/peer-discovery.js | 1 + 2 files changed, 4 insertions(+) diff --git a/index.js b/index.js index f70898c..64e949b 100644 --- a/index.js +++ b/index.js @@ -475,6 +475,9 @@ module.exports = class Hyperswarm extends EventEmitter { if (this.suspended) return const promises = [] + + promises.push(this.server.suspend()) + for (const discovery of this._discovery.values()) { promises.push(discovery.suspend()) } diff --git a/lib/peer-discovery.js b/lib/peer-discovery.js index 6a47c18..fbb8358 100644 --- a/lib/peer-discovery.js +++ b/lib/peer-discovery.js @@ -1,4 +1,5 @@ const safetyCatch = require('safety-catch') +const b4a = require('b4a') const REFRESH_INTERVAL = 1000 * 60 * 10 // 10 min const RANDOM_JITTER = 1000 * 60 * 2 // 2 min From 97c35b815a27bf27b0cd1e2f1c685afac94c3558 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 8 Nov 2023 13:12:23 +0100 Subject: [PATCH 3/3] force bump dht --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 14e7a3c..7224130 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "dependencies": { "b4a": "^1.3.1", "events": "^3.3.0", - "hyperdht": "^6.7.0", + "hyperdht": "^6.10.0", "safety-catch": "^1.0.2", "shuffled-priority-queue": "^2.1.0" },