mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-22 05:21:35 +00:00
refactor: clean up dial timeout abort
This commit is contained in:
@ -81,24 +81,18 @@ class Dialer {
|
||||
const signals = [timeoutController.signal]
|
||||
options.signal && signals.push(options.signal)
|
||||
const signal = anySignal(signals)
|
||||
const timeoutPromise = delay.reject(this.timeout, {
|
||||
value: errCode(new Error('Dial timed out'), codes.ERR_TIMEOUT)
|
||||
})
|
||||
const timeoutId = setTimeout(() => timeoutController.abort(), this.timeout)
|
||||
|
||||
try {
|
||||
// Race the dial request and the timeout
|
||||
const dialResult = await Promise.race([
|
||||
dialRequest.run({
|
||||
...options,
|
||||
signal
|
||||
}),
|
||||
timeoutPromise
|
||||
])
|
||||
timeoutPromise.clear()
|
||||
const dialResult = await dialRequest.run({ ...options, signal })
|
||||
clearTimeout(timeoutId)
|
||||
return dialResult
|
||||
} catch (err) {
|
||||
// Error is a timeout
|
||||
if (timeoutController.signal.aborted) {
|
||||
err = errCode(err, codes.ERR_TIMEOUT)
|
||||
}
|
||||
log.error(err)
|
||||
timeoutController.abort()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ class DialRequest {
|
||||
try {
|
||||
const signal = dialAbortControllers[i].signal
|
||||
conn = await this.dialAction(addr, { ...options, signal: anySignal([signal, options.signal]) })
|
||||
// Remove the successful AbortController so it is no aborted
|
||||
// Remove the successful AbortController so it is not aborted
|
||||
dialAbortControllers.splice(i, 1)
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
Reference in New Issue
Block a user