libp2p 0.40.0 → 0.41.0-1147550
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -6
- package/dist/index.min.js +58 -0
- package/dist/src/address-manager/index.d.ts +8 -0
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +1 -1
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/index.d.ts +11 -1
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +1 -1
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +1 -12
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +83 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +96 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection-manager/dialer/index.d.ts +13 -14
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +82 -62
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +11 -38
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +35 -65
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/fetch/index.d.ts +14 -0
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +29 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/get-peer.d.ts +2 -2
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +2 -5
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +129 -139
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +41 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +23 -0
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +23 -0
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/keychain/index.d.ts +3 -3
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +4 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +12 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +5 -1
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/pnet/index.d.ts +23 -0
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +23 -0
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.d.ts +3 -2
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +7 -10
- package/dist/src/pnet/key-generator.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -0
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +25 -0
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +1 -13
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +2 -16
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +46 -59
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +16 -19
- package/src/address-manager/index.ts +11 -2
- package/src/circuit/README.md +6 -6
- package/src/circuit/index.ts +13 -1
- package/src/circuit/transport.ts +1 -1
- package/src/config.ts +1 -12
- package/src/connection/index.ts +171 -0
- package/src/connection-manager/dialer/index.ts +100 -75
- package/src/connection-manager/index.ts +58 -114
- package/src/fetch/README.md +7 -2
- package/src/fetch/index.ts +30 -3
- package/src/get-peer.ts +3 -7
- package/src/index.ts +141 -152
- package/src/insecure/index.ts +24 -0
- package/src/keychain/index.ts +3 -3
- package/src/libp2p.ts +17 -6
- package/src/nat-manager.ts +6 -1
- package/src/peer-routing.ts +2 -2
- package/src/pnet/README.md +31 -29
- package/src/pnet/index.ts +24 -0
- package/src/pnet/key-generator.ts +7 -12
- package/src/registrar.ts +29 -2
- package/src/transport-manager.ts +2 -17
- package/src/upgrader.ts +51 -64
- package/src/version.ts +1 -1
- package/dist/src/metrics/index.d.ts +0 -93
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -234
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/moving-average.d.ts +0 -14
- package/dist/src/metrics/moving-average.d.ts.map +0 -1
- package/dist/src/metrics/moving-average.js +0 -40
- package/dist/src/metrics/moving-average.js.map +0 -1
- package/dist/src/metrics/stats.d.ts +0 -87
- package/dist/src/metrics/stats.d.ts.map +0 -1
- package/dist/src/metrics/stats.js +0 -183
- package/dist/src/metrics/stats.js.map +0 -1
- package/src/metrics/index.ts +0 -311
- package/src/metrics/moving-average.ts +0 -53
- package/src/metrics/stats.ts +0 -238
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger';
|
|
2
|
-
import all from 'it-all';
|
|
3
|
-
import filter from 'it-filter';
|
|
4
|
-
import { pipe } from 'it-pipe';
|
|
5
2
|
import errCode from 'err-code';
|
|
6
|
-
import { multiaddr, resolvers } from '@multiformats/multiaddr';
|
|
3
|
+
import { isMultiaddr, multiaddr, resolvers } from '@multiformats/multiaddr';
|
|
7
4
|
import { TimeoutController } from 'timeout-abort-controller';
|
|
8
|
-
import { AbortError } from '@libp2p/interfaces/errors';
|
|
9
5
|
import { anySignal } from 'any-signal';
|
|
10
6
|
import { setMaxListeners } from 'events';
|
|
11
7
|
import { DialRequest } from './dial-request.js';
|
|
@@ -13,13 +9,9 @@ import { publicAddressesFirst } from '@libp2p/utils/address-sort';
|
|
|
13
9
|
import { trackedMap } from '@libp2p/tracked-map';
|
|
14
10
|
import { codes } from '../../errors.js';
|
|
15
11
|
import { DIAL_TIMEOUT, MAX_PARALLEL_DIALS, MAX_PER_PEER_DIALS, MAX_ADDRS_TO_DIAL } from '../../constants.js';
|
|
12
|
+
import { isPeerId } from '@libp2p/interface-peer-id';
|
|
16
13
|
import { getPeer } from '../../get-peer.js';
|
|
17
|
-
import sort from 'it-sort';
|
|
18
|
-
import map from 'it-map';
|
|
19
14
|
const log = logger('libp2p:dialer');
|
|
20
|
-
const METRICS_COMPONENT = 'dialler';
|
|
21
|
-
const METRICS_PENDING_DIALS = 'pending-dials';
|
|
22
|
-
const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets';
|
|
23
15
|
export class DefaultDialer {
|
|
24
16
|
constructor(components, init = {}) {
|
|
25
17
|
this.started = false;
|
|
@@ -30,13 +22,11 @@ export class DefaultDialer {
|
|
|
30
22
|
this.tokens = [...new Array(init.maxParallelDials ?? MAX_PARALLEL_DIALS)].map((_, index) => index);
|
|
31
23
|
this.components = components;
|
|
32
24
|
this.pendingDials = trackedMap({
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
metrics: init.metrics
|
|
25
|
+
name: 'libp2p_dialler_pending_dials',
|
|
26
|
+
metrics: components.metrics
|
|
36
27
|
});
|
|
37
28
|
this.pendingDialTargets = trackedMap({
|
|
38
|
-
|
|
39
|
-
metric: METRICS_PENDING_DIAL_TARGETS,
|
|
29
|
+
name: 'libp2p_dialler_pending_dial_targets',
|
|
40
30
|
metrics: components.metrics
|
|
41
31
|
});
|
|
42
32
|
for (const [key, value] of Object.entries(init.resolvers ?? {})) {
|
|
@@ -64,7 +54,7 @@ export class DefaultDialer {
|
|
|
64
54
|
}
|
|
65
55
|
this.pendingDials.clear();
|
|
66
56
|
for (const pendingTarget of this.pendingDialTargets.values()) {
|
|
67
|
-
pendingTarget.
|
|
57
|
+
pendingTarget.abort();
|
|
68
58
|
}
|
|
69
59
|
this.pendingDialTargets.clear();
|
|
70
60
|
}
|
|
@@ -73,8 +63,8 @@ export class DefaultDialer {
|
|
|
73
63
|
* The dial to the first address that is successfully able to upgrade a connection
|
|
74
64
|
* will be used.
|
|
75
65
|
*/
|
|
76
|
-
async dial(
|
|
77
|
-
const { id, multiaddrs } = getPeer(
|
|
66
|
+
async dial(peerIdOrMultiaddr, options = {}) {
|
|
67
|
+
const { id, multiaddrs } = getPeer(peerIdOrMultiaddr);
|
|
78
68
|
if (this.components.peerId.equals(id)) {
|
|
79
69
|
throw errCode(new Error('Tried to dial self'), codes.ERR_DIALED_SELF);
|
|
80
70
|
}
|
|
@@ -87,11 +77,31 @@ export class DefaultDialer {
|
|
|
87
77
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED);
|
|
88
78
|
}
|
|
89
79
|
log('creating dial target for %p', id);
|
|
90
|
-
|
|
80
|
+
// resolving multiaddrs can involve dns lookups so allow them to be aborted
|
|
81
|
+
const controller = new AbortController();
|
|
82
|
+
const controllerId = randomId();
|
|
83
|
+
this.pendingDialTargets.set(controllerId, controller);
|
|
84
|
+
let signal = controller.signal;
|
|
85
|
+
// merge with the passed signal, if any
|
|
86
|
+
if (options.signal != null) {
|
|
87
|
+
signal = anySignal([signal, options.signal]);
|
|
88
|
+
}
|
|
89
|
+
let dialTarget;
|
|
90
|
+
try {
|
|
91
|
+
dialTarget = await this._createDialTarget(peerIdOrMultiaddr, {
|
|
92
|
+
...options,
|
|
93
|
+
signal
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
// done resolving the multiaddrs so remove the abort controller
|
|
98
|
+
this.pendingDialTargets.delete(controllerId);
|
|
99
|
+
}
|
|
91
100
|
if (dialTarget.addrs.length === 0) {
|
|
92
101
|
throw errCode(new Error('The dial request has no valid addresses'), codes.ERR_NO_VALID_ADDRESSES);
|
|
93
102
|
}
|
|
94
|
-
|
|
103
|
+
// try to join an in-flight dial for this peer if one is available
|
|
104
|
+
const pendingDial = this.pendingDials.get(dialTarget.id.toString()) ?? this._createPendingDial(dialTarget, options);
|
|
95
105
|
try {
|
|
96
106
|
const connection = await pendingDial.promise;
|
|
97
107
|
log('dial succeeded to %s', dialTarget.id);
|
|
@@ -110,61 +120,59 @@ export class DefaultDialer {
|
|
|
110
120
|
pendingDial.destroy();
|
|
111
121
|
}
|
|
112
122
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Connects to a given `peer` by dialing all of its known addresses.
|
|
115
|
-
* The dial to the first address that is successfully able to upgrade a connection
|
|
116
|
-
* will be used.
|
|
117
|
-
*/
|
|
118
|
-
async _createCancellableDialTarget(peer, options) {
|
|
119
|
-
// Make dial target promise cancellable
|
|
120
|
-
const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString()}${Date.now()}`;
|
|
121
|
-
const cancellablePromise = new Promise((resolve, reject) => {
|
|
122
|
-
this.pendingDialTargets.set(id, { resolve, reject });
|
|
123
|
-
});
|
|
124
|
-
try {
|
|
125
|
-
const dialTarget = await Promise.race([
|
|
126
|
-
this._createDialTarget(peer, options),
|
|
127
|
-
cancellablePromise
|
|
128
|
-
]);
|
|
129
|
-
return dialTarget;
|
|
130
|
-
}
|
|
131
|
-
finally {
|
|
132
|
-
this.pendingDialTargets.delete(id);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
123
|
/**
|
|
136
124
|
* Creates a DialTarget. The DialTarget is used to create and track
|
|
137
125
|
* the DialRequest to a given peer.
|
|
138
|
-
*
|
|
126
|
+
*
|
|
127
|
+
* If a multiaddr is received it should be the only address attempted.
|
|
128
|
+
*
|
|
139
129
|
* Multiaddrs not supported by the available transports will be filtered out.
|
|
140
130
|
*/
|
|
141
|
-
async _createDialTarget(
|
|
131
|
+
async _createDialTarget(peerIdOrMultiaddr, options) {
|
|
142
132
|
const _resolve = this._resolve.bind(this);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
// Multiaddrs not supported by the available transports will be filtered out.
|
|
153
|
-
(source) => filter(source, (ma) => Boolean(this.components.transportManager.transportForMultiaddr(ma))), (source) => map(source, (ma) => {
|
|
154
|
-
if (peer.toString() === ma.getPeerId()) {
|
|
155
|
-
return ma;
|
|
156
|
-
}
|
|
157
|
-
return ma.encapsulate(`/p2p/${peer.toString()}`);
|
|
158
|
-
}), async (source) => await all(source));
|
|
133
|
+
let addrs = isMultiaddr(peerIdOrMultiaddr) ? [peerIdOrMultiaddr] : await this._loadAddresses(peerIdOrMultiaddr);
|
|
134
|
+
addrs = (await Promise.all(addrs.map(async (ma) => await _resolve(ma, options))))
|
|
135
|
+
.flat()
|
|
136
|
+
// Multiaddrs not supported by the available transports will be filtered out.
|
|
137
|
+
.filter(ma => Boolean(this.components.transportManager.transportForMultiaddr(ma)));
|
|
138
|
+
// deduplicate addresses
|
|
139
|
+
addrs = [...new Set(addrs)];
|
|
159
140
|
if (addrs.length > this.maxAddrsToDial) {
|
|
160
|
-
await this.components.peerStore.delete(peer);
|
|
161
141
|
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES);
|
|
162
142
|
}
|
|
143
|
+
const peerId = isPeerId(peerIdOrMultiaddr) ? peerIdOrMultiaddr : undefined;
|
|
144
|
+
if (peerId != null) {
|
|
145
|
+
const peerIdMultiaddr = `/p2p/${peerId.toString()}`;
|
|
146
|
+
addrs = addrs.map(addr => {
|
|
147
|
+
const addressPeerId = addr.getPeerId();
|
|
148
|
+
if (addressPeerId == null || !peerId.equals(addressPeerId)) {
|
|
149
|
+
return addr.encapsulate(peerIdMultiaddr);
|
|
150
|
+
}
|
|
151
|
+
return addr;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
163
154
|
return {
|
|
164
|
-
id:
|
|
155
|
+
id: peerId == null ? randomId() : peerId.toString(),
|
|
165
156
|
addrs
|
|
166
157
|
};
|
|
167
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Loads a list of addresses from the peer store for the passed peer id
|
|
161
|
+
*/
|
|
162
|
+
async _loadAddresses(peer) {
|
|
163
|
+
const addresses = await this.components.peerStore.addressBook.get(peer);
|
|
164
|
+
return (await Promise.all(addresses.map(async (address) => {
|
|
165
|
+
const deny = await this.components.connectionGater.denyDialMultiaddr(peer, address.multiaddr);
|
|
166
|
+
if (deny) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return address;
|
|
170
|
+
})))
|
|
171
|
+
.filter(isTruthy)
|
|
172
|
+
// Sort addresses so, for example, we try certified public address first
|
|
173
|
+
.sort(this.addressSorter)
|
|
174
|
+
.map(address => address.multiaddr);
|
|
175
|
+
}
|
|
168
176
|
/**
|
|
169
177
|
* Creates a PendingDial that wraps the underlying DialRequest
|
|
170
178
|
*/
|
|
@@ -263,4 +271,16 @@ export class DefaultDialer {
|
|
|
263
271
|
}
|
|
264
272
|
}
|
|
265
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Type safe version of `list.filter(Boolean)`
|
|
276
|
+
*/
|
|
277
|
+
function isTruthy(e) {
|
|
278
|
+
return Boolean(e);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns a random string
|
|
282
|
+
*/
|
|
283
|
+
function randomId() {
|
|
284
|
+
return `${(parseInt(String(Math.random() * 1e9), 10)).toString()}${Date.now()}`;
|
|
285
|
+
}
|
|
266
286
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE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faces/startable';
|
|
5
5
|
import { PeerId } from '@libp2p/interface-peer-id';
|
|
6
6
|
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection';
|
|
7
|
-
import type { ConnectionManager, Dialer } from '@libp2p/interface-connection-manager';
|
|
7
|
+
import type { ConnectionManager, ConnectionManagerEvents, Dialer } from '@libp2p/interface-connection-manager';
|
|
8
8
|
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store';
|
|
9
|
-
import { Resolver } from '@multiformats/multiaddr';
|
|
9
|
+
import { Multiaddr, Resolver } from '@multiformats/multiaddr';
|
|
10
10
|
import type { Metrics } from '@libp2p/interface-metrics';
|
|
11
11
|
import type { Upgrader } from '@libp2p/interface-transport';
|
|
12
|
-
export interface
|
|
12
|
+
export interface ConnectionManagerConfig {
|
|
13
13
|
/**
|
|
14
|
-
* The maximum number of connections to
|
|
14
|
+
* The maximum number of connections libp2p is willing to have before it starts disconnecting. Defaults to `Infinity`
|
|
15
15
|
*/
|
|
16
16
|
maxConnections: number;
|
|
17
17
|
/**
|
|
18
|
-
* The minimum number of connections to
|
|
18
|
+
* The minimum number of connections below which libp2p not activate preemptive disconnections. Defaults to `0`.
|
|
19
19
|
*/
|
|
20
20
|
minConnections: number;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
maxData?: number;
|
|
25
|
-
/**
|
|
26
|
-
* The max outgoing data, per average interval to allow
|
|
27
|
-
*/
|
|
28
|
-
maxSentData?: number;
|
|
29
|
-
/**
|
|
30
|
-
* The max incoming data, per average interval to allow
|
|
31
|
-
*/
|
|
32
|
-
maxReceivedData?: number;
|
|
33
|
-
/**
|
|
34
|
-
* The upper limit the event loop can take to run
|
|
22
|
+
* Sets the maximum event loop delay (measured in milliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
|
35
23
|
*/
|
|
36
24
|
maxEventLoopDelay?: number;
|
|
37
25
|
/**
|
|
38
|
-
*
|
|
26
|
+
* Sets the poll interval (in milliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
|
|
39
27
|
*/
|
|
40
28
|
pollInterval?: number;
|
|
41
|
-
/**
|
|
42
|
-
* How often, in milliseconds, to compute averages
|
|
43
|
-
*/
|
|
44
|
-
movingAverageInterval?: number;
|
|
45
29
|
/**
|
|
46
30
|
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
47
31
|
*/
|
|
@@ -107,10 +91,6 @@ export interface ConnectionManagerInit {
|
|
|
107
91
|
*/
|
|
108
92
|
maxIncomingPendingConnections?: number;
|
|
109
93
|
}
|
|
110
|
-
export interface ConnectionManagerEvents {
|
|
111
|
-
'peer:connect': CustomEvent<PeerId>;
|
|
112
|
-
'peer:disconnect': CustomEvent<PeerId>;
|
|
113
|
-
}
|
|
114
94
|
export interface DefaultConnectionManagerComponents {
|
|
115
95
|
peerId: PeerId;
|
|
116
96
|
metrics?: Metrics;
|
|
@@ -118,6 +98,7 @@ export interface DefaultConnectionManagerComponents {
|
|
|
118
98
|
peerStore: PeerStore;
|
|
119
99
|
dialer: Dialer;
|
|
120
100
|
}
|
|
101
|
+
export type ConnectionManagerInit = ConnectionManagerConfig;
|
|
121
102
|
/**
|
|
122
103
|
* Responsible for managing known connections.
|
|
123
104
|
*/
|
|
@@ -126,8 +107,7 @@ export declare class DefaultConnectionManager extends EventEmitter<ConnectionMan
|
|
|
126
107
|
private readonly opts;
|
|
127
108
|
private readonly connections;
|
|
128
109
|
private started;
|
|
129
|
-
private
|
|
130
|
-
private readonly latencyMonitor;
|
|
110
|
+
private readonly latencyMonitor?;
|
|
131
111
|
private readonly startupReconnectTimeout;
|
|
132
112
|
private connectOnStartupController?;
|
|
133
113
|
private readonly dialTimeout;
|
|
@@ -135,7 +115,7 @@ export declare class DefaultConnectionManager extends EventEmitter<ConnectionMan
|
|
|
135
115
|
private readonly deny;
|
|
136
116
|
private readonly inboundConnectionRateLimiter;
|
|
137
117
|
private incomingPendingConnections;
|
|
138
|
-
constructor(components: DefaultConnectionManagerComponents, init:
|
|
118
|
+
constructor(components: DefaultConnectionManagerComponents, init: ConnectionManagerConfig);
|
|
139
119
|
isStarted(): boolean;
|
|
140
120
|
/**
|
|
141
121
|
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
@@ -152,13 +132,6 @@ export declare class DefaultConnectionManager extends EventEmitter<ConnectionMan
|
|
|
152
132
|
* Cleans up the connections
|
|
153
133
|
*/
|
|
154
134
|
_close(): Promise<void>;
|
|
155
|
-
/**
|
|
156
|
-
* Checks the libp2p metrics to determine if any values have exceeded
|
|
157
|
-
* the configured maximums.
|
|
158
|
-
*
|
|
159
|
-
* @private
|
|
160
|
-
*/
|
|
161
|
-
_checkMetrics(): Promise<void>;
|
|
162
135
|
onConnect(evt: CustomEvent<Connection>): void;
|
|
163
136
|
/**
|
|
164
137
|
* Tracks the incoming connection and check the connection limit
|
|
@@ -169,7 +142,7 @@ export declare class DefaultConnectionManager extends EventEmitter<ConnectionMan
|
|
|
169
142
|
*/
|
|
170
143
|
onDisconnect(evt: CustomEvent<Connection>): void;
|
|
171
144
|
getConnections(peerId?: PeerId): Connection[];
|
|
172
|
-
openConnection(
|
|
145
|
+
openConnection(peerIdOrMultiaddr: PeerId | Multiaddr, options?: AbortOptions): Promise<Connection>;
|
|
173
146
|
closeConnections(peerId: PeerId): Promise<void>;
|
|
174
147
|
/**
|
|
175
148
|
* Get all open connections with a peer
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,aAAa,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD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IAwFL,UAAU;IA6CV,UAAU;IAOhB;;OAEG;IACG,IAAI;IASV;;OAEG;IACG,MAAM;IAoBZ,SAAS,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAMvC;;OAEG;IACG,UAAU,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IA8B9C;;OAEG;IACH,YAAY,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAoB1C,cAAc,CAAE,MAAM,CAAC,EAAE,MAAM,GAAG,UAAU,EAAE;IAcxC,cAAc,CAAE,iBAAiB,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAiEvG,gBAAgB,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUtD;;OAEG;IACH,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,UAAU,EAAE;IAgBrC;;OAEG;IACH,iBAAiB,CAAE,GAAG,EAAE,WAAW,CAAC,aAAa,CAAC;IASlD;;OAEG;IACG,cAAc,CAAE,IAAI,EAAE,MAAM,qBAAqB,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,MAAU;IAe3F;;;OAGG;IACG,iBAAiB,CAAE,OAAO,EAAE,MAAM;IAiElC,wBAAwB,CAAE,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAiD9E,mBAAmB;CAGpB"}
|
|
@@ -2,34 +2,27 @@ import { logger } from '@libp2p/logger';
|
|
|
2
2
|
import errCode from 'err-code';
|
|
3
3
|
import mergeOptions from 'merge-options';
|
|
4
4
|
import { LatencyMonitor } from './latency-monitor.js';
|
|
5
|
-
// @ts-expect-error retimer does not have types
|
|
6
|
-
import retimer from 'retimer';
|
|
7
5
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events';
|
|
8
6
|
import { codes } from '../errors.js';
|
|
9
7
|
import { isPeerId } from '@libp2p/interface-peer-id';
|
|
10
8
|
import { setMaxListeners } from 'events';
|
|
11
9
|
import * as STATUS from '@libp2p/interface-connection/status';
|
|
12
|
-
import { multiaddr } from '@multiformats/multiaddr';
|
|
10
|
+
import { isMultiaddr, multiaddr } from '@multiformats/multiaddr';
|
|
13
11
|
import { PeerMap } from '@libp2p/peer-collections';
|
|
14
12
|
import { TimeoutController } from 'timeout-abort-controller';
|
|
15
13
|
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags';
|
|
16
14
|
import { RateLimiterMemory } from 'rate-limiter-flexible';
|
|
15
|
+
import { getPeer } from '../get-peer.js';
|
|
17
16
|
const log = logger('libp2p:connection-manager');
|
|
18
17
|
const defaultOptions = {
|
|
19
18
|
maxConnections: Infinity,
|
|
20
19
|
minConnections: 0,
|
|
21
|
-
maxData: Infinity,
|
|
22
|
-
maxSentData: Infinity,
|
|
23
|
-
maxReceivedData: Infinity,
|
|
24
20
|
maxEventLoopDelay: Infinity,
|
|
25
21
|
pollInterval: 2000,
|
|
26
22
|
autoDialInterval: 10000,
|
|
27
|
-
movingAverageInterval: 60000,
|
|
28
23
|
inboundConnectionThreshold: 5,
|
|
29
24
|
maxIncomingPendingConnections: 10
|
|
30
25
|
};
|
|
31
|
-
const METRICS_SYSTEM = 'libp2p';
|
|
32
|
-
const METRICS_COMPONENT = 'connection-manager';
|
|
33
26
|
const STARTUP_RECONNECT_TIMEOUT = 60000;
|
|
34
27
|
/**
|
|
35
28
|
* Responsible for managing known connections.
|
|
@@ -48,11 +41,12 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
48
41
|
*/
|
|
49
42
|
this.connections = new Map();
|
|
50
43
|
this.started = false;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
if (init.maxEventLoopDelay != null && init.maxEventLoopDelay > 0 && init.maxEventLoopDelay !== Infinity) {
|
|
45
|
+
this.latencyMonitor = new LatencyMonitor({
|
|
46
|
+
latencyCheckIntervalMs: init.pollInterval,
|
|
47
|
+
dataEmitIntervalMs: init.pollInterval
|
|
48
|
+
});
|
|
49
|
+
}
|
|
56
50
|
try {
|
|
57
51
|
// This emitter gets listened to a lot
|
|
58
52
|
setMaxListeners?.(Infinity, this);
|
|
@@ -78,16 +72,9 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
78
72
|
* only event loop and connection limits will be monitored.
|
|
79
73
|
*/
|
|
80
74
|
async start() {
|
|
81
|
-
if (this.components.metrics != null) {
|
|
82
|
-
this.timer = this.timer ?? retimer(this._checkMetrics, this.opts.pollInterval);
|
|
83
|
-
}
|
|
84
75
|
// track inbound/outbound connections
|
|
85
|
-
this.components.metrics?.
|
|
86
|
-
|
|
87
|
-
component: METRICS_COMPONENT,
|
|
88
|
-
metric: 'connections',
|
|
89
|
-
label: 'direction',
|
|
90
|
-
value: () => {
|
|
76
|
+
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_connections', {
|
|
77
|
+
calculate: () => {
|
|
91
78
|
const metric = {
|
|
92
79
|
inbound: 0,
|
|
93
80
|
outbound: 0
|
|
@@ -106,12 +93,9 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
106
93
|
}
|
|
107
94
|
});
|
|
108
95
|
// track total number of streams per protocol
|
|
109
|
-
this.components.metrics?.
|
|
110
|
-
system: METRICS_SYSTEM,
|
|
111
|
-
component: METRICS_COMPONENT,
|
|
112
|
-
metric: 'protocol-streams-total',
|
|
96
|
+
this.components.metrics?.registerMetricGroup('libp2p_protocol_streams_total', {
|
|
113
97
|
label: 'protocol',
|
|
114
|
-
|
|
98
|
+
calculate: () => {
|
|
115
99
|
const metric = {};
|
|
116
100
|
for (const conns of this.connections.values()) {
|
|
117
101
|
for (const conn of conns) {
|
|
@@ -125,12 +109,9 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
125
109
|
}
|
|
126
110
|
});
|
|
127
111
|
// track 90th percentile of streams per protocol
|
|
128
|
-
this.components.metrics?.
|
|
129
|
-
system: METRICS_SYSTEM,
|
|
130
|
-
component: METRICS_COMPONENT,
|
|
131
|
-
metric: 'protocol-streams-per-connection-90th-percentile',
|
|
112
|
+
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_protocol_streams_per_connection_90th_percentile', {
|
|
132
113
|
label: 'protocol',
|
|
133
|
-
|
|
114
|
+
calculate: () => {
|
|
134
115
|
const allStreams = {};
|
|
135
116
|
for (const conns of this.connections.values()) {
|
|
136
117
|
for (const conn of conns) {
|
|
@@ -155,9 +136,9 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
155
136
|
}
|
|
156
137
|
});
|
|
157
138
|
// latency monitor
|
|
158
|
-
this.latencyMonitor
|
|
139
|
+
this.latencyMonitor?.start();
|
|
159
140
|
this._onLatencyMeasure = this._onLatencyMeasure.bind(this);
|
|
160
|
-
this.latencyMonitor
|
|
141
|
+
this.latencyMonitor?.addEventListener('data', this._onLatencyMeasure);
|
|
161
142
|
this.started = true;
|
|
162
143
|
log('started');
|
|
163
144
|
}
|
|
@@ -208,9 +189,8 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
208
189
|
* Stops the Connection Manager
|
|
209
190
|
*/
|
|
210
191
|
async stop() {
|
|
211
|
-
this.
|
|
212
|
-
this.latencyMonitor
|
|
213
|
-
this.latencyMonitor.stop();
|
|
192
|
+
this.latencyMonitor?.removeEventListener('data', this._onLatencyMeasure);
|
|
193
|
+
this.latencyMonitor?.stop();
|
|
214
194
|
this.started = false;
|
|
215
195
|
await this._close();
|
|
216
196
|
log('stopped');
|
|
@@ -237,30 +217,6 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
237
217
|
await Promise.all(tasks);
|
|
238
218
|
this.connections.clear();
|
|
239
219
|
}
|
|
240
|
-
/**
|
|
241
|
-
* Checks the libp2p metrics to determine if any values have exceeded
|
|
242
|
-
* the configured maximums.
|
|
243
|
-
*
|
|
244
|
-
* @private
|
|
245
|
-
*/
|
|
246
|
-
async _checkMetrics() {
|
|
247
|
-
const metrics = this.components.metrics;
|
|
248
|
-
if (metrics != null) {
|
|
249
|
-
try {
|
|
250
|
-
const movingAverages = metrics.getGlobal().getMovingAverages();
|
|
251
|
-
const received = movingAverages.dataReceived[this.opts.movingAverageInterval].movingAverage;
|
|
252
|
-
await this._checkMaxLimit('maxReceivedData', received);
|
|
253
|
-
const sent = movingAverages.dataSent[this.opts.movingAverageInterval].movingAverage;
|
|
254
|
-
await this._checkMaxLimit('maxSentData', sent);
|
|
255
|
-
const total = received + sent;
|
|
256
|
-
await this._checkMaxLimit('maxData', total);
|
|
257
|
-
log.trace('metrics update', total);
|
|
258
|
-
}
|
|
259
|
-
finally {
|
|
260
|
-
this.timer = retimer(this._checkMetrics, this.opts.pollInterval);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
220
|
onConnect(evt) {
|
|
265
221
|
void this._onConnect(evt).catch(err => {
|
|
266
222
|
log.error(err);
|
|
@@ -311,7 +267,6 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
311
267
|
else if (storedConn != null) {
|
|
312
268
|
this.connections.delete(peerId);
|
|
313
269
|
this.dispatchEvent(new CustomEvent('peer:disconnect', { detail: connection }));
|
|
314
|
-
this.components.metrics?.onPeerDisconnected(connection.remotePeer);
|
|
315
270
|
}
|
|
316
271
|
}
|
|
317
272
|
getConnections(peerId) {
|
|
@@ -324,7 +279,18 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
324
279
|
}
|
|
325
280
|
return conns;
|
|
326
281
|
}
|
|
327
|
-
async openConnection(
|
|
282
|
+
async openConnection(peerIdOrMultiaddr, options = {}) {
|
|
283
|
+
let peerId;
|
|
284
|
+
if (isPeerId(peerIdOrMultiaddr)) {
|
|
285
|
+
peerId = peerIdOrMultiaddr;
|
|
286
|
+
}
|
|
287
|
+
else if (isMultiaddr(peerIdOrMultiaddr)) {
|
|
288
|
+
const info = getPeer(peerIdOrMultiaddr);
|
|
289
|
+
peerId = info.id;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
throw errCode(new TypeError('Can only open connections to PeerIds or Multiaddrs'), codes.ERR_INVALID_PARAMETERS);
|
|
293
|
+
}
|
|
328
294
|
log('dial to %p', peerId);
|
|
329
295
|
const existingConnections = this.getConnections(peerId);
|
|
330
296
|
if (existingConnections.length > 0) {
|
|
@@ -342,7 +308,7 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
342
308
|
catch { }
|
|
343
309
|
}
|
|
344
310
|
try {
|
|
345
|
-
const connection = await this.components.dialer.dial(
|
|
311
|
+
const connection = await this.components.dialer.dial(peerIdOrMultiaddr, options);
|
|
346
312
|
let peerConnections = this.connections.get(peerId.toString());
|
|
347
313
|
if (peerConnections == null) {
|
|
348
314
|
peerConnections = [];
|
|
@@ -404,6 +370,10 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
404
370
|
*/
|
|
405
371
|
async _checkMaxLimit(name, value, toPrune = 1) {
|
|
406
372
|
const limit = this.opts[name];
|
|
373
|
+
if (limit == null) {
|
|
374
|
+
log.trace('limit %s was not set so it cannot be applied', name);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
407
377
|
log.trace('checking limit of %s. current value: %d of %d', name, value, limit);
|
|
408
378
|
if (value > limit) {
|
|
409
379
|
log('%s: limit exceeded: %p, %d/%d, pruning %d connection(s)', this.components.peerId, name, value, limit, toPrune);
|