mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-16 20:41:20 +00:00
feat: add early token recycling in
This commit is contained in:
parent
e1e3be8106
commit
a5b54a7017
@ -45,6 +45,7 @@ class DialRequest {
|
|||||||
const th = new FIFO()
|
const th = new FIFO()
|
||||||
tokens.forEach(t => th.push(t))
|
tokens.forEach(t => th.push(t))
|
||||||
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
||||||
|
let completedDials = 0
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await pAny(this.addrs.map(async (addr, i) => {
|
return await pAny(this.addrs.map(async (addr, i) => {
|
||||||
@ -56,9 +57,17 @@ class DialRequest {
|
|||||||
// Remove the successful AbortController so it is no aborted
|
// Remove the successful AbortController so it is no aborted
|
||||||
dialAbortControllers.splice(i, 1)
|
dialAbortControllers.splice(i, 1)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
th.push(token) // return to token holder on error so another ma can be attempted
|
|
||||||
throw err
|
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
|
return conn
|
||||||
}))
|
}))
|
||||||
} finally {
|
} finally {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user