refactor: cleanup and reorganize

This commit is contained in:
Jacob Heun 2019-12-04 23:04:43 +01:00
parent 6ad840c3f3
commit 98e82df5e1
2 changed files with 8 additions and 8 deletions

View File

@ -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
} }
} }
} }

View File

@ -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 {
/** /**