refactor: clean up dial timeout abort

This commit is contained in:
Jacob Heun
2019-12-04 16:59:38 +01:00
parent a5b54a7017
commit b6cd48b798
4 changed files with 12 additions and 14 deletions

View File

@ -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
}
}

View File

@ -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