Skip to content

Commit

Permalink
fix!: do not auto-dial peers (#1397)
Browse files Browse the repository at this point in the history
Currently whenever a new peer is discovered we automatically dial them if we are below the connection manager connection limit.

This is incredibly resource intensive and does not necessarily result in high-quality connections (e.g. KAD close, relays or supporting protocols we are interested in).

Now that we can search the DHT for peers, this is no longer necessary so remove the functionality.

BREAKING CHANGE: the old behaviour was to dial any peer we discover, now we just add them to the peer store instead
  • Loading branch information
achingbrain authored Oct 6, 2022
1 parent 90d3528 commit ca30192
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 81 deletions.
5 changes: 5 additions & 0 deletions examples/libp2p-in-the-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/peer-and-content-routing/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions examples/webrtc-direct/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 0 additions & 65 deletions src/connection-manager/dialer/auto-dialer.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -238,20 +237,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> 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)
Expand Down

0 comments on commit ca30192

Please sign in to comment.