libp2p 0.36.2-fab4f13 → 0.37.0-d5386df
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/dist/src/address-manager/index.d.ts +2 -1
- 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/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +7 -6
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/circuit/hop.d.ts +1 -1
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +4 -4
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/index.d.ts +1 -1
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/listener.d.ts +3 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +10 -2
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +5 -4
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +28 -2
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +7 -5
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -7
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +4 -4
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +37 -8
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -23
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +57 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -42
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +1 -1
- package/dist/src/connection-manager/latency-monitor.js.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +2 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +4 -1
- package/dist/src/dht/dummy-dht.d.ts.map +1 -1
- package/dist/src/dht/dummy-dht.js +8 -1
- package/dist/src/dht/dummy-dht.js.map +1 -1
- package/dist/src/fetch/index.d.ts +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +1 -1
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +4 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +7 -1
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +1 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +4 -4
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +7 -24
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +4 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +8 -1
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +0 -1
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +0 -1
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +57 -43
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/index.d.ts +1 -1
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -1
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +1 -1
- package/dist/src/metrics/stats.js.map +1 -1
- package/dist/src/nat-manager.d.ts +6 -3
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +6 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +1 -1
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-routing.d.ts +2 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -1
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +1 -0
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +1 -0
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.d.ts +1 -1
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +1 -1
- package/dist/src/pnet/key-generator.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +1 -1
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +0 -1
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +2 -17
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +3 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +1 -1
- 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 +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +16 -25
- package/src/address-manager/index.ts +2 -1
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +5 -5
- package/src/circuit/index.ts +1 -1
- package/src/circuit/listener.ts +18 -5
- package/src/circuit/pb/index.ts +33 -4
- package/src/circuit/transport.ts +8 -5
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +5 -5
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/index.ts +48 -37
- package/src/connection-manager/index.ts +146 -60
- package/src/connection-manager/latency-monitor.ts +1 -1
- package/src/connection-manager/visibility-change-emitter.ts +1 -1
- package/src/content-routing/index.ts +2 -1
- package/src/dht/dummy-dht.ts +10 -1
- package/src/fetch/index.ts +2 -2
- package/src/fetch/pb/proto.ts +10 -3
- package/src/identify/index.ts +5 -5
- package/src/identify/pb/message.ts +2 -1
- package/src/index.ts +7 -28
- package/src/insecure/pb/proto.ts +11 -4
- package/src/keychain/index.ts +0 -1
- package/src/libp2p.ts +68 -49
- package/src/metrics/index.ts +1 -1
- package/src/metrics/stats.ts +1 -1
- package/src/nat-manager.ts +8 -4
- package/src/peer-record-updater.ts +1 -1
- package/src/peer-routing.ts +2 -1
- package/src/ping/index.ts +3 -2
- package/src/pnet/index.ts +2 -0
- package/src/pnet/key-generator.ts +1 -1
- package/src/pubsub/dummy-pubsub.ts +1 -1
- package/src/registrar.ts +3 -19
- package/src/transport-manager.ts +3 -1
- package/src/upgrader.ts +1 -1
- package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/dialer/auto-dialer.js +0 -24
- package/dist/src/dialer/auto-dialer.js.map +0 -1
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/dial-request.js.map +0 -1
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/dialer/index.js.map +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { TimeoutController } from 'timeout-abort-controller';
|
|
3
|
+
const log = logger('libp2p:dialer:auto-dialer');
|
|
4
|
+
export class AutoDialer {
|
|
5
|
+
constructor(components, init) {
|
|
6
|
+
this.components = components;
|
|
7
|
+
this.enabled = init.enabled;
|
|
8
|
+
this.minConnections = init.minConnections;
|
|
9
|
+
this.dialTimeout = init.dialTimeout;
|
|
10
|
+
}
|
|
11
|
+
handle(evt) {
|
|
12
|
+
const { detail: peer } = evt;
|
|
13
|
+
if (!this.enabled) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const connections = this.components.getConnectionManager().getConnections(peer.id);
|
|
17
|
+
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
|
18
|
+
if (connections.length === 0) {
|
|
19
|
+
const minConnections = this.minConnections ?? 0;
|
|
20
|
+
const allConnections = this.components.getConnectionManager().getConnections();
|
|
21
|
+
if (minConnections > allConnections.length) {
|
|
22
|
+
log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout);
|
|
23
|
+
const controller = new TimeoutController(this.dialTimeout);
|
|
24
|
+
void this.components.getConnectionManager().openConnection(peer.id, {
|
|
25
|
+
signal: controller.signal
|
|
26
|
+
})
|
|
27
|
+
.catch(err => {
|
|
28
|
+
log.error('could not connect to discovered peer %p with %o', peer.id, err);
|
|
29
|
+
})
|
|
30
|
+
.finally(() => {
|
|
31
|
+
controller.clear();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=auto-dialer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-dialer.js","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/auto-dialer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAE5D,MAAM,GAAG,GAAG,MAAM,CAAC,2BAA2B,CAAC,CAAA;AAQ/C,MAAM,OAAO,UAAU;IAMrB,YAAa,UAAsB,EAAE,IAAoB;QACvD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAA;QAC3B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAA;QACzC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAA;IACrC,CAAC;IAEM,MAAM,CAAE,GAA0B;QACvC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAA;QAE5B,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;YACjB,OAAM;SACP;QAED,MAAM,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QAElF,uFAAuF;QACvF,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE;YAC5B,MAAM,cAAc,GAAG,IAAI,CAAC,cAAc,IAAI,CAAC,CAAA;YAE/C,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC,cAAc,EAAE,CAAA;YAE9E,IAAI,cAAc,GAAG,cAAc,CAAC,MAAM,EAAE;gBAC1C,GAAG,CAAC,iDAAiD,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,WAAW,CAAC,CAAA;gBAEjF,MAAM,UAAU,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;gBAE1D,KAAK,IAAI,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,EAAE;oBAClE,MAAM,EAAE,UAAU,CAAC,MAAM;iBAC1B,CAAC;qBACC,KAAK,CAAC,GAAG,CAAC,EAAE;oBACX,GAAG,CAAC,KAAK,CAAC,iDAAiD,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;gBAC5E,CAAC,CAAC;qBACD,OAAO,CAAC,GAAG,EAAE;oBACZ,UAAU,CAAC,KAAK,EAAE,CAAA;gBACpB,CAAC,CAAC,CAAA;aACL;SACF;IACH,CAAC;CACF"}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
2
2
|
import type { Connection } from '@libp2p/interfaces/connection';
|
|
3
3
|
import type { AbortOptions } from '@libp2p/interfaces';
|
|
4
|
-
import type {
|
|
4
|
+
import type { Dialer } from './index.js';
|
|
5
5
|
export interface DialAction {
|
|
6
6
|
(m: Multiaddr, options: AbortOptions): Promise<Connection>;
|
|
7
7
|
}
|
|
8
8
|
export interface DialRequestOptions {
|
|
9
9
|
addrs: Multiaddr[];
|
|
10
10
|
dialAction: DialAction;
|
|
11
|
-
dialer:
|
|
11
|
+
dialer: Dialer;
|
|
12
12
|
}
|
|
13
13
|
export declare class DialRequest {
|
|
14
14
|
private readonly addrs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dial-request.d.ts","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/dial-request.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAIxC,MAAM,WAAW,UAAU;IACzB,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CAC3D;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,SAAS,EAAE,CAAA;IAClB,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IAEvC;;;;;;OAMG;gBACU,OAAO,EAAE,kBAAkB;IAYlC,GAAG,CAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;CA+D5D"}
|
|
@@ -3,7 +3,7 @@ import { anySignal } from 'any-signal';
|
|
|
3
3
|
import FIFO from 'p-fifo';
|
|
4
4
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
5
5
|
import { setMaxListeners } from 'events';
|
|
6
|
-
import { codes } from '
|
|
6
|
+
import { codes } from '../../errors.js';
|
|
7
7
|
import { logger } from '@libp2p/logger';
|
|
8
8
|
const log = logger('libp2p:dialer:dial-request');
|
|
9
9
|
export class DialRequest {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dial-request.js","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/dial-request.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,IAAI,MAAM,QAAQ,CAAA;AACzB,qEAAqE;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAMvC,MAAM,GAAG,GAAG,MAAM,CAAC,4BAA4B,CAAC,CAAA;AAYhD,MAAM,OAAO,WAAW;IAKtB;;;;;;OAMG;IACH,YAAa,OAA2B;QACtC,MAAM,EACJ,KAAK,EACL,UAAU,EACV,MAAM,EACP,GAAG,OAAO,CAAA;QAEX,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,UAAwB,EAAE;QACnC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAEvD,oCAAoC;QACpC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,0BAA0B,CAAC,EAAE,KAAK,CAAC,kBAAkB,CAAC,CAAA;SAC/E;QAED,MAAM,WAAW,GAAG,IAAI,IAAI,EAAU,CAAA;QAEtC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;YAC1B,KAAK,WAAW,CAAC,IAAI,C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AoB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;iBAClC;wBAAS;oBACR,cAAc,EAAE,CAAA;oBAChB,gGAAgG;oBAChG,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE;wBACvD,KAAK,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;4BACvC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;wBAChB,CAAC,CAAC,CAAA;qBACH;yBAAM;wBACL,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;qBACrE;iBACF;gBAED,OAAO,IAAI,CAAA;YACb,CAAC,CAAC,CAAC,CAAA;SACJ;gBAAS;YACR,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAA,CAAC,kDAAkD;YAC3F,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAA,CAAC,oCAAoC;SAC9F;IACH,CAAC;CACF"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Multiaddr } from '@multiformats/multiaddr';
|
|
1
|
+
import { Multiaddr, Resolver } from '@multiformats/multiaddr';
|
|
2
2
|
import { TimeoutController } from 'timeout-abort-controller';
|
|
3
3
|
import { DialRequest } from './dial-request.js';
|
|
4
4
|
import type { Connection } from '@libp2p/interfaces/connection';
|
|
5
|
-
import type { AbortOptions
|
|
5
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
6
|
+
import type { Startable } from '@libp2p/interfaces/startable';
|
|
6
7
|
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
7
|
-
import
|
|
8
|
-
import type {
|
|
8
|
+
import { Components, Initializable } from '@libp2p/interfaces/components';
|
|
9
|
+
import type { AddressSorter } from '@libp2p/interfaces/peer-store';
|
|
10
|
+
import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics';
|
|
9
11
|
export interface DialTarget {
|
|
10
12
|
id: string;
|
|
11
13
|
addrs: Multiaddr[];
|
|
@@ -20,8 +22,35 @@ export interface PendingDialTarget {
|
|
|
20
22
|
resolve: (value: any) => void;
|
|
21
23
|
reject: (err: Error) => void;
|
|
22
24
|
}
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
+
export interface DialerInit {
|
|
26
|
+
/**
|
|
27
|
+
* Sort the known addresses of a peer before trying to dial
|
|
28
|
+
*/
|
|
29
|
+
addressSorter?: AddressSorter;
|
|
30
|
+
/**
|
|
31
|
+
* Number of max concurrent dials
|
|
32
|
+
*/
|
|
33
|
+
maxParallelDials?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Number of max addresses to dial for a given peer
|
|
36
|
+
*/
|
|
37
|
+
maxAddrsToDial?: number;
|
|
38
|
+
/**
|
|
39
|
+
* How long a dial attempt is allowed to take
|
|
40
|
+
*/
|
|
41
|
+
dialTimeout?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Number of max concurrent dials per peer
|
|
44
|
+
*/
|
|
45
|
+
maxDialsPerPeer?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Multiaddr resolvers to use when dialing
|
|
48
|
+
*/
|
|
49
|
+
resolvers?: Record<string, Resolver>;
|
|
50
|
+
metrics?: ComponentMetricsTracker;
|
|
51
|
+
}
|
|
52
|
+
export declare class Dialer implements Startable, Initializable {
|
|
53
|
+
private components;
|
|
25
54
|
private readonly addressSorter;
|
|
26
55
|
private readonly maxAddrsToDial;
|
|
27
56
|
private readonly timeout;
|
|
@@ -30,7 +59,8 @@ export declare class DefaultDialer implements Dialer, Startable {
|
|
|
30
59
|
pendingDials: Map<string, PendingDial>;
|
|
31
60
|
pendingDialTargets: Map<string, PendingDialTarget>;
|
|
32
61
|
private started;
|
|
33
|
-
constructor(
|
|
62
|
+
constructor(init?: DialerInit);
|
|
63
|
+
init(components: Components): void;
|
|
34
64
|
isStarted(): boolean;
|
|
35
65
|
start(): Promise<void>;
|
|
36
66
|
/**
|
|
@@ -43,7 +73,6 @@ export declare class DefaultDialer implements Dialer, Startable {
|
|
|
43
73
|
* will be used.
|
|
44
74
|
*/
|
|
45
75
|
dial(peer: PeerId | Multiaddr, options?: AbortOptions): Promise<Connection>;
|
|
46
|
-
dialProtocol(peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions): Promise<import("@libp2p/interfaces/connection").ProtocolStream>;
|
|
47
76
|
/**
|
|
48
77
|
* Connects to a given `peer` by dialing all of its known addresses.
|
|
49
78
|
* The dial to the first address that is successfully able to upgrade a connection
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAK5D,OAAO,EAAc,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAU3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAGxD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAEzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAClE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAA;AAQzE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ACzD,OAAO,CAAC,OAAO,CAAS;gBAEX,IAAI,GAAE,UAAe;IAuBlC,IAAI,CAAE,UAAU,EAAE,UAAU,GAAG,IAAI;IAInC,SAAS;IAIH,KAAK;IAIX;;OAEG;IACG,IAAI;IAkBV;;;;OAIG;IACG,IAAI,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IA6CtF;;;;OAIG;IACG,4BAA4B,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAmBtE;;;;;OAKG;IACG,iBAAiB,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAuC3D;;OAEG;IACH,kBAAkB,CAAE,UAAU,EAAE,UAAU,EAAE,OAAO,GAAE,YAAiB,GAAG,WAAW;IA+CpF,SAAS,CAAE,GAAG,EAAE,MAAM;IAOtB,YAAY,CAAE,KAAK,EAAE,MAAM;IAU3B;;OAEG;IACG,QAAQ,CAAE,EAAE,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAwBpD;;OAEG;IACG,cAAc,CAAE,EAAE,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;CAU3D"}
|
|
@@ -12,18 +12,19 @@ import { setMaxListeners } from 'events';
|
|
|
12
12
|
import { DialRequest } from './dial-request.js';
|
|
13
13
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort';
|
|
14
14
|
import { trackedMap } from '@libp2p/tracked-map';
|
|
15
|
-
import { codes } from '
|
|
16
|
-
import { DIAL_TIMEOUT, MAX_PARALLEL_DIALS, MAX_PER_PEER_DIALS, MAX_ADDRS_TO_DIAL } from '
|
|
17
|
-
import { getPeer } from '
|
|
15
|
+
import { codes } from '../../errors.js';
|
|
16
|
+
import { DIAL_TIMEOUT, MAX_PARALLEL_DIALS, MAX_PER_PEER_DIALS, MAX_ADDRS_TO_DIAL } from '../../constants.js';
|
|
17
|
+
import { getPeer } from '../../get-peer.js';
|
|
18
18
|
import sort from 'it-sort';
|
|
19
|
+
import { Components } from '@libp2p/interfaces/components';
|
|
19
20
|
import map from 'it-map';
|
|
20
21
|
const log = logger('libp2p:dialer');
|
|
21
22
|
const METRICS_COMPONENT = 'dialler';
|
|
22
23
|
const METRICS_PENDING_DIALS = 'pending-dials';
|
|
23
24
|
const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets';
|
|
24
|
-
export class
|
|
25
|
-
constructor(
|
|
26
|
-
this.components =
|
|
25
|
+
export class Dialer {
|
|
26
|
+
constructor(init = {}) {
|
|
27
|
+
this.components = new Components();
|
|
27
28
|
this.started = false;
|
|
28
29
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst;
|
|
29
30
|
this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL;
|
|
@@ -44,6 +45,9 @@ export class DefaultDialer {
|
|
|
44
45
|
Multiaddr.resolvers.set(key, value);
|
|
45
46
|
}
|
|
46
47
|
}
|
|
48
|
+
init(components) {
|
|
49
|
+
this.components = components;
|
|
50
|
+
}
|
|
47
51
|
isStarted() {
|
|
48
52
|
return this.started;
|
|
49
53
|
}
|
|
@@ -87,12 +91,6 @@ export class DefaultDialer {
|
|
|
87
91
|
if (await this.components.getConnectionGater().denyDialPeer(id)) {
|
|
88
92
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED);
|
|
89
93
|
}
|
|
90
|
-
log('dial to %p', id);
|
|
91
|
-
const existingConnection = this.components.getConnectionManager().getConnection(id);
|
|
92
|
-
if (existingConnection != null) {
|
|
93
|
-
log('had an existing connection to %p', id);
|
|
94
|
-
return existingConnection;
|
|
95
|
-
}
|
|
96
94
|
log('creating dial target for %p', id);
|
|
97
95
|
const dialTarget = await this._createCancellableDialTarget(id);
|
|
98
96
|
if (dialTarget.addrs.length === 0) {
|
|
@@ -117,17 +115,6 @@ export class DefaultDialer {
|
|
|
117
115
|
pendingDial.destroy();
|
|
118
116
|
}
|
|
119
117
|
}
|
|
120
|
-
async dialProtocol(peer, protocols, options = {}) {
|
|
121
|
-
if (protocols == null) {
|
|
122
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM);
|
|
123
|
-
}
|
|
124
|
-
protocols = Array.isArray(protocols) ? protocols : [protocols];
|
|
125
|
-
if (protocols.length === 0) {
|
|
126
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM);
|
|
127
|
-
}
|
|
128
|
-
const connection = await this.dial(peer, options);
|
|
129
|
-
return await connection.newStream(protocols);
|
|
130
|
-
}
|
|
131
118
|
/**
|
|
132
119
|
* Connects to a given `peer` by dialing all of its known addresses.
|
|
133
120
|
* The dial to the first address that is successfully able to upgrade a connection
|
|
@@ -0,0 +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,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,SAAS,EAAY,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,qEAAqE;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AACxC,OAAO,EAAc,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4B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AAE,CAAC,OAAO,EAAE,CAAA;YACrC,OAAO,UAAU,CAAA;SAClB;QAAC,OAAO,GAAG,EAAE;YACZ,GAAG,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC,QAAQ,EAAE,wBAAwB,EAAE,GAAG,CAAC,CAAA;YAClE,OAAO,EAAE,CAAA;SACV;IACH,CAAC;CACF"}
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { SummaryObject } from './latency-monitor.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
3
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
4
|
+
import type { Startable } from '@libp2p/interfaces/startable';
|
|
3
5
|
import { PeerId } from '@libp2p/interfaces/peer-id';
|
|
4
6
|
import type { Connection } from '@libp2p/interfaces/connection';
|
|
5
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
7
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager';
|
|
8
|
+
import { Components, Initializable } from '@libp2p/interfaces/components';
|
|
9
|
+
import { Dialer } from './dialer/index.js';
|
|
10
|
+
import type { AddressSorter } from '@libp2p/interfaces/peer-store';
|
|
11
|
+
import type { Resolver } from '@multiformats/multiaddr';
|
|
11
12
|
export interface ConnectionManagerInit {
|
|
12
13
|
/**
|
|
13
|
-
* The maximum number of connections
|
|
14
|
+
* The maximum number of connections to keep open
|
|
14
15
|
*/
|
|
15
|
-
maxConnections
|
|
16
|
+
maxConnections: number;
|
|
16
17
|
/**
|
|
17
|
-
* The minimum number of connections to
|
|
18
|
+
* The minimum number of connections to keep open
|
|
18
19
|
*/
|
|
19
|
-
minConnections
|
|
20
|
+
minConnections: number;
|
|
20
21
|
/**
|
|
21
22
|
* The max data (in and out), per average interval to allow
|
|
22
23
|
*/
|
|
@@ -46,32 +47,65 @@ export interface ConnectionManagerInit {
|
|
|
46
47
|
*/
|
|
47
48
|
defaultPeerValue?: number;
|
|
48
49
|
/**
|
|
49
|
-
*
|
|
50
|
+
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
50
51
|
*/
|
|
51
52
|
autoDial?: boolean;
|
|
52
53
|
/**
|
|
53
|
-
* How
|
|
54
|
+
* How long to wait between attempting to keep our number of concurrent connections
|
|
55
|
+
* above minConnections
|
|
56
|
+
*/
|
|
57
|
+
autoDialInterval: number;
|
|
58
|
+
/**
|
|
59
|
+
* Sort the known addresses of a peer before trying to dial
|
|
60
|
+
*/
|
|
61
|
+
addressSorter?: AddressSorter;
|
|
62
|
+
/**
|
|
63
|
+
* Number of max concurrent dials
|
|
64
|
+
*/
|
|
65
|
+
maxParallelDials?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Number of max addresses to dial for a given peer
|
|
68
|
+
*/
|
|
69
|
+
maxAddrsToDial?: number;
|
|
70
|
+
/**
|
|
71
|
+
* How long a dial attempt is allowed to take
|
|
72
|
+
*/
|
|
73
|
+
dialTimeout?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Number of max concurrent dials per peer
|
|
54
76
|
*/
|
|
55
|
-
|
|
77
|
+
maxDialsPerPeer?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Multiaddr resolvers to use when dialing
|
|
80
|
+
*/
|
|
81
|
+
resolvers?: Record<string, Resolver>;
|
|
82
|
+
}
|
|
83
|
+
export interface ConnectionManagerEvents {
|
|
84
|
+
'peer:connect': CustomEvent<PeerId>;
|
|
85
|
+
'peer:disconnect': CustomEvent<PeerId>;
|
|
56
86
|
}
|
|
57
87
|
/**
|
|
58
88
|
* Responsible for managing known connections.
|
|
59
89
|
*/
|
|
60
|
-
export declare class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
|
|
61
|
-
|
|
62
|
-
private
|
|
90
|
+
export declare class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable, Initializable {
|
|
91
|
+
readonly dialer: Dialer;
|
|
92
|
+
private components;
|
|
93
|
+
private readonly opts;
|
|
63
94
|
private readonly peerValues;
|
|
64
95
|
private readonly connections;
|
|
65
96
|
private started;
|
|
66
97
|
private timer?;
|
|
67
98
|
private readonly latencyMonitor;
|
|
68
|
-
constructor(
|
|
99
|
+
constructor(init: ConnectionManagerInit);
|
|
100
|
+
init(components: Components): void;
|
|
69
101
|
isStarted(): boolean;
|
|
70
102
|
/**
|
|
71
103
|
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
72
104
|
* only event loop and connection limits will be monitored.
|
|
73
105
|
*/
|
|
74
106
|
start(): Promise<void>;
|
|
107
|
+
afterStart(): Promise<void>;
|
|
108
|
+
beforeStop(): Promise<void>;
|
|
75
109
|
/**
|
|
76
110
|
* Stops the Connection Manager
|
|
77
111
|
*/
|
|
@@ -92,21 +126,18 @@ export declare class DefaultConnectionManager extends EventEmitter<ConnectionMan
|
|
|
92
126
|
* @private
|
|
93
127
|
*/
|
|
94
128
|
_checkMetrics(): Promise<void>;
|
|
129
|
+
onConnect(evt: CustomEvent<Connection>): void;
|
|
95
130
|
/**
|
|
96
131
|
* Tracks the incoming connection and check the connection limit
|
|
97
132
|
*/
|
|
98
|
-
|
|
133
|
+
_onConnect(evt: CustomEvent<Connection>): Promise<void>;
|
|
99
134
|
/**
|
|
100
135
|
* Removes the connection from tracking
|
|
101
136
|
*/
|
|
102
137
|
onDisconnect(evt: CustomEvent<Connection>): void;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Get a connection with a peer
|
|
108
|
-
*/
|
|
109
|
-
getConnection(peerId: PeerId): Connection | undefined;
|
|
138
|
+
getConnections(peerId?: PeerId): Connection[];
|
|
139
|
+
openConnection(peerId: PeerId, options?: AbortOptions): Promise<Connection>;
|
|
140
|
+
closeConnections(peerId: PeerId): Promise<void>;
|
|
110
141
|
/**
|
|
111
142
|
* Get all open connections with a peer
|
|
112
143
|
*/
|
|
@@ -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;AAGpE,OAAO,EAAe,YAAY,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAGpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,EAAe,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAG7D,OAAO,EAAY,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAG7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAA;AAC9E,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAEzE,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAClE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAqBvD,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,cAAc,EAAE,MAAM,CAAA;IAEtB;;OAEG;IACH,cAAc,EAAE,MAAM,CAAA;IAEtB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;OAEG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;OAEG;IACH,YA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UAAU;IAKV,UAAU;IAKhB;;OAEG;IACG,IAAI;IAYV;;OAEG;IACG,MAAM;IAcZ;;;OAGG;IACH,YAAY,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAQ3C;;;;;OAKG;IACG,aAAa;IAmBnB,SAAS,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAMvC;;OAEG;IACG,UAAU,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IA+B9C;;OAEG;IACH,YAAY,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAuB1C,cAAc,CAAE,MAAM,CAAC,EAAE,MAAM,GAAG,UAAU,EAAE;IAcxC,cAAc,CAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC;IAoC5E,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;IAStE;;;OAGG;IACG,mBAAmB;CA6B1B"}
|
|
@@ -4,13 +4,15 @@ import mergeOptions from 'merge-options';
|
|
|
4
4
|
import { LatencyMonitor } from './latency-monitor.js';
|
|
5
5
|
// @ts-expect-error retimer does not have types
|
|
6
6
|
import retimer from 'retimer';
|
|
7
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces';
|
|
7
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events';
|
|
8
8
|
import { trackedMap } from '@libp2p/tracked-map';
|
|
9
9
|
import { codes } from '../errors.js';
|
|
10
10
|
import { isPeerId } from '@libp2p/interfaces/peer-id';
|
|
11
11
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
12
12
|
import { setMaxListeners } from 'events';
|
|
13
|
+
import { Components } from '@libp2p/interfaces/components';
|
|
13
14
|
import * as STATUS from '@libp2p/interfaces/connection/status';
|
|
15
|
+
import { Dialer } from './dialer/index.js';
|
|
14
16
|
const log = logger('libp2p:connection-manager');
|
|
15
17
|
const defaultOptions = {
|
|
16
18
|
maxConnections: Infinity,
|
|
@@ -31,14 +33,14 @@ const METRICS_PEER_VALUES = 'peer-values';
|
|
|
31
33
|
* Responsible for managing known connections.
|
|
32
34
|
*/
|
|
33
35
|
export class DefaultConnectionManager extends EventEmitter {
|
|
34
|
-
constructor(
|
|
36
|
+
constructor(init) {
|
|
35
37
|
super();
|
|
36
|
-
this.components =
|
|
37
|
-
this.
|
|
38
|
-
if (this.
|
|
38
|
+
this.components = new Components();
|
|
39
|
+
this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init);
|
|
40
|
+
if (this.opts.maxConnections < this.opts.minConnections) {
|
|
39
41
|
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS);
|
|
40
42
|
}
|
|
41
|
-
log('options: %o', this.
|
|
43
|
+
log('options: %o', this.opts);
|
|
42
44
|
/**
|
|
43
45
|
* Map of peer identifiers to their peer value for pruning connections.
|
|
44
46
|
*
|
|
@@ -68,12 +70,13 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
68
70
|
setMaxListeners?.(Infinity, this);
|
|
69
71
|
}
|
|
70
72
|
catch { }
|
|
71
|
-
this.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.components
|
|
73
|
+
this.dialer = new Dialer(this.opts);
|
|
74
|
+
this.onConnect = this.onConnect.bind(this);
|
|
75
|
+
this.onDisconnect = this.onDisconnect.bind(this);
|
|
76
|
+
}
|
|
77
|
+
init(components) {
|
|
78
|
+
this.components = components;
|
|
79
|
+
this.dialer.init(components);
|
|
77
80
|
}
|
|
78
81
|
isStarted() {
|
|
79
82
|
return this.started;
|
|
@@ -84,15 +87,24 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
84
87
|
*/
|
|
85
88
|
async start() {
|
|
86
89
|
if (this.components.getMetrics() != null) {
|
|
87
|
-
this.timer = this.timer ?? retimer(this._checkMetrics, this.
|
|
90
|
+
this.timer = this.timer ?? retimer(this._checkMetrics, this.opts.pollInterval);
|
|
88
91
|
}
|
|
89
92
|
// latency monitor
|
|
90
93
|
this.latencyMonitor.start();
|
|
91
94
|
this._onLatencyMeasure = this._onLatencyMeasure.bind(this);
|
|
92
95
|
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure);
|
|
96
|
+
await this.dialer.start();
|
|
93
97
|
this.started = true;
|
|
94
98
|
log('started');
|
|
95
99
|
}
|
|
100
|
+
async afterStart() {
|
|
101
|
+
this.components.getUpgrader().addEventListener('connection', this.onConnect);
|
|
102
|
+
this.components.getUpgrader().addEventListener('connectionEnd', this.onDisconnect);
|
|
103
|
+
}
|
|
104
|
+
async beforeStop() {
|
|
105
|
+
this.components.getUpgrader().removeEventListener('connection', this.onConnect);
|
|
106
|
+
this.components.getUpgrader().removeEventListener('connectionEnd', this.onDisconnect);
|
|
107
|
+
}
|
|
96
108
|
/**
|
|
97
109
|
* Stops the Connection Manager
|
|
98
110
|
*/
|
|
@@ -100,6 +112,7 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
100
112
|
this.timer?.clear();
|
|
101
113
|
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure);
|
|
102
114
|
this.latencyMonitor.stop();
|
|
115
|
+
await this.dialer.stop();
|
|
103
116
|
this.started = false;
|
|
104
117
|
await this._close();
|
|
105
118
|
log('stopped');
|
|
@@ -140,23 +153,28 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
140
153
|
if (metrics != null) {
|
|
141
154
|
try {
|
|
142
155
|
const movingAverages = metrics.getGlobal().getMovingAverages();
|
|
143
|
-
const received = movingAverages.dataReceived[this.
|
|
156
|
+
const received = movingAverages.dataReceived[this.opts.movingAverageInterval].movingAverage;
|
|
144
157
|
await this._checkMaxLimit('maxReceivedData', received);
|
|
145
|
-
const sent = movingAverages.dataSent[this.
|
|
158
|
+
const sent = movingAverages.dataSent[this.opts.movingAverageInterval].movingAverage;
|
|
146
159
|
await this._checkMaxLimit('maxSentData', sent);
|
|
147
160
|
const total = received + sent;
|
|
148
161
|
await this._checkMaxLimit('maxData', total);
|
|
149
|
-
log('metrics update', total);
|
|
162
|
+
log.trace('metrics update', total);
|
|
150
163
|
}
|
|
151
164
|
finally {
|
|
152
|
-
this.timer = retimer(this._checkMetrics, this.
|
|
165
|
+
this.timer = retimer(this._checkMetrics, this.opts.pollInterval);
|
|
153
166
|
}
|
|
154
167
|
}
|
|
155
168
|
}
|
|
169
|
+
onConnect(evt) {
|
|
170
|
+
void this._onConnect(evt).catch(err => {
|
|
171
|
+
log.error(err);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
156
174
|
/**
|
|
157
175
|
* Tracks the incoming connection and check the connection limit
|
|
158
176
|
*/
|
|
159
|
-
async
|
|
177
|
+
async _onConnect(evt) {
|
|
160
178
|
const { detail: connection } = evt;
|
|
161
179
|
if (!this.started) {
|
|
162
180
|
// This can happen when we are in the process of shutting down the node
|
|
@@ -166,7 +184,6 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
166
184
|
const peerId = connection.remotePeer;
|
|
167
185
|
const peerIdStr = peerId.toString();
|
|
168
186
|
const storedConns = this.connections.get(peerIdStr);
|
|
169
|
-
this.dispatchEvent(new CustomEvent('peer:connect', { detail: connection }));
|
|
170
187
|
if (storedConns != null) {
|
|
171
188
|
storedConns.push(connection);
|
|
172
189
|
}
|
|
@@ -177,9 +194,10 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
177
194
|
await this.components.getPeerStore().keyBook.set(peerId, peerId.publicKey);
|
|
178
195
|
}
|
|
179
196
|
if (!this.peerValues.has(peerIdStr)) {
|
|
180
|
-
this.peerValues.set(peerIdStr, this.
|
|
197
|
+
this.peerValues.set(peerIdStr, this.opts.defaultPeerValue);
|
|
181
198
|
}
|
|
182
|
-
await this._checkMaxLimit('maxConnections', this.
|
|
199
|
+
await this._checkMaxLimit('maxConnections', this.getConnections().length);
|
|
200
|
+
this.dispatchEvent(new CustomEvent('peer:connect', { detail: connection }));
|
|
183
201
|
}
|
|
184
202
|
/**
|
|
185
203
|
* Removes the connection from tracking
|
|
@@ -203,28 +221,48 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
203
221
|
this.components.getMetrics()?.onPeerDisconnected(connection.remotePeer);
|
|
204
222
|
}
|
|
205
223
|
}
|
|
206
|
-
getConnectionMap() {
|
|
207
|
-
return this.connections;
|
|
208
|
-
}
|
|
209
|
-
getConnectionList() {
|
|
210
|
-
let output = [];
|
|
211
|
-
for (const connections of this.connections.values()) {
|
|
212
|
-
output = output.concat(connections);
|
|
213
|
-
}
|
|
214
|
-
return output;
|
|
215
|
-
}
|
|
216
224
|
getConnections(peerId) {
|
|
217
|
-
|
|
225
|
+
if (peerId != null) {
|
|
226
|
+
return this.connections.get(peerId.toString()) ?? [];
|
|
227
|
+
}
|
|
228
|
+
let conns = [];
|
|
229
|
+
for (const c of this.connections.values()) {
|
|
230
|
+
conns = conns.concat(c);
|
|
231
|
+
}
|
|
232
|
+
return conns;
|
|
218
233
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return connections[0];
|
|
234
|
+
async openConnection(peerId, options) {
|
|
235
|
+
log('dial to %p', peerId);
|
|
236
|
+
const existingConnections = this.getConnections(peerId);
|
|
237
|
+
if (existingConnections.length > 0) {
|
|
238
|
+
log('had an existing connection to %p', peerId);
|
|
239
|
+
return existingConnections[0];
|
|
226
240
|
}
|
|
227
|
-
|
|
241
|
+
const connection = await this.dialer.dial(peerId, options);
|
|
242
|
+
let peerConnections = this.connections.get(peerId.toString());
|
|
243
|
+
if (peerConnections == null) {
|
|
244
|
+
peerConnections = [];
|
|
245
|
+
this.connections.set(peerId.toString(), peerConnections);
|
|
246
|
+
}
|
|
247
|
+
// we get notified of connections via the Upgrader emitting "connection"
|
|
248
|
+
// events, double check we aren't already tracking this connection before
|
|
249
|
+
// storing it
|
|
250
|
+
let trackedConnection = false;
|
|
251
|
+
for (const conn of peerConnections) {
|
|
252
|
+
if (conn.id === connection.id) {
|
|
253
|
+
trackedConnection = true;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (!trackedConnection) {
|
|
257
|
+
peerConnections.push(connection);
|
|
258
|
+
}
|
|
259
|
+
return connection;
|
|
260
|
+
}
|
|
261
|
+
async closeConnections(peerId) {
|
|
262
|
+
const connections = this.connections.get(peerId.toString()) ?? [];
|
|
263
|
+
await Promise.all(connections.map(async (connection) => {
|
|
264
|
+
return await connection.close();
|
|
265
|
+
}));
|
|
228
266
|
}
|
|
229
267
|
/**
|
|
230
268
|
* Get all open connections with a peer
|
|
@@ -255,7 +293,7 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
255
293
|
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
|
256
294
|
*/
|
|
257
295
|
async _checkMaxLimit(name, value) {
|
|
258
|
-
const limit = this.
|
|
296
|
+
const limit = this.opts[name];
|
|
259
297
|
log.trace('checking limit of %s. current value: %d of %d', name, value, limit);
|
|
260
298
|
if (value > limit) {
|
|
261
299
|
log('%s: limit exceeded: %p, %d', this.components.getPeerId(), name, value);
|
|
@@ -267,7 +305,7 @@ export class DefaultConnectionManager extends EventEmitter {
|
|
|
267
305
|
* to the lowest valued peer.
|
|
268
306
|
*/
|
|
269
307
|
async _maybeDisconnectOne() {
|
|
270
|
-
if (this.
|
|
308
|
+
if (this.opts.minConnections < this.connections.size) {
|
|
271
309
|
const peerValues = Array.from(new Map([...this.peerValues.entries()].sort((a, b) => a[1] - b[1])));
|
|
272
310
|
log('%p: sorted peer values: %j', this.components.getPeerId(), peerValues);
|
|
273
311
|
const disconnectPeer = peerValues[0];
|