Skip to content

Commit da3526c

Browse files
achingbrainmaschad
andauthored
fix!: remove connection manager autodial option (#1626)
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 <[email protected]>
1 parent d6c8601 commit da3526c

File tree

13 files changed

+44
-64
lines changed

13 files changed

+44
-64
lines changed

doc/CONFIGURATION.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,7 @@ const node = await createLibp2p({
284284
],
285285
interval: 2000
286286
)
287-
],
288-
connectionManager: {
289-
autoDial: true // Auto connect to discovered peers (limited by ConnectionManager minConnections)
290-
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
291-
// The associated object, will be passed to the service when it is instantiated.
292-
}
287+
]
293288
})
294289
```
295290

doc/GETTING_STARTED.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,7 @@ const node = await createLibp2p({
206206
bootstrap({
207207
list: bootstrapMultiaddrs, // provide array of multiaddrs
208208
})
209-
],
210-
connectionManager: {
211-
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
212-
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
213-
// The associated object, will be passed to the service when it is instantiated.
214-
}
209+
]
215210
})
216211

217212
node.addEventListener('peer:discovery', (evt) => {

doc/PEER_DISCOVERY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* To ensure reasonable resource usage, discovered peers are not connected to automatically
2323
* Applications should lisen for the `peer:connect` event if they wish to take a specific action when new connections are established
2424
* 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.
25-
* Applications can disable this behaviour via the `connectionManager.autoDial` config property, and handle increasing the current number of connections themselves
25+
* 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.
2626

2727
## Scenarios
2828

doc/migrations/v0.42-v0.43.md

+38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A migration guide for refactoring your application code from libp2p v0.42.x to v
55
## Table of Contents <!-- omit in toc -->
66

77
- [Circuit Relay v2](#circuit-relay-v2)
8+
- [Connection manager autodial](#connection-manager-autodial)
89

910
## Circuit Relay v2
1011

@@ -77,3 +78,40 @@ const node = await createLibp2p({
7778
```
7879

7980
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.
81+
82+
## Connection manager autodial
83+
84+
The `autoDial` configutation option has been removed from the connection manager.
85+
86+
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`.
87+
88+
Instead, just set `minConnections` to `0` if you don't want to keep a minimum number of connections open.
89+
90+
**Before**
91+
92+
```js
93+
import { createLibp2p } from 'libp2p'
94+
95+
const node = await createLibp2p({
96+
// ... other options
97+
connectionManager: {
98+
autoDial: false,
99+
minConnections: 10,
100+
maxConnections: 100
101+
}
102+
}
103+
```
104+
105+
**After**
106+
107+
```js
108+
import { createLibp2p } from 'libp2p'
109+
110+
const node = await createLibp2p({
111+
// ... other options
112+
connectionManager: {
113+
minConnections: 0,
114+
maxConnections: 100
115+
}
116+
}
117+
```

examples/discovery-mechanisms/1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import bootstrapers from './bootstrappers.js'
3030

3131
node.addEventListener('peer:discovery', (evt) => {
3232
const peer = evt.detail
33-
// No need to dial, autoDial is on
33+
3434
console.log('Discovered:', peer.id.toString())
3535
})
3636
})();

examples/transports/4.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ const createNode = async (addresses = []) => {
3535
})
3636
],
3737
connectionEncryption: [noise()],
38-
streamMuxers: [mplex()],
39-
connectionManager: {
40-
// Disable autoDial as it would fail because we are using a self-signed cert.
41-
// `dialProtocol` does not fail because we pass `rejectUnauthorized: false`.
42-
autoDial: false
43-
}
38+
streamMuxers: [mplex()]
4439
})
4540

4641
return node

src/config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const DefaultConfig: Partial<Libp2pInit> = {
2121
connectionManager: {
2222
maxConnections: 300,
2323
minConnections: 50,
24-
autoDial: true,
2524
autoDialInterval: 10000,
2625
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
2726
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,

src/connection-manager/auto-dialler.ts

-16
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
1010
const log = logger('libp2p:connection-manager:auto-dialler')
1111

1212
export interface AutoDiallerInit {
13-
/**
14-
* Should preemptively guarantee connections are above the low watermark
15-
*/
16-
enabled?: boolean
17-
1813
/**
1914
* The minimum number of connections to avoid pruning
2015
*/
@@ -33,7 +28,6 @@ export interface AutoDiallerComponents {
3328
}
3429

3530
const defaultOptions: Partial<AutoDiallerInit> = {
36-
enabled: true,
3731
minConnections: 0,
3832
autoDialInterval: 10000
3933
}
@@ -66,11 +60,6 @@ export class AutoDialler implements Startable {
6660
* Starts the auto dialer
6761
*/
6862
async start (): Promise<void> {
69-
if (!this.options.enabled) {
70-
log('not enabled')
71-
return
72-
}
73-
7463
this.running = true
7564

7665
void this._autoDial().catch(err => {
@@ -84,11 +73,6 @@ export class AutoDialler implements Startable {
8473
* Stops the auto dialler
8574
*/
8675
async stop (): Promise<void> {
87-
if (!this.options.enabled) {
88-
log('not enabled')
89-
return
90-
}
91-
9276
this.running = false
9377

9478
if (this.autoDialTimeout != null) {

src/connection-manager/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ export interface ConnectionManagerConfig {
4444
*/
4545
pollInterval?: number
4646

47-
/**
48-
* If true, try to connect to all discovered peers up to the connection manager limit
49-
*/
50-
autoDial?: boolean
51-
5247
/**
5348
* How long to wait between attempting to keep our number of concurrent connections
5449
* above minConnections

src/libp2p.ts

-7
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
157157
// update our peer record when addresses change
158158
this.configureComponent(new PeerRecordUpdater(this.components))
159159

160-
this.configureComponent(new AutoDialler(this.components, {
161-
enabled: init.connectionManager.autoDial,
162-
minConnections: init.connectionManager.minConnections,
163-
autoDialInterval: init.connectionManager.autoDialInterval
164-
}))
165-
166160
// Create keychain
167161
const keychainOpts = DefaultKeyChain.generateOptions()
168162
this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
@@ -237,7 +231,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
237231
}))
238232

239233
this.configureComponent(new AutoDialler(this.components, {
240-
enabled: init.connectionManager.autoDial,
241234
minConnections: init.connectionManager.minConnections,
242235
autoDialInterval: init.connectionManager.autoDialInterval
243236
}))

test/circuit/utils.ts

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ export function createNodeOptions (...overrides: Libp2pOptions[]): Libp2pOptions
2020
return createBaseOptions({
2121
addresses: {
2222
listen: [listenAddr]
23-
},
24-
connectionManager: {
25-
autoDial: false
2623
}
2724
}, ...overrides)
2825
}

test/dialing/resolver.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ describe('Dialing (resolvable addresses)', () => {
4949
listen: [`${relayAddr.toString()}/p2p-circuit`]
5050
},
5151
connectionManager: {
52-
autoDial: false,
5352
resolvers: {
5453
dnsaddr: resolver
5554
}
@@ -62,7 +61,6 @@ describe('Dialing (resolvable addresses)', () => {
6261
listen: [`${relayAddr.toString()}/p2p-circuit`]
6362
},
6463
connectionManager: {
65-
autoDial: false,
6664
resolvers: {
6765
dnsaddr: resolver
6866
}

test/peer-discovery/index.node.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ describe('peer discovery scenarios', () => {
7474
listenAddr.toString()
7575
]
7676
},
77-
connectionManager: {
78-
autoDial: false
79-
},
8077
peerDiscovery: [
8178
bootstrap({
8279
list: bootstrappers
@@ -122,10 +119,7 @@ describe('peer discovery scenarios', () => {
122119
interval: 200, // discover quickly
123120
serviceTag
124121
})
125-
],
126-
connectionManager: {
127-
autoDial: false
128-
}
122+
]
129123
})
130124

131125
libp2p = await createLibp2pNode(getConfig(peerId))
@@ -170,9 +164,6 @@ describe('peer discovery scenarios', () => {
170164
listenAddr.toString()
171165
]
172166
},
173-
connectionManager: {
174-
autoDial: false
175-
},
176167
dht: kadDHT()
177168
})
178169

0 commit comments

Comments
 (0)