fix: release tokens as soon as they are available

This commit is contained in:
Jacob Heun
2019-12-10 14:02:18 +01:00
parent 633b0c291f
commit 2570a1ba30
2 changed files with 52 additions and 3 deletions

View File

@ -47,11 +47,12 @@ class DialRequest {
const tokenHolder = new FIFO()
tokens.forEach(token => tokenHolder.push(token))
const dialAbortControllers = this.addrs.map(() => new AbortController())
let completedDials = 0
let startedDials = 0
try {
return await pAny(this.addrs.map(async (addr, i) => {
const token = await tokenHolder.shift() // get token
startedDials++
let conn
try {
const signal = dialAbortControllers[i].signal
@ -59,9 +60,8 @@ class DialRequest {
// Remove the successful AbortController so it is not aborted
dialAbortControllers.splice(i, 1)
} finally {
completedDials++
// If we have more dials to make, recycle the token, otherwise release it
if (completedDials < this.addrs.length) {
if (startedDials < this.addrs.length) {
tokenHolder.push(token)
} else {
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])