mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-10 01:38:06 +00:00
fix: do not let closest peers run forever (#1047)
The DHT takes a `signal` not a timeout so if a timeout is passed, create a `TimeoutController` that will abort the query after the timeout.
This commit is contained in:
parent
6d0ac819f1
commit
91c2ec9856
@ -10,6 +10,7 @@ const {
|
|||||||
uniquePeers,
|
uniquePeers,
|
||||||
requirePeers
|
requirePeers
|
||||||
} = require('./content-routing/utils')
|
} = require('./content-routing/utils')
|
||||||
|
const { TimeoutController } = require('timeout-abort-controller')
|
||||||
|
|
||||||
const merge = require('it-merge')
|
const merge = require('it-merge')
|
||||||
const { pipe } = require('it-pipe')
|
const { pipe } = require('it-pipe')
|
||||||
@ -34,6 +35,7 @@ const { DHTPeerRouting } = require('./dht/dht-peer-routing')
|
|||||||
* @property {boolean} [enabled = true] - Whether to enable the Refresh manager
|
* @property {boolean} [enabled = true] - Whether to enable the Refresh manager
|
||||||
* @property {number} [bootDelay = 6e5] - Boot delay to start the Refresh Manager (in ms)
|
* @property {number} [bootDelay = 6e5] - Boot delay to start the Refresh Manager (in ms)
|
||||||
* @property {number} [interval = 10e3] - Interval between each Refresh Manager run (in ms)
|
* @property {number} [interval = 10e3] - Interval between each Refresh Manager run (in ms)
|
||||||
|
* @property {number} [timeout = 10e3] - How long to let each refresh run (in ms)
|
||||||
*
|
*
|
||||||
* @typedef {Object} PeerRoutingOptions
|
* @typedef {Object} PeerRoutingOptions
|
||||||
* @property {RefreshManagerOptions} [refreshManager]
|
* @property {RefreshManagerOptions} [refreshManager]
|
||||||
@ -79,7 +81,7 @@ class PeerRouting {
|
|||||||
async _findClosestPeersTask () {
|
async _findClosestPeersTask () {
|
||||||
try {
|
try {
|
||||||
// nb getClosestPeers adds the addresses to the address book
|
// nb getClosestPeers adds the addresses to the address book
|
||||||
await drain(this.getClosestPeers(this._peerId.id))
|
await drain(this.getClosestPeers(this._peerId.id, { timeout: this._refreshManagerOptions.timeout || 10e3 }))
|
||||||
} catch (/** @type {any} */ err) {
|
} catch (/** @type {any} */ err) {
|
||||||
log.error(err)
|
log.error(err)
|
||||||
}
|
}
|
||||||
@ -131,7 +133,8 @@ class PeerRouting {
|
|||||||
*
|
*
|
||||||
* @param {Uint8Array} key - A CID like key
|
* @param {Uint8Array} key - A CID like key
|
||||||
* @param {Object} [options]
|
* @param {Object} [options]
|
||||||
* @param {number} [options.timeout=30e3] - How long the query can take.
|
* @param {number} [options.timeout=30e3] - How long the query can take
|
||||||
|
* @param {AbortSignal} [options.signal] - An AbortSignal to abort the request
|
||||||
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
||||||
*/
|
*/
|
||||||
async * getClosestPeers (key, options = { timeout: 30e3 }) {
|
async * getClosestPeers (key, options = { timeout: 30e3 }) {
|
||||||
@ -139,6 +142,10 @@ class PeerRouting {
|
|||||||
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
|
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.timeout) {
|
||||||
|
options.signal = new TimeoutController(options.timeout).signal
|
||||||
|
}
|
||||||
|
|
||||||
yield * pipe(
|
yield * pipe(
|
||||||
merge(
|
merge(
|
||||||
...this._routers.map(router => router.getClosestPeers(key, options))
|
...this._routers.map(router => router.getClosestPeers(key, options))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user