diff --git a/examples/libp2p-in-the-browser/index.js b/examples/libp2p-in-the-browser/index.js index 92213d9afe..eccd6953c7 100644 --- a/examples/libp2p-in-the-browser/index.js +++ b/examples/libp2p-in-the-browser/index.js @@ -54,6 +54,11 @@ document.addEventListener('DOMContentLoaded', async () => { libp2p.addEventListener('peer:discovery', (evt) => { const peer = evt.detail log(`Found peer ${peer.id.toString()}`) + + // dial them when we discover them + libp2p.dial(evt.detail.id).catch(err => { + log(`Could not dial ${evt.detail.id}`, err) + }) }) // Listen for new connections to peers diff --git a/examples/peer-and-content-routing/1.js b/examples/peer-and-content-routing/1.js index 12ecfd4be8..7208909826 100644 --- a/examples/peer-and-content-routing/1.js +++ b/examples/peer-and-content-routing/1.js @@ -38,7 +38,7 @@ const createNode = async () => { ]) // The DHT routing tables need a moment to populate - await delay(100) + await delay(1000) const peer = await node1.peerRouting.findPeer(node3.peerId) diff --git a/examples/webrtc-direct/dialer.js b/examples/webrtc-direct/dialer.js index 1663054cfb..d1dc0066ba 100644 --- a/examples/webrtc-direct/dialer.js +++ b/examples/webrtc-direct/dialer.js @@ -31,6 +31,11 @@ document.addEventListener('DOMContentLoaded', async () => { // Listen for new peers libp2p.addEventListener('peer:discovery', (evt) => { log(`Found peer ${evt.detail.id.toString()}`) + + // dial them when we discover them + libp2p.dial(evt.detail.id).catch(err => { + log(`Could not dial ${evt.detail.id}`, err) + }) }) // Listen for new connections to peers diff --git a/src/connection-manager/dialer/auto-dialer.ts b/src/connection-manager/dialer/auto-dialer.ts deleted file mode 100644 index 0c572647ca..0000000000 --- a/src/connection-manager/dialer/auto-dialer.ts +++ /dev/null @@ -1,65 +0,0 @@ -import type { PeerInfo } from '@libp2p/interface-peer-info' -import { logger } from '@libp2p/logger' -import type { Components } from '@libp2p/components' -import { TimeoutController } from 'timeout-abort-controller' -import { setMaxListeners } from 'events' - -const log = logger('libp2p:dialer:auto-dialer') - -export interface AutoDialerInit { - enabled: boolean - minConnections: number - dialTimeout: number -} - -export class AutoDialer { - private readonly components: Components - private readonly enabled: boolean - private readonly minConnections: number - private readonly dialTimeout: number - - constructor (components: Components, init: AutoDialerInit) { - this.components = components - this.enabled = init.enabled - this.minConnections = init.minConnections - this.dialTimeout = init.dialTimeout - } - - public handle (evt: CustomEvent) { - const { detail: peer } = evt - - if (!this.enabled) { - return - } - - const connections = this.components.getConnectionManager().getConnections(peer.id) - - // If auto dialing is on and we have no connection to the peer, check if we should dial - if (connections.length === 0) { - const minConnections = this.minConnections ?? 0 - - const allConnections = this.components.getConnectionManager().getConnections() - - if (minConnections > allConnections.length) { - log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout) - - const controller = new TimeoutController(this.dialTimeout) - - try { - // fails on node < 15.4 - setMaxListeners?.(Infinity, controller.signal) - } catch {} - - void this.components.getConnectionManager().openConnection(peer.id, { - signal: controller.signal - }) - .catch(err => { - log.error('could not connect to discovered peer %p with %o', peer.id, err) - }) - .finally(() => { - controller.clear() - }) - } - } - } -} diff --git a/src/libp2p.ts b/src/libp2p.ts index 0bc0c9959c..00a9748881 100644 --- a/src/libp2p.ts +++ b/src/libp2p.ts @@ -26,7 +26,6 @@ import { PeerRecordUpdater } from './peer-record-updater.js' import { DHTPeerRouting } from './dht/dht-peer-routing.js' import { PersistentPeerStore } from '@libp2p/peer-store' import { DHTContentRouting } from './dht/dht-content-routing.js' -import { AutoDialer } from './connection-manager/dialer/auto-dialer.js' import { Initializable, Components, isInitializable } from '@libp2p/components' import type { PeerId } from '@libp2p/interface-peer-id' import type { Connection } from '@libp2p/interface-connection' @@ -238,20 +237,6 @@ export class Libp2pNode extends EventEmitter implements Libp2p { ...init.ping })) - const autoDialer = this.configureComponent(new AutoDialer(this.components, { - enabled: init.connectionManager.autoDial !== false, - minConnections: init.connectionManager.minConnections, - dialTimeout: init.connectionManager.dialTimeout ?? 30000 - })) - - this.addEventListener('peer:discovery', evt => { - if (!this.isStarted()) { - return - } - - autoDialer.handle(evt) - }) - // Discovery modules for (const service of init.peerDiscovery ?? []) { this.configureComponent(service)