mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-24 22:41:33 +00:00
feat: abort all pending dials on stop
This commit is contained in:
@ -38,6 +38,7 @@ class Dialer {
|
||||
this.timeout = timeout
|
||||
this.perPeerLimit = perPeerLimit
|
||||
this.tokens = [...new Array(concurrency)].map((_, index) => index)
|
||||
this.pendingDials = new Set()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,6 +70,12 @@ class Dialer {
|
||||
const signal = anySignal(signals)
|
||||
const timeoutId = setTimeout(() => timeoutController.abort(), this.timeout)
|
||||
|
||||
const dial = {
|
||||
dialRequest,
|
||||
controller: timeoutController
|
||||
}
|
||||
this.pendingDials.add(dial)
|
||||
|
||||
try {
|
||||
const dialResult = await dialRequest.run({ ...options, signal })
|
||||
clearTimeout(timeoutId)
|
||||
@ -81,6 +88,8 @@ class Dialer {
|
||||
}
|
||||
log.error(err)
|
||||
throw err
|
||||
} finally {
|
||||
this.pendingDials.delete(dial)
|
||||
}
|
||||
}
|
||||
|
||||
|
11
src/index.js
11
src/index.js
@ -194,8 +194,15 @@ class Libp2p extends EventEmitter {
|
||||
log('libp2p is stopping')
|
||||
|
||||
try {
|
||||
this.pubsub && await this.pubsub.stop()
|
||||
this._dht && await this._dht.stop()
|
||||
await Promise.all([
|
||||
this.pubsub && this.pubsub.stop(),
|
||||
this._dht && this._dht.stop()
|
||||
])
|
||||
|
||||
for (const dial of this.dialer.pendingDials.values()) {
|
||||
dial.abort()
|
||||
}
|
||||
|
||||
await this.transportManager.close()
|
||||
await this.registrar.close()
|
||||
} catch (err) {
|
||||
|
Reference in New Issue
Block a user