Skip to content

Commit c3723a8

Browse files
committed
fix some suspension issues and fwd logs
1 parent 079837a commit c3723a8

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class HyperDHT extends DHT {
4242
this._validatedLocalAddresses = new Map()
4343

4444
this._lastRandomPunch = 0
45+
this._connectable = true
4546
this._randomPunchInterval = opts.randomPunchInterval || 20000 // min 20s between random punches...
4647
this._randomPunches = 0
4748
this._randomPunchLimit = 1 // set to one for extra safety for now
@@ -76,19 +77,29 @@ class HyperDHT extends DHT {
7677
return new ConnectionPool(this)
7778
}
7879

79-
async resume () {
80-
await super.resume()
80+
async resume ({ log = noop } = {}) {
81+
await super.resume({ log })
8182
const resuming = []
8283
for (const server of this.listening) resuming.push(server.resume())
84+
log('Resuming hyperdht servers')
8385
await Promise.allSettled(resuming)
86+
log('Done, hyperdht fully resumed')
8487
}
8588

86-
async suspend () {
89+
async suspend ({ log = noop } = {}) {
90+
this._connectable = false // just so nothing gets connected during suspension
8791
const suspending = []
8892
for (const server of this.listening) suspending.push(server.suspend())
93+
log('Suspending all hyperdht servers')
8994
await Promise.allSettled(suspending)
90-
await super.suspend()
95+
log('Done, clearing all raw streams')
9196
await this._rawStreams.clear()
97+
log('Done, suspending dht-rpc')
98+
await super.suspend({ log })
99+
log('Done, clearing raw streams again')
100+
await this._rawStreams.clear()
101+
log('Done, hyperdht fully suspended')
102+
this._connectable = true
92103
}
93104

94105
async destroy ({ force = false } = {}) {

lib/connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = function connect (dht, publicKey, opts = {}) {
4646
})
4747

4848
// in case a socket is made during suspended state, destroy it immediately
49-
if (dht.suspended) {
49+
if (dht.suspended || !dht._connectable) {
5050
encryptedSocket.destroy(SUSPENDED())
5151
return encryptedSocket
5252
}

0 commit comments

Comments
 (0)