Skip to content

Commit 73b87c5

Browse files
authored
fix(libp2p): only dial one address at a time for peers (#2028)
The observation for dialing is that for a given peer with multiple addresses, for a given class of address (e.g. public or private), either the first dial succeeds or they all fail, so there's not much point dialing all addresses together since in either case you waste resources either cancelling redundant dials or dialing addresses that fail. We can't do much about dialing addresses that fail but we can reduce the number of redundant dials so with this change we only ever dial one multiaddr at a time.
1 parent 46dc3ce commit 73b87c5

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
export * from './constants.defaults.js'
22

3-
/**
4-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
5-
*/
6-
export const MAX_PARALLEL_DIALS = 10
7-
83
/**
94
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
105
*/
@@ -16,6 +11,11 @@ export const MIN_CONNECTIONS = 5
1611
export const MAX_CONNECTIONS = 100
1712

1813
/**
19-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
14+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
15+
*/
16+
export const MAX_PARALLEL_DIALS = 50
17+
18+
/**
19+
* @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialPeerRetryThreshold
2020
*/
21-
export const AUTO_DIAL_CONCURRENCY = 10
21+
export const AUTO_DIAL_PEER_RETRY_THRESHOLD = 1000 * 60 * 7

packages/libp2p/src/connection-manager/constants.defaults.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export const MAX_PEER_ADDRS_TO_DIAL = 25
1616
/**
1717
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
1818
*/
19-
export const MAX_PARALLEL_DIALS_PER_PEER = 10
19+
export const MAX_PARALLEL_DIALS_PER_PEER = 1
2020

2121
/**
2222
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
2323
*/
2424
export const AUTO_DIAL_INTERVAL = 5000
2525

26+
/**
27+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
28+
*/
29+
export const AUTO_DIAL_CONCURRENCY = 25
30+
2631
/**
2732
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
2833
*/
@@ -33,11 +38,6 @@ export const AUTO_DIAL_PRIORITY = 0
3338
*/
3439
export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
3540

36-
/**
37-
* @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialPeerRetryThreshold
38-
*/
39-
export const AUTO_DIAL_PEER_RETRY_THRESHOLD = 1000 * 60
40-
4141
/**
4242
* @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialDiscoveredPeersDebounce
4343
*/
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
export * from './constants.defaults.js'
22

3-
/**
4-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
5-
*/
6-
export const MAX_PARALLEL_DIALS = 100
7-
83
/**
94
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
105
*/
@@ -16,6 +11,11 @@ export const MIN_CONNECTIONS = 50
1611
export const MAX_CONNECTIONS = 300
1712

1813
/**
19-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
14+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
15+
*/
16+
export const MAX_PARALLEL_DIALS = 100
17+
18+
/**
19+
* @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialPeerRetryThreshold
2020
*/
21-
export const AUTO_DIAL_CONCURRENCY = 25
21+
export const AUTO_DIAL_PEER_RETRY_THRESHOLD = 1000 * 60

packages/libp2p/src/connection-manager/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { codes } from '../errors.js'
1010
import { getPeerAddress } from '../get-peer.js'
1111
import { AutoDial } from './auto-dial.js'
1212
import { ConnectionPruner } from './connection-pruner.js'
13-
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
13+
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
1414
import { DialQueue } from './dial-queue.js'
1515
import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions } from '@libp2p/interface'
1616
import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection'
@@ -30,14 +30,14 @@ const DEFAULT_DIAL_PRIORITY = 50
3030
export interface ConnectionManagerInit {
3131
/**
3232
* The maximum number of connections libp2p is willing to have before it starts
33-
* pruning connections to reduce resource usage. (default: 300)
33+
* pruning connections to reduce resource usage. (default: 300, 100 in browsers)
3434
*/
3535
maxConnections?: number
3636

3737
/**
3838
* The minimum number of connections below which libp2p will start to dial peers
3939
* from the peer book. Setting this to 0 effectively disables this behaviour.
40-
* (default: 50)
40+
* (default: 50, 5 in browsers)
4141
*/
4242
minConnections?: number
4343

@@ -68,7 +68,7 @@ export interface ConnectionManagerInit {
6868

6969
/**
7070
* When we've failed to dial a peer, do not autodial them again within this
71-
* number of ms. (default: 1 minute)
71+
* number of ms. (default: 1 minute, 7 minutes in browsers)
7272
*/
7373
autoDialPeerRetryThreshold?: number
7474

@@ -88,15 +88,15 @@ export interface ConnectionManagerInit {
8888

8989
/**
9090
* The maximum number of dials across all peers to execute in parallel.
91-
* (default: 100)
91+
* (default: 100, 50 in browsers)
9292
*/
9393
maxParallelDials?: number
9494

9595
/**
9696
* To prevent individual peers with large amounts of multiaddrs swamping the
9797
* dial queue, this value controls how many addresses to dial in parallel per
9898
* peer. So for example if two peers have 10 addresses and this value is set
99-
* at 5, we will dial 5 addresses from each at a time. (default: 10)
99+
* at 5, we will dial 5 addresses from each at a time. (default: 1)
100100
*/
101101
maxParallelDialsPerPeer?: number
102102

@@ -257,6 +257,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
257257
addressSorter: init.addressSorter ?? publicAddressesFirst,
258258
maxParallelDials: init.maxParallelDials ?? MAX_PARALLEL_DIALS,
259259
maxPeerAddrsToDial: init.maxPeerAddrsToDial ?? MAX_PEER_ADDRS_TO_DIAL,
260+
maxParallelDialsPerPeer: init.maxParallelDialsPerPeer ?? MAX_PARALLEL_DIALS_PER_PEER,
260261
dialTimeout: init.dialTimeout ?? DIAL_TIMEOUT,
261262
resolvers: init.resolvers ?? {
262263
dnsaddr: dnsaddrResolver

packages/libp2p/test/circuit-relay/relay.node.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,11 @@ describe('circuit-relay', () => {
470470

471471
it('should not add listener to a already relayed connection', async () => {
472472
// Relay 1 discovers Relay 3 and connect
473-
await relay1.peerStore.merge(relay3.peerId, {
474-
multiaddrs: relay3.getMultiaddrs()
475-
})
476-
await relay1.dial(relay3.peerId)
473+
await relay1.dial(relay3.getMultiaddrs())
477474
await usingAsRelay(relay1, relay3)
478475

479476
// Relay 2 discovers Relay 3 and connect
480-
await relay2.peerStore.merge(relay3.peerId, {
481-
multiaddrs: relay3.getMultiaddrs()
482-
})
483-
await relay2.dial(relay3.peerId)
477+
await relay2.dial(relay3.getMultiaddrs())
484478
await usingAsRelay(relay2, relay3)
485479

486480
// Relay 1 discovers Relay 2 relayed multiaddr via Relay 3

packages/libp2p/test/connection-manager/dial-queue.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ describe('dial queue', () => {
199199
const controller = new AbortController()
200200

201201
dialer = new DialQueue(components, {
202-
maxParallelDials: 2
202+
maxParallelDials: 2,
203+
maxParallelDialsPerPeer: 10
203204
})
204205

205206
components.transportManager.transportForMultiaddr.returns(stubInterface<Transport>())
@@ -268,7 +269,8 @@ describe('dial queue', () => {
268269
})
269270

270271
dialer = new DialQueue(components, {
271-
maxParallelDials: 50
272+
maxParallelDials: 50,
273+
maxParallelDialsPerPeer: 10
272274
})
273275

274276
await expect(dialer.dial(Object.keys(actions).map(str => multiaddr(str)))).to.eventually.equal(connection2)

packages/libp2p/test/connection-manager/direct.node.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ describe('dialing (direct, TCP)', () => {
230230
]
231231

232232
const dialer = new DialQueue(localComponents, {
233-
maxParallelDials: 2
233+
maxParallelDials: 2,
234+
maxParallelDialsPerPeer: 10
234235
})
235236

236237
const deferredDial = pDefer<Connection>()
@@ -268,7 +269,9 @@ describe('dialing (direct, TCP)', () => {
268269
multiaddrs: addrs
269270
})
270271

271-
const dialer = new DialQueue(localComponents)
272+
const dialer = new DialQueue(localComponents, {
273+
maxParallelDialsPerPeer: 10
274+
})
272275

273276
const transportManagerDialStub = sinon.stub(localTM, 'dial')
274277
transportManagerDialStub.callsFake(async (ma) => {

packages/libp2p/test/connection-manager/direct.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ describe('dialing (direct, WebSockets)', () => {
198198

199199
connectionManager = new DefaultConnectionManager(localComponents, {
200200
addressSorter: publicAddressesFirstSpy,
201-
maxParallelDials: 3
201+
maxParallelDials: 3,
202+
maxParallelDialsPerPeer: 3
202203
})
203204
await connectionManager.start()
204205

@@ -272,7 +273,9 @@ describe('dialing (direct, WebSockets)', () => {
272273
multiaddrs: addrs
273274
})
274275

275-
connectionManager = new DefaultConnectionManager(localComponents)
276+
connectionManager = new DefaultConnectionManager(localComponents, {
277+
maxParallelDialsPerPeer: 10
278+
})
276279
await connectionManager.start()
277280

278281
const transactionManagerDialStub = sinon.stub(localTM, 'dial')

0 commit comments

Comments
 (0)