2019-12-03 10:28:52 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const AbortController = require('abort-controller')
|
2019-12-04 15:59:01 +01:00
|
|
|
const anySignal = require('any-signal')
|
2019-12-03 10:28:52 +01:00
|
|
|
const debug = require('debug')
|
2019-12-04 16:27:33 +01:00
|
|
|
const errCode = require('err-code')
|
2019-12-03 10:28:52 +01:00
|
|
|
const log = debug('libp2p:dialer:request')
|
|
|
|
log.error = debug('libp2p:dialer:request:error')
|
2019-12-04 16:27:33 +01:00
|
|
|
const FIFO = require('p-fifo')
|
|
|
|
const pAny = require('p-any')
|
2019-12-03 10:28:52 +01:00
|
|
|
|
|
|
|
class DialRequest {
|
|
|
|
/**
|
2019-12-06 18:43:59 +01:00
|
|
|
* Manages running the `dialAction` on multiple provided `addrs` in parallel
|
|
|
|
* up to a maximum determined by the number of tokens returned
|
|
|
|
* from `dialer.getTokens`. Once a DialRequest is created, it can be
|
|
|
|
* started using `DialRequest.run(options)`. Once a single dial has succeeded,
|
|
|
|
* all other dials in the request will be cancelled.
|
2019-12-03 10:28:52 +01:00
|
|
|
* @param {object} options
|
|
|
|
* @param {Multiaddr[]} options.addrs
|
2019-12-06 14:26:21 +01:00
|
|
|
* @param {function(Multiaddr):Promise<Connection>} options.dialAction
|
2019-12-03 10:28:52 +01:00
|
|
|
* @param {Dialer} options.dialer
|
|
|
|
*/
|
|
|
|
constructor ({
|
|
|
|
addrs,
|
|
|
|
dialAction,
|
|
|
|
dialer
|
|
|
|
}) {
|
|
|
|
this.addrs = addrs
|
|
|
|
this.dialer = dialer
|
|
|
|
this.dialAction = dialAction
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @async
|
|
|
|
* @param {object} options
|
|
|
|
* @param {AbortSignal} options.signal An AbortController signal
|
|
|
|
* @returns {Connection}
|
|
|
|
*/
|
|
|
|
async run (options) {
|
2019-12-04 16:27:33 +01:00
|
|
|
const tokens = this.dialer.getTokens(this.addrs.length)
|
2019-12-03 10:28:52 +01:00
|
|
|
// If no tokens are available, throw
|
|
|
|
if (tokens.length < 1) {
|
2019-12-04 16:27:33 +01:00
|
|
|
throw errCode(new Error('No dial tokens available'), 'ERR_NO_DIAL_TOKENS')
|
2019-12-03 10:28:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:04:43 +01:00
|
|
|
const tokenHolder = new FIFO()
|
|
|
|
tokens.forEach(token => tokenHolder.push(token))
|
2019-12-04 16:27:33 +01:00
|
|
|
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
2019-12-10 14:02:18 +01:00
|
|
|
let startedDials = 0
|
2019-12-03 10:28:52 +01:00
|
|
|
|
|
|
|
try {
|
2019-12-04 16:27:33 +01:00
|
|
|
return await pAny(this.addrs.map(async (addr, i) => {
|
2019-12-04 23:04:43 +01:00
|
|
|
const token = await tokenHolder.shift() // get token
|
2019-12-10 14:02:18 +01:00
|
|
|
startedDials++
|
2019-12-04 16:27:33 +01:00
|
|
|
let conn
|
|
|
|
try {
|
|
|
|
const signal = dialAbortControllers[i].signal
|
|
|
|
conn = await this.dialAction(addr, { ...options, signal: anySignal([signal, options.signal]) })
|
2019-12-04 16:59:38 +01:00
|
|
|
// Remove the successful AbortController so it is not aborted
|
2019-12-04 16:27:33 +01:00
|
|
|
dialAbortControllers.splice(i, 1)
|
2019-12-04 16:33:04 +01:00
|
|
|
} finally {
|
|
|
|
// If we have more dials to make, recycle the token, otherwise release it
|
2019-12-10 14:02:18 +01:00
|
|
|
if (startedDials < this.addrs.length) {
|
2019-12-04 23:04:43 +01:00
|
|
|
tokenHolder.push(token)
|
2019-12-04 16:33:04 +01:00
|
|
|
} else {
|
|
|
|
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
|
|
|
}
|
2019-12-04 16:27:33 +01:00
|
|
|
}
|
2019-12-04 16:33:04 +01:00
|
|
|
|
2019-12-04 16:27:33 +01:00
|
|
|
return conn
|
|
|
|
}))
|
2019-12-03 10:28:52 +01:00
|
|
|
} finally {
|
2019-12-04 16:27:33 +01:00
|
|
|
dialAbortControllers.map(c => c.abort()) // success/failure happened, abort everything else
|
2019-12-04 23:04:43 +01:00
|
|
|
tokens.forEach(token => this.dialer.releaseToken(token)) // release tokens back to the dialer
|
2019-12-03 10:28:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.DialRequest = DialRequest
|