Skip to content

Commit

Permalink
improve suspend/destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 8, 2023
1 parent 1b5daa1 commit 8ff3a9b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
49 changes: 34 additions & 15 deletions lib/peer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -183,6 +187,8 @@ module.exports = class PeerDiscovery {
this._timer = null
}

const nodes = this._closestNodes

if (this._currentRefresh) {
try {
await this._currentRefresh
Expand All @@ -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 () {
Expand Down

0 comments on commit 8ff3a9b

Please sign in to comment.