Skip to content

Commit 4ca481b

Browse files
committed
fix: make dialer configurable (#521)
docs: update configuration and api docs
1 parent 5d7ee50 commit 4ca481b

File tree

6 files changed

+62
-7
lines changed

6 files changed

+62
-7
lines changed

doc/API.md

+3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ Creates an instance of Libp2p.
4646
| options | `Object` | libp2p options |
4747
| options.modules | `Array<Object>` | libp2p modules to use |
4848
| [options.config] | `Object` | libp2p modules configuration and core configuration |
49+
| [options.connectionManager] | `Object` | libp2p Connection Manager configuration |
4950
| [options.datastore] | `Object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
51+
| [options.dialer] | `Object` | libp2p Dialer configuration
52+
| [options.metrics] | `Object` | libp2p Metrics configuration
5053
| [options.peerInfo] | [PeerInfo](https://github.com/libp2p/js-peer-info) | peerInfo instance (it will be created if not provided) |
5154

5255
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).

doc/CONFIGURATION.md

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Customizing DHT](#customizing-dht)
2121
- [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing)
2222
- [Setup with Relay](#setup-with-relay)
23+
- [Configuring Dialing](#configuring-dialing)
2324
- [Configuring Connection Manager](#configuring-connection-manager)
2425
- [Configuring Metrics](#configuring-metrics)
2526
- [Configuration examples](#configuration-examples)
@@ -414,6 +415,29 @@ const node = await Libp2p.create({
414415
})
415416
```
416417

418+
#### Configuring Dialing
419+
420+
Dialing in libp2p can be configured to limit the rate of dialing, and how long dials are allowed to take. The below configuration example shows the default values for the dialer.
421+
422+
```js
423+
const Libp2p = require('libp2p')
424+
const TCP = require('libp2p-tcp')
425+
const MPLEX = require('libp2p-mplex')
426+
const SECIO = require('libp2p-secio')
427+
428+
const node = await Libp2p.create({
429+
modules: {
430+
transport: [TCP],
431+
streamMuxer: [MPLEX],
432+
connEncryption: [SECIO]
433+
},
434+
dialer: {
435+
maxParallelDials: 100, // How many multiaddrs we can dial in parallel
436+
maxDialsPerPeer: 4, // How many multiaddrs we can dial per peer, in parallel
437+
dialTimeout: 30e3 // 30 second dial timeout per peer
438+
}
439+
```
440+
417441
#### Configuring Connection Manager
418442
419443
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md). The configuration values below show the defaults for Connection Manager. See [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md#options) for a full description of the parameters.

src/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
'use strict'
22

33
const mergeOptions = require('merge-options')
4+
const Constants = require('./constants')
45

56
const DefaultConfig = {
67
connectionManager: {
78
minPeers: 25
89
},
10+
dialer: {
11+
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
12+
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
13+
dialTimeout: Constants.DIAL_TIMEOUT
14+
},
915
metrics: {
1016
enabled: false
1117
},

src/constants.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
'use strict'
22

33
module.exports = {
4-
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
5-
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
64
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
7-
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
85
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
96
MAX_PER_PEER_DIALS: 4, // Allowed parallel dials per DialRequest
10-
QUARTER_HOUR: 15 * 60e3,
11-
PRIORITY_HIGH: 10,
12-
PRIORITY_LOW: 20,
137
METRICS: {
148
computeThrottleMaxQueueSize: 1000,
159
computeThrottleTimeout: 2000,

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ class Libp2p extends EventEmitter {
107107

108108
this.dialer = new Dialer({
109109
transportManager: this.transportManager,
110-
peerStore: this.peerStore
110+
peerStore: this.peerStore,
111+
concurrency: this._options.dialer.maxParallelDials,
112+
perPeerLimit: this._options.dialer.maxDialsPerPeer,
113+
timeout: this._options.dialer.dialTimeout
111114
})
112115

113116
this._modules.transport.forEach((Transport) => {

test/dialing/direct.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,35 @@ describe('Dialing (direct, WebSockets)', () => {
273273
})
274274

275275
expect(libp2p.dialer).to.exist()
276+
expect(libp2p.dialer.concurrency).to.equal(Constants.MAX_PARALLEL_DIALS)
277+
expect(libp2p.dialer.perPeerLimit).to.equal(Constants.MAX_PER_PEER_DIALS)
278+
expect(libp2p.dialer.timeout).to.equal(Constants.DIAL_TIMEOUT)
276279
// Ensure the dialer also has the transport manager
277280
expect(libp2p.transportManager).to.equal(libp2p.dialer.transportManager)
278281
})
279282

283+
it('should be able to override dialer options', async () => {
284+
const config = {
285+
peerInfo,
286+
modules: {
287+
transport: [Transport],
288+
streamMuxer: [Muxer],
289+
connEncryption: [Crypto]
290+
},
291+
dialer: {
292+
maxParallelDials: 10,
293+
maxDialsPerPeer: 1,
294+
dialTimeout: 1e3 // 30 second dial timeout per peer
295+
}
296+
}
297+
libp2p = await Libp2p.create(config)
298+
299+
expect(libp2p.dialer).to.exist()
300+
expect(libp2p.dialer.concurrency).to.equal(config.dialer.maxParallelDials)
301+
expect(libp2p.dialer.perPeerLimit).to.equal(config.dialer.maxDialsPerPeer)
302+
expect(libp2p.dialer.timeout).to.equal(config.dialer.dialTimeout)
303+
})
304+
280305
it('should use the dialer for connecting', async () => {
281306
libp2p = new Libp2p({
282307
peerInfo,

0 commit comments

Comments
 (0)