Skip to content

Commit

Permalink
feat: add early token recycling in
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Dec 12, 2019
1 parent ea62c52 commit 24c6037
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/dialer/dial-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DialRequest {
const th = new FIFO()
tokens.forEach(t => th.push(t))
const dialAbortControllers = this.addrs.map(() => new AbortController())
let completedDials = 0

try {
return await pAny(this.addrs.map(async (addr, i) => {
Expand All @@ -56,9 +57,17 @@ class DialRequest {
// Remove the successful AbortController so it is no aborted
dialAbortControllers.splice(i, 1)
} catch (err) {
th.push(token) // return to token holder on error so another ma can be attempted
throw err
} finally {
completedDials++
// If we have more dials to make, recycle the token, otherwise release it
if (completedDials < this.addrs.length) {
th.push(token)
} else {
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
}
}

return conn
}))
} finally {
Expand Down

0 comments on commit 24c6037

Please sign in to comment.