|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug') |
| 4 | +const mergeOptions = require('merge-options') |
| 5 | +// @ts-ignore retimer does not have types |
| 6 | +const retimer = require('retimer') |
| 7 | + |
| 8 | +const log = Object.assign(debug('libp2p:connection-manager:auto-dialler'), { |
| 9 | + error: debug('libp2p:connection-manager:auto-dialler:err') |
| 10 | +}) |
| 11 | + |
| 12 | +const defaultOptions = { |
| 13 | + enabled: true, |
| 14 | + minConnections: 0, |
| 15 | + autoDialInterval: 10000 |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @typedef {import('../index')} Libp2p |
| 20 | + * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @typedef {Object} AutoDiallerOptions |
| 25 | + * @property {boolean} [enabled = true] - Should preemptively guarantee connections are above the low watermark |
| 26 | + * @property {number} [minConnections = 0] - The minimum number of connections to avoid pruning |
| 27 | + * @property {number} [autoDialInterval = 10000] - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark |
| 28 | + */ |
| 29 | + |
| 30 | +class AutoDialler { |
| 31 | + /** |
| 32 | + * Proactively tries to connect to known peers stored in the PeerStore. |
| 33 | + * It will keep the number of connections below the upper limit and sort |
| 34 | + * the peers to connect based on wether we know their keys and protocols. |
| 35 | + * |
| 36 | + * @class |
| 37 | + * @param {Libp2p} libp2p |
| 38 | + * @param {AutoDiallerOptions} options |
| 39 | + */ |
| 40 | + constructor (libp2p, options = {}) { |
| 41 | + this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options) |
| 42 | + this._libp2p = libp2p |
| 43 | + this._running = false |
| 44 | + this._autoDialTimeout = null |
| 45 | + this._autoDial = this._autoDial.bind(this) |
| 46 | + |
| 47 | + log('options: %j', this._options) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Starts the auto dialer |
| 52 | + */ |
| 53 | + start () { |
| 54 | + if (!this._options.enabled) { |
| 55 | + log('not enabled') |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + this._running = true |
| 60 | + this._autoDial() |
| 61 | + log('started') |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Stops the auto dialler |
| 66 | + */ |
| 67 | + async stop () { |
| 68 | + if (!this._options.enabled) { |
| 69 | + log('not enabled') |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + this._running = false |
| 74 | + this._autoDialTimeout && this._autoDialTimeout.clear() |
| 75 | + log('stopped') |
| 76 | + } |
| 77 | + |
| 78 | + async _autoDial () { |
| 79 | + const minConnections = this._options.minConnections |
| 80 | + |
| 81 | + // Already has enough connections |
| 82 | + if (this._libp2p.connections.size >= minConnections) { |
| 83 | + this._autoDialTimeout = retimer(this._autoDial, this._options.autoDialInterval) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Sort peers on wether we know protocols of public keys for them |
| 88 | + const peers = Array.from(this._libp2p.peerStore.peers.values()) |
| 89 | + .sort((a, b) => { |
| 90 | + if (b.protocols && b.protocols.length && (!a.protocols || !a.protocols.length)) { |
| 91 | + return 1 |
| 92 | + } else if (b.id.pubKey && !a.id.pubKey) { |
| 93 | + return 1 |
| 94 | + } |
| 95 | + return -1 |
| 96 | + }) |
| 97 | + |
| 98 | + for (let i = 0; this._running && i < peers.length && this._libp2p.connections.size < minConnections; i++) { |
| 99 | + if (!this._libp2p.connectionManager.get(peers[i].id)) { |
| 100 | + log('connecting to a peerStore stored peer %s', peers[i].id.toB58String()) |
| 101 | + try { |
| 102 | + await this._libp2p.dialer.connectToPeer(peers[i].id) |
| 103 | + } catch (/** @type {any} */ err) { |
| 104 | + log.error('could not connect to peerStore stored peer', err) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Connection Manager was stopped |
| 110 | + if (!this._running) { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + this._autoDialTimeout = retimer(this._autoDial, this._options.autoDialInterval) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = AutoDialler |
0 commit comments