js-libp2p/src/dialer/dial-request.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-12-03 10:28:52 +01:00
'use strict'
const errCode = require('err-code')
2021-01-27 09:45:31 +01:00
const { anySignal } = require('any-signal')
// @ts-ignore p-fifo does not export types
const FIFO = require('p-fifo')
const pAny = require('p-any')
// @ts-expect-error setMaxListeners is missing from the types
const { setMaxListeners } = require('events')
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} Multiaddr
2020-12-10 14:48:14 +01:00
*/
/**
* @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-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 = {}) {
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) {
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))
const dialAbortControllers = this.addrs.map(() => {
const controller = new AbortController()
setMaxListeners && setMaxListeners(Infinity, controller.signal)
return controller
})
2019-12-10 14:45:52 +01:00
let completedDials = 0
2019-12-03 10:28:52 +01:00
try {
return await pAny(this.addrs.map(async (addr, i) => {
2019-12-04 23:04:43 +01:00
const token = await tokenHolder.shift() // get token
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
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:33:04 +01:00
return conn
}))
2019-12-03 10:28:52 +01:00
} finally {
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