mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-26 11:02:14 +00:00
refactor: cleanup and reorganize
This commit is contained in:
parent
6ad840c3f3
commit
98e82df5e1
@ -41,14 +41,14 @@ class DialRequest {
|
||||
throw errCode(new Error('No dial tokens available'), 'ERR_NO_DIAL_TOKENS')
|
||||
}
|
||||
|
||||
const th = new FIFO()
|
||||
tokens.forEach(t => th.push(t))
|
||||
const tokenHolder = new FIFO()
|
||||
tokens.forEach(token => tokenHolder.push(token))
|
||||
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
||||
let completedDials = 0
|
||||
|
||||
try {
|
||||
return await pAny(this.addrs.map(async (addr, i) => {
|
||||
const token = await th.shift() // get token
|
||||
const token = await tokenHolder.shift() // get token
|
||||
let conn
|
||||
try {
|
||||
const signal = dialAbortControllers[i].signal
|
||||
@ -59,7 +59,7 @@ class DialRequest {
|
||||
completedDials++
|
||||
// If we have more dials to make, recycle the token, otherwise release it
|
||||
if (completedDials < this.addrs.length) {
|
||||
th.push(token)
|
||||
tokenHolder.push(token)
|
||||
} else {
|
||||
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
||||
}
|
||||
@ -69,7 +69,7 @@ class DialRequest {
|
||||
}))
|
||||
} finally {
|
||||
dialAbortControllers.map(c => c.abort()) // success/failure happened, abort everything else
|
||||
tokens.forEach(t => this.dialer.releaseToken(t)) // release tokens back to the dialer
|
||||
tokens.forEach(token => this.dialer.releaseToken(token)) // release tokens back to the dialer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ const anySignal = require('any-signal')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:dialer')
|
||||
log.error = debug('libp2p:dialer:error')
|
||||
const { DialRequest } = require('./dialer/dial-request')
|
||||
const { DialRequest } = require('./dial-request')
|
||||
|
||||
const { codes } = require('./errors')
|
||||
const { codes } = require('../errors')
|
||||
const {
|
||||
DIAL_TIMEOUT,
|
||||
MAX_PARALLEL_DIALS,
|
||||
MAX_PER_PEER_DIALS
|
||||
} = require('./constants')
|
||||
} = require('../constants')
|
||||
|
||||
class Dialer {
|
||||
/**
|
Loading…
x
Reference in New Issue
Block a user