mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-26 11:02:14 +00:00
fix: token release logic
This commit is contained in:
parent
3cadeb39cb
commit
1838a641d9
@ -47,12 +47,11 @@ class DialRequest {
|
|||||||
const tokenHolder = new FIFO()
|
const tokenHolder = new FIFO()
|
||||||
tokens.forEach(token => tokenHolder.push(token))
|
tokens.forEach(token => tokenHolder.push(token))
|
||||||
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
||||||
let startedDials = 0
|
let completedDials = 0
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await pAny(this.addrs.map(async (addr, i) => {
|
return await pAny(this.addrs.map(async (addr, i) => {
|
||||||
const token = await tokenHolder.shift() // get token
|
const token = await tokenHolder.shift() // get token
|
||||||
startedDials++
|
|
||||||
let conn
|
let conn
|
||||||
try {
|
try {
|
||||||
const signal = dialAbortControllers[i].signal
|
const signal = dialAbortControllers[i].signal
|
||||||
@ -60,8 +59,9 @@ class DialRequest {
|
|||||||
// Remove the successful AbortController so it is not aborted
|
// Remove the successful AbortController so it is not aborted
|
||||||
dialAbortControllers.splice(i, 1)
|
dialAbortControllers.splice(i, 1)
|
||||||
} finally {
|
} finally {
|
||||||
// If we have more dials to make, recycle the token, otherwise release it
|
completedDials++
|
||||||
if (startedDials < this.addrs.length) {
|
// If we have more or equal dials remaining than tokens, recycle the token, otherwise release it
|
||||||
|
if (this.addrs.length - completedDials >= tokens.length) {
|
||||||
tokenHolder.push(token)
|
tokenHolder.push(token)
|
||||||
} else {
|
} else {
|
||||||
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
||||||
|
@ -53,16 +53,11 @@ describe('Dial Request', () => {
|
|||||||
|
|
||||||
it('should release tokens when all addr dials have started', async () => {
|
it('should release tokens when all addr dials have started', async () => {
|
||||||
const mockConnection = await createMockConnection()
|
const mockConnection = await createMockConnection()
|
||||||
|
const firstDials = pDefer()
|
||||||
const deferred = pDefer()
|
const deferred = pDefer()
|
||||||
const actions = {
|
const actions = {
|
||||||
1: async () => {
|
1: () => firstDials.promise,
|
||||||
await delay(0)
|
2: () => firstDials.promise,
|
||||||
return Promise.reject(error)
|
|
||||||
},
|
|
||||||
2: async () => {
|
|
||||||
await delay(0)
|
|
||||||
return Promise.reject(error)
|
|
||||||
},
|
|
||||||
3: () => deferred.promise
|
3: () => deferred.promise
|
||||||
}
|
}
|
||||||
const dialAction = (num) => actions[num]()
|
const dialAction = (num) => actions[num]()
|
||||||
@ -85,7 +80,11 @@ describe('Dial Request', () => {
|
|||||||
sinon.spy(dialer, 'releaseToken')
|
sinon.spy(dialer, 'releaseToken')
|
||||||
dialRequest.run({ signal: controller.signal })
|
dialRequest.run({ signal: controller.signal })
|
||||||
// Let the first dials run
|
// Let the first dials run
|
||||||
await delay(100)
|
await delay(0)
|
||||||
|
|
||||||
|
// Finish the first 2 dials
|
||||||
|
firstDials.reject(error)
|
||||||
|
await delay(0)
|
||||||
|
|
||||||
// Only 1 dial should remain, so 1 token should have been released
|
// Only 1 dial should remain, so 1 token should have been released
|
||||||
expect(actions[1]).to.have.property('callCount', 1)
|
expect(actions[1]).to.have.property('callCount', 1)
|
||||||
@ -93,7 +92,7 @@ describe('Dial Request', () => {
|
|||||||
expect(actions[3]).to.have.property('callCount', 1)
|
expect(actions[3]).to.have.property('callCount', 1)
|
||||||
expect(dialer.releaseToken).to.have.property('callCount', 1)
|
expect(dialer.releaseToken).to.have.property('callCount', 1)
|
||||||
|
|
||||||
// Finish the dial
|
// Finish the dial and release the 2nd token
|
||||||
deferred.resolve(mockConnection)
|
deferred.resolve(mockConnection)
|
||||||
await delay(0)
|
await delay(0)
|
||||||
expect(dialer.releaseToken).to.have.property('callCount', 2)
|
expect(dialer.releaseToken).to.have.property('callCount', 2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user