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')
|
throw errCode(new Error('No dial tokens available'), 'ERR_NO_DIAL_TOKENS')
|
||||||
}
|
}
|
||||||
|
|
||||||
const th = new FIFO()
|
const tokenHolder = new FIFO()
|
||||||
tokens.forEach(t => th.push(t))
|
tokens.forEach(token => tokenHolder.push(token))
|
||||||
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
||||||
let completedDials = 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 th.shift() // get token
|
const token = await tokenHolder.shift() // get token
|
||||||
let conn
|
let conn
|
||||||
try {
|
try {
|
||||||
const signal = dialAbortControllers[i].signal
|
const signal = dialAbortControllers[i].signal
|
||||||
@ -59,7 +59,7 @@ class DialRequest {
|
|||||||
completedDials++
|
completedDials++
|
||||||
// If we have more dials to make, recycle the token, otherwise release it
|
// If we have more dials to make, recycle the token, otherwise release it
|
||||||
if (completedDials < this.addrs.length) {
|
if (completedDials < this.addrs.length) {
|
||||||
th.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])
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ class DialRequest {
|
|||||||
}))
|
}))
|
||||||
} finally {
|
} finally {
|
||||||
dialAbortControllers.map(c => c.abort()) // success/failure happened, abort everything else
|
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 debug = require('debug')
|
||||||
const log = debug('libp2p:dialer')
|
const log = debug('libp2p:dialer')
|
||||||
log.error = debug('libp2p:dialer:error')
|
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 {
|
const {
|
||||||
DIAL_TIMEOUT,
|
DIAL_TIMEOUT,
|
||||||
MAX_PARALLEL_DIALS,
|
MAX_PARALLEL_DIALS,
|
||||||
MAX_PER_PEER_DIALS
|
MAX_PER_PEER_DIALS
|
||||||
} = require('./constants')
|
} = require('../constants')
|
||||||
|
|
||||||
class Dialer {
|
class Dialer {
|
||||||
/**
|
/**
|
Loading…
x
Reference in New Issue
Block a user