Skip to content

Commit

Permalink
fix!: remove connection manager autodial option (#1626)
Browse files Browse the repository at this point in the history
The connection manager's `autoDial` option originally meant "dial every
peer we discover".  Then it changed to "if we have fewer than `minConnections`,
dial peers from the peer store until we have more than `minConnections`".

This is confusing and also redundant, since if we don't want to dial
peers to ensure we have more than `minConnections` we can just set
`minConnections` to `0`.

Also fixes a bug where we actually configured two autodialer components.

BREAKING CHANGE: the `autoDial` option has been removed from the connection manager, please see the upgrade guide

Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com>
  • Loading branch information
achingbrain and maschad authored Mar 17, 2023
1 parent d6c8601 commit da3526c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 64 deletions.
7 changes: 1 addition & 6 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,7 @@ const node = await createLibp2p({
],
interval: 2000
)
],
connectionManager: {
autoDial: true // Auto connect to discovered peers (limited by ConnectionManager minConnections)
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
// The associated object, will be passed to the service when it is instantiated.
}
]
})
```

Expand Down
7 changes: 1 addition & 6 deletions doc/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,7 @@ const node = await createLibp2p({
bootstrap({
list: bootstrapMultiaddrs, // provide array of multiaddrs
})
],
connectionManager: {
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
// The associated object, will be passed to the service when it is instantiated.
}
]
})

node.addEventListener('peer:discovery', (evt) => {
Expand Down
2 changes: 1 addition & 1 deletion doc/PEER_DISCOVERY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* To ensure reasonable resource usage, discovered peers are not connected to automatically
* Applications should lisen for the `peer:connect` event if they wish to take a specific action when new connections are established
* Libp2p functions best with a good number of network connections to a diverse set of peers. When the number of connected peers a node has falls under the [ConnectionManager](./CONFIGURATION.md#configuring-connection-manager) `minConnections` setting, randomly selected peers from the peer store will be dialed until the node's number of connections rises above this number.
* Applications can disable this behaviour via the `connectionManager.autoDial` config property, and handle increasing the current number of connections themselves
* Applications can disable this behaviour by setting the `connectionManager.minConnections` config property to `0`, but they will have to manage increasing the current number of connections themselves.

## Scenarios

Expand Down
38 changes: 38 additions & 0 deletions doc/migrations/v0.42-v0.43.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A migration guide for refactoring your application code from libp2p v0.42.x to v
## Table of Contents <!-- omit in toc -->

- [Circuit Relay v2](#circuit-relay-v2)
- [Connection manager autodial](#connection-manager-autodial)

## Circuit Relay v2

Expand Down Expand Up @@ -77,3 +78,40 @@ const node = await createLibp2p({
```

Please see the [Setup with Relay](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#setup-with-relay) section of the configuration for a full breakdown of all the options.

## Connection manager autodial

The `autoDial` configutation option has been removed from the connection manager.

This setting used to control whether libp2p would automatically dial every discovered peer, then it was changed to control whether libp2p would try to dial peers from the peer store to keep the number of connections above `minConnections`.

Instead, just set `minConnections` to `0` if you don't want to keep a minimum number of connections open.

**Before**

```js
import { createLibp2p } from 'libp2p'

const node = await createLibp2p({
// ... other options
connectionManager: {
autoDial: false,
minConnections: 10,
maxConnections: 100
}
}
```
**After**
```js
import { createLibp2p } from 'libp2p'

const node = await createLibp2p({
// ... other options
connectionManager: {
minConnections: 0,
maxConnections: 100
}
}
```
2 changes: 1 addition & 1 deletion examples/discovery-mechanisms/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import bootstrapers from './bootstrappers.js'

node.addEventListener('peer:discovery', (evt) => {
const peer = evt.detail
// No need to dial, autoDial is on

console.log('Discovered:', peer.id.toString())
})
})();
7 changes: 1 addition & 6 deletions examples/transports/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ const createNode = async (addresses = []) => {
})
],
connectionEncryption: [noise()],
streamMuxers: [mplex()],
connectionManager: {
// Disable autoDial as it would fail because we are using a self-signed cert.
// `dialProtocol` does not fail because we pass `rejectUnauthorized: false`.
autoDial: false
}
streamMuxers: [mplex()]
})

return node
Expand Down
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const DefaultConfig: Partial<Libp2pInit> = {
connectionManager: {
maxConnections: 300,
minConnections: 50,
autoDial: true,
autoDialInterval: 10000,
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
Expand Down
16 changes: 0 additions & 16 deletions src/connection-manager/auto-dialler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
const log = logger('libp2p:connection-manager:auto-dialler')

export interface AutoDiallerInit {
/**
* Should preemptively guarantee connections are above the low watermark
*/
enabled?: boolean

/**
* The minimum number of connections to avoid pruning
*/
Expand All @@ -33,7 +28,6 @@ export interface AutoDiallerComponents {
}

const defaultOptions: Partial<AutoDiallerInit> = {
enabled: true,
minConnections: 0,
autoDialInterval: 10000
}
Expand Down Expand Up @@ -66,11 +60,6 @@ export class AutoDialler implements Startable {
* Starts the auto dialer
*/
async start (): Promise<void> {
if (!this.options.enabled) {
log('not enabled')
return
}

this.running = true

void this._autoDial().catch(err => {
Expand All @@ -84,11 +73,6 @@ export class AutoDialler implements Startable {
* Stops the auto dialler
*/
async stop (): Promise<void> {
if (!this.options.enabled) {
log('not enabled')
return
}

this.running = false

if (this.autoDialTimeout != null) {
Expand Down
5 changes: 0 additions & 5 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ export interface ConnectionManagerConfig {
*/
pollInterval?: number

/**
* If true, try to connect to all discovered peers up to the connection manager limit
*/
autoDial?: boolean

/**
* How long to wait between attempting to keep our number of concurrent connections
* above minConnections
Expand Down
7 changes: 0 additions & 7 deletions src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
// update our peer record when addresses change
this.configureComponent(new PeerRecordUpdater(this.components))

this.configureComponent(new AutoDialler(this.components, {
enabled: init.connectionManager.autoDial,
minConnections: init.connectionManager.minConnections,
autoDialInterval: init.connectionManager.autoDialInterval
}))

// Create keychain
const keychainOpts = DefaultKeyChain.generateOptions()
this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
Expand Down Expand Up @@ -237,7 +231,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
}))

this.configureComponent(new AutoDialler(this.components, {
enabled: init.connectionManager.autoDial,
minConnections: init.connectionManager.minConnections,
autoDialInterval: init.connectionManager.autoDialInterval
}))
Expand Down
3 changes: 0 additions & 3 deletions test/circuit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export function createNodeOptions (...overrides: Libp2pOptions[]): Libp2pOptions
return createBaseOptions({
addresses: {
listen: [listenAddr]
},
connectionManager: {
autoDial: false
}
}, ...overrides)
}
Expand Down
2 changes: 0 additions & 2 deletions test/dialing/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('Dialing (resolvable addresses)', () => {
listen: [`${relayAddr.toString()}/p2p-circuit`]
},
connectionManager: {
autoDial: false,
resolvers: {
dnsaddr: resolver
}
Expand All @@ -62,7 +61,6 @@ describe('Dialing (resolvable addresses)', () => {
listen: [`${relayAddr.toString()}/p2p-circuit`]
},
connectionManager: {
autoDial: false,
resolvers: {
dnsaddr: resolver
}
Expand Down
11 changes: 1 addition & 10 deletions test/peer-discovery/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ describe('peer discovery scenarios', () => {
listenAddr.toString()
]
},
connectionManager: {
autoDial: false
},
peerDiscovery: [
bootstrap({
list: bootstrappers
Expand Down Expand Up @@ -122,10 +119,7 @@ describe('peer discovery scenarios', () => {
interval: 200, // discover quickly
serviceTag
})
],
connectionManager: {
autoDial: false
}
]
})

libp2p = await createLibp2pNode(getConfig(peerId))
Expand Down Expand Up @@ -170,9 +164,6 @@ describe('peer discovery scenarios', () => {
listenAddr.toString()
]
},
connectionManager: {
autoDial: false
},
dht: kadDHT()
})

Expand Down

0 comments on commit da3526c

Please sign in to comment.