From 1650d7b7a3ea1a830044ae2f09f6ce77214cc9b9 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 7 Dec 2021 14:36:29 +0000 Subject: [PATCH] fix: exclude dht from search-for-self The peer routing module starts a recurring process that searches for peers close to our peer id. This makes the DHT module query the network for peers. Thing is the DHT module is already doing this because periodically searching for peers close to us is in the DHT spec so this ends up making redundant queries. This PR excludes the DHT from the search-for-self task --- src/peer-routing.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/peer-routing.js b/src/peer-routing.js index 2ffa5cfd..8c0485ac 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -15,7 +15,6 @@ const { TimeoutController } = require('timeout-abort-controller') const merge = require('it-merge') const { pipe } = require('it-pipe') const first = require('it-first') -const drain = require('it-drain') const filter = require('it-filter') const { setDelayedInterval, @@ -77,13 +76,25 @@ class PeerRouting { /** * Recurrent task to find closest peers and add their addresses to the Address Book. + * Does not include the DHT (if present) as it has it's own method of periodically + * finding the closest peers */ async _findClosestPeersTask () { + const timeout = new TimeoutController(this._refreshManagerOptions.timeout || 10e3) + try { - // nb getClosestPeers adds the addresses to the address book - await drain(this.getClosestPeers(this._peerId.id, { timeout: this._refreshManagerOptions.timeout || 10e3 })) + const routers = this._routers.filter(router => !(router instanceof DHTPeerRouting)) + + await pipe( + merge( + ...routers.map(router => router.getClosestPeers(this._peerId.id, { signal: timeout.signal })) + ), + (source) => storeAddresses(source, this._peerStore) + ) } catch (/** @type {any} */ err) { log.error(err) + } finally { + timeout.clear() } }