2019-12-03 10:28:52 +01:00
|
|
|
'use strict'
|
|
|
|
|
2019-12-04 16:27:33 +01:00
|
|
|
const errCode = require('err-code')
|
2020-12-10 14:48:14 +01:00
|
|
|
const AbortController = require('abort-controller').default
|
2021-01-27 09:45:31 +01:00
|
|
|
const { anySignal } = require('any-signal')
|
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
|
|
|
|
2020-12-10 14:48:14 +01:00
|
|
|
/**
|
|
|
|
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
|
|
* @typedef {import('./')} Dialer
|
|
|
|
* @typedef {import('multiaddr')} Multiaddr
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} DialOptions
|
|
|
|
* @property {AbortSignal} signal
|
|
|
|
*
|
|
|
|
* @typedef {Object} DialRequestOptions
|
|
|
|
* @property {Multiaddr[]} addrs
|
|
|
|
* @property {(m: Multiaddr, options: DialOptions) => Promise<Connection>} dialAction
|
|
|
|
* @property {Dialer} dialer
|
|
|
|
*/
|
|
|
|
|
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.
|
2020-10-06 14:59:43 +02:00
|
|
|
*
|
2020-12-10 14:48:14 +01:00
|
|
|
* @class
|
|
|
|
* @param {DialRequestOptions} options
|
2019-12-03 10:28:52 +01:00
|
|
|
*/
|
|
|
|
constructor ({
|
|
|
|
addrs,
|
|
|
|
dialAction,
|
|
|
|
dialer
|
|
|
|
}) {
|
|
|
|
this.addrs = addrs
|
|
|
|
this.dialer = dialer
|
|
|
|
this.dialAction = dialAction
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @async
|
2020-12-10 14:48:14 +01:00
|
|
|
* @param {object} [options]
|
|
|
|
* @param {AbortSignal} [options.signal] - An AbortController signal
|
|
|
|
* @returns {Promise<Connection>}
|
2019-12-03 10:28:52 +01:00
|
|
|
*/
|
2020-12-10 14:48:14 +01:00
|
|
|
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:45:52 +01:00
|
|
|
let completedDials = 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-04 16:27:33 +01:00
|
|
|
let conn
|
|
|
|
try {
|
|
|
|
const signal = dialAbortControllers[i].signal
|
2021-01-27 09:45:31 +01:00
|
|
|
conn = await this.dialAction(addr, { ...options, signal: options.signal ? anySignal([signal, options.signal]) : 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 {
|
2019-12-10 14:45:52 +01:00
|
|
|
completedDials++
|
|
|
|
// If we have more or equal dials remaining than tokens, recycle the token, otherwise release it
|
|
|
|
if (this.addrs.length - completedDials >= tokens.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 14:48:14 +01:00
|
|
|
module.exports = DialRequest
|