libp2p 0.43.3 → 0.43.4-20c8ed4b
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/index.min.js +30 -30
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +1 -3
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/constants.d.ts.map +1 -0
- package/dist/src/circuit-relay/constants.js.map +1 -0
- package/dist/src/circuit-relay/index.d.ts.map +1 -0
- package/dist/src/circuit-relay/index.js.map +1 -0
- package/dist/src/circuit-relay/pb/index.d.ts.map +1 -0
- package/dist/src/circuit-relay/pb/index.js.map +1 -0
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -0
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -0
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -0
- package/dist/src/circuit-relay/server/index.js.map +1 -0
- package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit-relay/server/reservation-store.js.map +1 -0
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit-relay/server/reservation-voucher.js.map +1 -0
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -0
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -0
- package/dist/src/circuit-relay/transport/index.js.map +1 -0
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -0
- package/dist/src/circuit-relay/transport/listener.js.map +1 -0
- package/dist/src/{circuit → circuit-relay}/transport/reservation-store.d.ts +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -0
- package/dist/src/circuit-relay/utils.d.ts.map +1 -0
- package/dist/src/{circuit → circuit-relay}/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -0
- package/dist/src/components.d.ts +1 -6
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +0 -10
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -9
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +36 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +122 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +28 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +96 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +77 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +366 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +65 -69
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +116 -229
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- package/dist/src/content-routing/utils.js +1 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/get-peer.d.ts +6 -5
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +34 -12
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/identify/index.js +1 -1
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +13 -5
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +8 -12
- package/dist/src/libp2p.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 +2 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +14 -14
- package/src/autonat/index.ts +2 -5
- package/src/{circuit → circuit-relay}/transport/reservation-store.ts +1 -1
- package/src/{circuit → circuit-relay}/utils.ts +3 -1
- package/src/components.ts +1 -17
- package/src/config.ts +0 -9
- package/src/connection-manager/auto-dial.ts +171 -0
- package/src/connection-manager/connection-pruner.ts +137 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +504 -0
- package/src/connection-manager/index.ts +190 -319
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/utils.ts +1 -1
- package/src/get-peer.ts +45 -15
- package/src/identify/index.ts +1 -1
- package/src/index.ts +1 -1
- package/src/libp2p.ts +21 -17
- package/src/upgrader.ts +3 -2
- package/src/version.ts +1 -1
- package/dist/src/circuit/constants.d.ts.map +0 -1
- package/dist/src/circuit/constants.js.map +0 -1
- package/dist/src/circuit/index.d.ts.map +0 -1
- package/dist/src/circuit/index.js.map +0 -1
- package/dist/src/circuit/pb/index.d.ts.map +0 -1
- package/dist/src/circuit/pb/index.js.map +0 -1
- package/dist/src/circuit/server/advert-service.d.ts.map +0 -1
- package/dist/src/circuit/server/advert-service.js.map +0 -1
- package/dist/src/circuit/server/index.d.ts.map +0 -1
- package/dist/src/circuit/server/index.js.map +0 -1
- package/dist/src/circuit/server/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit/server/reservation-store.js.map +0 -1
- package/dist/src/circuit/server/reservation-voucher.d.ts.map +0 -1
- package/dist/src/circuit/server/reservation-voucher.js.map +0 -1
- package/dist/src/circuit/transport/discovery.d.ts.map +0 -1
- package/dist/src/circuit/transport/discovery.js.map +0 -1
- package/dist/src/circuit/transport/index.d.ts.map +0 -1
- package/dist/src/circuit/transport/index.js.map +0 -1
- package/dist/src/circuit/transport/listener.d.ts.map +0 -1
- package/dist/src/circuit/transport/listener.js.map +0 -1
- package/dist/src/circuit/transport/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit/transport/reservation-store.js.map +0 -1
- package/dist/src/circuit/utils.d.ts.map +0 -1
- package/dist/src/circuit/utils.js.map +0 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -122
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -321
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -1
- package/dist/typedoc-urls.json +0 -41
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -457
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
- /package/dist/src/{circuit → circuit-relay}/constants.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/constants.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/index.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/index.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/pb/index.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/pb/index.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/advert-service.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/advert-service.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/index.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/index.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/reservation-store.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/reservation-store.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/reservation-voucher.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/server/reservation-voucher.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/discovery.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/discovery.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/index.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/index.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/listener.d.ts +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/listener.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/transport/reservation-store.js +0 -0
- /package/dist/src/{circuit → circuit-relay}/utils.d.ts +0 -0
- /package/src/{circuit → circuit-relay}/constants.ts +0 -0
- /package/src/{circuit → circuit-relay}/index.ts +0 -0
- /package/src/{circuit → circuit-relay}/pb/index.proto +0 -0
- /package/src/{circuit → circuit-relay}/pb/index.ts +0 -0
- /package/src/{circuit → circuit-relay}/server/advert-service.ts +0 -0
- /package/src/{circuit → circuit-relay}/server/index.ts +0 -0
- /package/src/{circuit → circuit-relay}/server/reservation-store.ts +0 -0
- /package/src/{circuit → circuit-relay}/server/reservation-voucher.ts +0 -0
- /package/src/{circuit → circuit-relay}/transport/discovery.ts +0 -0
- /package/src/{circuit → circuit-relay}/transport/index.ts +0 -0
- /package/src/{circuit → circuit-relay}/transport/listener.ts +0 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
3
|
+
import PQueue from 'p-queue';
|
|
4
|
+
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js';
|
|
5
|
+
const log = logger('libp2p:connection-manager:auto-dial');
|
|
6
|
+
const defaultOptions = {
|
|
7
|
+
minConnections: MIN_CONNECTIONS,
|
|
8
|
+
autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
|
|
9
|
+
autoDialPriority: AUTO_DIAL_PRIORITY,
|
|
10
|
+
autoDialInterval: AUTO_DIAL_INTERVAL
|
|
11
|
+
};
|
|
12
|
+
export class AutoDial {
|
|
13
|
+
/**
|
|
14
|
+
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
15
|
+
* It will keep the number of connections below the upper limit and sort
|
|
16
|
+
* the peers to connect based on whether we know their keys and protocols.
|
|
17
|
+
*/
|
|
18
|
+
constructor(components, init) {
|
|
19
|
+
this.connectionManager = components.connectionManager;
|
|
20
|
+
this.peerStore = components.peerStore;
|
|
21
|
+
this.minConnections = init.minConnections ?? defaultOptions.minConnections;
|
|
22
|
+
this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority;
|
|
23
|
+
this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval;
|
|
24
|
+
this.started = false;
|
|
25
|
+
this.queue = new PQueue({
|
|
26
|
+
concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
|
|
27
|
+
});
|
|
28
|
+
this.queue.addListener('error', (err) => {
|
|
29
|
+
log.error('error during auto-dial', err);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
isStarted() {
|
|
33
|
+
return this.started;
|
|
34
|
+
}
|
|
35
|
+
start() {
|
|
36
|
+
this.autoDialInterval = setInterval(() => {
|
|
37
|
+
this.autoDial()
|
|
38
|
+
.catch(err => {
|
|
39
|
+
log.error('error while autodialing', err);
|
|
40
|
+
});
|
|
41
|
+
}, this.autoDialIntervalMs);
|
|
42
|
+
this.started = true;
|
|
43
|
+
}
|
|
44
|
+
afterStart() {
|
|
45
|
+
this.autoDial()
|
|
46
|
+
.catch(err => {
|
|
47
|
+
log.error('error while autodialing', err);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
stop() {
|
|
51
|
+
// clear the queue
|
|
52
|
+
this.queue.clear();
|
|
53
|
+
clearInterval(this.autoDialInterval);
|
|
54
|
+
this.started = false;
|
|
55
|
+
}
|
|
56
|
+
async autoDial() {
|
|
57
|
+
const numConnections = this.connectionManager.getConnections().length;
|
|
58
|
+
// Already has enough connections
|
|
59
|
+
if (numConnections >= this.minConnections) {
|
|
60
|
+
log('have enough connections %d/%d', numConnections, this.minConnections);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections);
|
|
64
|
+
// Sort peers on whether we know protocols or public keys for them
|
|
65
|
+
let peers = await this.peerStore.all();
|
|
66
|
+
log('loaded %d peers from the peer store', peers.length);
|
|
67
|
+
// Remove some peers
|
|
68
|
+
peers = peers.filter((peer) => {
|
|
69
|
+
// Remove peers without addresses
|
|
70
|
+
if (peer.addresses.length === 0) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
});
|
|
75
|
+
// shuffle the peers
|
|
76
|
+
peers = peers.sort(() => Math.random() > 0.5 ? 1 : -1);
|
|
77
|
+
// Sort shuffled peers by tag value
|
|
78
|
+
const peerValues = new PeerMap();
|
|
79
|
+
for (const peer of peers) {
|
|
80
|
+
if (peerValues.has(peer.id)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const tags = await this.peerStore.getTags(peer.id);
|
|
84
|
+
// sum all tag values
|
|
85
|
+
peerValues.set(peer.id, (tags ?? []).reduce((acc, curr) => {
|
|
86
|
+
return acc + curr.value;
|
|
87
|
+
}, 0));
|
|
88
|
+
}
|
|
89
|
+
// sort by value, highest to lowest
|
|
90
|
+
const sortedPeers = peers.sort((a, b) => {
|
|
91
|
+
const peerAValue = peerValues.get(a.id) ?? 0;
|
|
92
|
+
const peerBValue = peerValues.get(b.id) ?? 0;
|
|
93
|
+
if (peerAValue > peerBValue) {
|
|
94
|
+
return -1;
|
|
95
|
+
}
|
|
96
|
+
if (peerAValue < peerBValue) {
|
|
97
|
+
return 1;
|
|
98
|
+
}
|
|
99
|
+
return 0;
|
|
100
|
+
});
|
|
101
|
+
log('selected %d peers to dial', sortedPeers.length);
|
|
102
|
+
for (const peer of sortedPeers) {
|
|
103
|
+
this.queue.add(async () => {
|
|
104
|
+
const numConnections = this.connectionManager.getConnections().length;
|
|
105
|
+
// Check to see if we still need to auto dial
|
|
106
|
+
if (numConnections >= this.minConnections) {
|
|
107
|
+
log('got enough connections now %d/%d', numConnections, this.minConnections);
|
|
108
|
+
this.queue.clear();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
log('connecting to a peerStore stored peer %p', peer.id);
|
|
112
|
+
await this.connectionManager.openConnection(peer.id, {
|
|
113
|
+
// @ts-expect-error needs adding to the ConnectionManager interface
|
|
114
|
+
priority: this.autoDialPriority
|
|
115
|
+
});
|
|
116
|
+
}).catch(err => {
|
|
117
|
+
log.error('could not connect to peerStore stored peer', err);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# s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ore } from '@libp2p/interface-peer-store';
|
|
3
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
4
|
+
interface ConnectionPrunerInit {
|
|
5
|
+
maxConnections?: number;
|
|
6
|
+
allow?: Multiaddr[];
|
|
7
|
+
}
|
|
8
|
+
interface ConnectionPrunerComponents {
|
|
9
|
+
connectionManager: ConnectionManager;
|
|
10
|
+
peerStore: PeerStore;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* If we go over the max connections limit, choose some connections to close
|
|
14
|
+
*/
|
|
15
|
+
export declare class ConnectionPruner {
|
|
16
|
+
private readonly maxConnections;
|
|
17
|
+
private readonly connectionManager;
|
|
18
|
+
private readonly peerStore;
|
|
19
|
+
private readonly allow;
|
|
20
|
+
constructor(components: ConnectionPrunerComponents, init?: ConnectionPrunerInit);
|
|
21
|
+
/**
|
|
22
|
+
* If we have more connections than our maximum, select some excess connections
|
|
23
|
+
* to prune based on peer value
|
|
24
|
+
*/
|
|
25
|
+
maybePruneConnections(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=connection-pruner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-pruner.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/connection-pruner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAE7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,UAAU,oBAAoB;IAC5B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,KAAK,CAAC,EAAE,SAAS,EAAE,CAAA;CACpB;AAED,UAAU,0BAA0B;IAClC,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,SAAS,EAAE,SAAS,CAAA;CACrB;AAOD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAQ;IACvC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;gBAEtB,UAAU,EAAE,0BAA0B,EAAE,IAAI,GAAE,oBAAyB;IAOpF;;;OAGG;IACG,qBAAqB,IAAK,OAAO,CAAC,IAAI,CAAC;CA2F9C"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
3
|
+
import { MAX_CONNECTIONS } from './constants.js';
|
|
4
|
+
import { CustomEvent } from '@libp2p/interfaces/events';
|
|
5
|
+
const log = logger('libp2p:connection-manager:connection-pruner');
|
|
6
|
+
const defaultOptions = {
|
|
7
|
+
maxConnections: MAX_CONNECTIONS,
|
|
8
|
+
allow: []
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* If we go over the max connections limit, choose some connections to close
|
|
12
|
+
*/
|
|
13
|
+
export class ConnectionPruner {
|
|
14
|
+
constructor(components, init = {}) {
|
|
15
|
+
this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections;
|
|
16
|
+
this.allow = init.allow ?? defaultOptions.allow;
|
|
17
|
+
this.connectionManager = components.connectionManager;
|
|
18
|
+
this.peerStore = components.peerStore;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* If we have more connections than our maximum, select some excess connections
|
|
22
|
+
* to prune based on peer value
|
|
23
|
+
*/
|
|
24
|
+
async maybePruneConnections() {
|
|
25
|
+
const connections = this.connectionManager.getConnections();
|
|
26
|
+
const numConnections = connections.length;
|
|
27
|
+
const toPrune = Math.max(numConnections - this.maxConnections, 0);
|
|
28
|
+
log.trace('checking max connections limit %d/%d', numConnections, this.maxConnections);
|
|
29
|
+
if (numConnections <= this.maxConnections) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune);
|
|
33
|
+
const peerValues = new PeerMap();
|
|
34
|
+
// work out peer values
|
|
35
|
+
for (const connection of connections) {
|
|
36
|
+
const remotePeer = connection.remotePeer;
|
|
37
|
+
if (peerValues.has(remotePeer)) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const tags = await this.peerStore.getTags(remotePeer);
|
|
41
|
+
// sum all tag values
|
|
42
|
+
peerValues.set(remotePeer, tags.reduce((acc, curr) => {
|
|
43
|
+
return acc + curr.value;
|
|
44
|
+
}, 0));
|
|
45
|
+
}
|
|
46
|
+
// sort by value, lowest to highest
|
|
47
|
+
const sortedConnections = connections.sort((a, b) => {
|
|
48
|
+
const peerAValue = peerValues.get(a.remotePeer) ?? 0;
|
|
49
|
+
const peerBValue = peerValues.get(b.remotePeer) ?? 0;
|
|
50
|
+
if (peerAValue > peerBValue) {
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
if (peerAValue < peerBValue) {
|
|
54
|
+
return -1;
|
|
55
|
+
}
|
|
56
|
+
// if the peers have an equal tag value then we want to close short-lived connections first
|
|
57
|
+
const connectionALifespan = a.stat.timeline.open;
|
|
58
|
+
const connectionBLifespan = b.stat.timeline.open;
|
|
59
|
+
if (connectionALifespan < connectionBLifespan) {
|
|
60
|
+
return 1;
|
|
61
|
+
}
|
|
62
|
+
if (connectionALifespan > connectionBLifespan) {
|
|
63
|
+
return -1;
|
|
64
|
+
}
|
|
65
|
+
return 0;
|
|
66
|
+
});
|
|
67
|
+
// close some connections
|
|
68
|
+
const toClose = [];
|
|
69
|
+
for (const connection of sortedConnections) {
|
|
70
|
+
log('too many connections open - closing a connection to %p', connection.remotePeer);
|
|
71
|
+
// check allow list
|
|
72
|
+
const connectionInAllowList = this.allow.some((ma) => {
|
|
73
|
+
return connection.remoteAddr.toString().startsWith(ma.toString());
|
|
74
|
+
});
|
|
75
|
+
// Connections in the allow list should be excluded from pruning
|
|
76
|
+
if (!connectionInAllowList) {
|
|
77
|
+
toClose.push(connection);
|
|
78
|
+
}
|
|
79
|
+
if (toClose.length === toPrune) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// close connections
|
|
84
|
+
await Promise.all(toClose.map(async (connection) => {
|
|
85
|
+
try {
|
|
86
|
+
await connection.close();
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
log.error(err);
|
|
90
|
+
}
|
|
91
|
+
}));
|
|
92
|
+
// despatch prune event
|
|
93
|
+
this.connectionManager.dispatchEvent(new CustomEvent('peer:prune', { detail: toClose.map(conn => conn.remotePeer) }));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=connection-pruner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-pruner.js","sourceRoot":"","sources":["../../../src/connection-manager/connection-pruner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAGvC,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AAEvD,MAAM,GAAG,GAAG,MAAM,CAAC,6CAA6C,CAAC,CAAA;AAYjE,MAAM,cAAc,GAAG;IACrB,cAAc,EAAE,eAAe;IAC/B,KAAK,EAAE,EAAE;CACV,CAAA;AAED;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAM3B,YAAa,UAAsC,EAAE,OAA6B,EAAE;QAClF,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,cAAc,CAAA;QAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,cAAc,CAAC,KAAK,CAAA;QAC/C,IAAI,CAAC,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;aACN;SACF;QAED,oBAAoB;QACpB,MAAM,OAAO,CAAC,GAAG,CACf,OAAO,CAAC,GAAG,CAAC,KAAK,EAAC,UAAU,EAAC,EAAE;YAC7B,IAAI;gBACF,MAAM,UAAU,CAAC,KAAK,EAAE,CAAA;aACzB;YAAC,OAAO,GAAG,EAAE;gBACZ,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;aACf;QACH,CAAC,CAAC,CACH,CAAA;QAED,uBAAuB;QACvB,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;IACvH,CAAC;CACF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
|
|
3
|
+
*/
|
|
4
|
+
export declare const DIAL_TIMEOUT = 30000;
|
|
5
|
+
/**
|
|
6
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
|
|
7
|
+
*/
|
|
8
|
+
export declare const INBOUND_UPGRADE_TIMEOUT = 30000;
|
|
9
|
+
/**
|
|
10
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
|
|
11
|
+
*/
|
|
12
|
+
export declare const MAX_PARALLEL_DIALS = 100;
|
|
13
|
+
/**
|
|
14
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
|
|
15
|
+
*/
|
|
16
|
+
export declare const MAX_PEER_ADDRS_TO_DIAL = 25;
|
|
17
|
+
/**
|
|
18
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
|
|
19
|
+
*/
|
|
20
|
+
export declare const MAX_PARALLEL_DIALS_PER_PEER = 10;
|
|
21
|
+
/**
|
|
22
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
|
|
23
|
+
*/
|
|
24
|
+
export declare const MIN_CONNECTIONS = 50;
|
|
25
|
+
/**
|
|
26
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxConnections
|
|
27
|
+
*/
|
|
28
|
+
export declare const MAX_CONNECTIONS = 300;
|
|
29
|
+
/**
|
|
30
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
|
|
31
|
+
*/
|
|
32
|
+
export declare const AUTO_DIAL_INTERVAL = 5000;
|
|
33
|
+
/**
|
|
34
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
|
|
35
|
+
*/
|
|
36
|
+
export declare const AUTO_DIAL_CONCURRENCY = 25;
|
|
37
|
+
/**
|
|
38
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
|
|
39
|
+
*/
|
|
40
|
+
export declare const AUTO_DIAL_PRIORITY = 0;
|
|
41
|
+
/**
|
|
42
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
|
|
43
|
+
*/
|
|
44
|
+
export declare const INBOUND_CONNECTION_THRESHOLD = 5;
|
|
45
|
+
/**
|
|
46
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxIncomingPendingConnections
|
|
47
|
+
*/
|
|
48
|
+
export declare const MAX_INCOMING_PENDING_CONNECTIONS = 10;
|
|
49
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/constants.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,eAAO,MAAM,YAAY,QAAO,CAAA;AAEhC;;GAEG;AACH,eAAO,MAAM,uBAAuB,QAAO,CAAA;AAE3C;;GAEG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC;;GAEG;AACH,eAAO,MAAM,sBAAsB,KAAK,CAAA;AAExC;;GAEG;AACH,eAAO,MAAM,2BAA2B,KAAK,CAAA;AAE7C;;GAEG;AACH,eAAO,MAAM,eAAe,KAAK,CAAA;AAEjC;;GAEG;AACH,eAAO,MAAM,eAAe,MAAM,CAAA;AAElC;;GAEG;AACH,eAAO,MAAM,kBAAkB,OAAO,CAAA;AAEtC;;GAEG;AACH,eAAO,MAAM,qBAAqB,KAAK,CAAA;AAEvC;;GAEG;AACH,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAEnC;;GAEG;AACH,eAAO,MAAM,4BAA4B,IAAI,CAAA;AAE7C;;GAEG;AACH,eAAO,MAAM,gCAAgC,KAAK,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
|
|
3
|
+
*/
|
|
4
|
+
export const DIAL_TIMEOUT = 30e3;
|
|
5
|
+
/**
|
|
6
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
|
|
7
|
+
*/
|
|
8
|
+
export const INBOUND_UPGRADE_TIMEOUT = 30e3;
|
|
9
|
+
/**
|
|
10
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
|
|
11
|
+
*/
|
|
12
|
+
export const MAX_PARALLEL_DIALS = 100;
|
|
13
|
+
/**
|
|
14
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
|
|
15
|
+
*/
|
|
16
|
+
export const MAX_PEER_ADDRS_TO_DIAL = 25;
|
|
17
|
+
/**
|
|
18
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
|
|
19
|
+
*/
|
|
20
|
+
export const MAX_PARALLEL_DIALS_PER_PEER = 10;
|
|
21
|
+
/**
|
|
22
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
|
|
23
|
+
*/
|
|
24
|
+
export const MIN_CONNECTIONS = 50;
|
|
25
|
+
/**
|
|
26
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxConnections
|
|
27
|
+
*/
|
|
28
|
+
export const MAX_CONNECTIONS = 300;
|
|
29
|
+
/**
|
|
30
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
|
|
31
|
+
*/
|
|
32
|
+
export const AUTO_DIAL_INTERVAL = 5000;
|
|
33
|
+
/**
|
|
34
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
|
|
35
|
+
*/
|
|
36
|
+
export const AUTO_DIAL_CONCURRENCY = 25;
|
|
37
|
+
/**
|
|
38
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
|
|
39
|
+
*/
|
|
40
|
+
export const AUTO_DIAL_PRIORITY = 0;
|
|
41
|
+
/**
|
|
42
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
|
|
43
|
+
*/
|
|
44
|
+
export const INBOUND_CONNECTION_THRESHOLD = 5;
|
|
45
|
+
/**
|
|
46
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxIncomingPendingConnections
|
|
47
|
+
*/
|
|
48
|
+
export const MAX_INCOMING_PENDING_CONNECTIONS = 10;
|
|
49
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/connection-manager/constants.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAA;AAEhC;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAA;AAE3C;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAA;AAExC;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE7C;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAA;AAEjC;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAA;AAElC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,CAAA;AAEtC;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAA;AAEvC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,CAAA;AAE7C;;GAEG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,EAAE,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Multiaddr, Resolver } from '@multiformats/multiaddr';
|
|
2
|
+
import type { Connection } from '@libp2p/interface-connection';
|
|
3
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
4
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
5
|
+
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store';
|
|
6
|
+
import type { Metrics } from '@libp2p/interface-metrics';
|
|
7
|
+
import type { TransportManager } from '@libp2p/interface-transport';
|
|
8
|
+
import type { ConnectionGater } from '@libp2p/interface-connection-gater';
|
|
9
|
+
import PQueue from 'p-queue';
|
|
10
|
+
export type PendingDialStatus = 'queued' | 'active' | 'error' | 'success';
|
|
11
|
+
export interface PendingDial {
|
|
12
|
+
id: string;
|
|
13
|
+
status: PendingDialStatus;
|
|
14
|
+
peerId?: PeerId;
|
|
15
|
+
multiaddrs: Multiaddr[];
|
|
16
|
+
promise: Promise<Connection>;
|
|
17
|
+
}
|
|
18
|
+
export interface PendingDialTarget {
|
|
19
|
+
resolve: (value: any) => void;
|
|
20
|
+
reject: (err: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface DialOptions extends AbortOptions {
|
|
23
|
+
priority?: number;
|
|
24
|
+
}
|
|
25
|
+
interface DialerInit {
|
|
26
|
+
addressSorter?: AddressSorter;
|
|
27
|
+
maxParallelDials?: number;
|
|
28
|
+
maxPeerAddrsToDial?: number;
|
|
29
|
+
maxParallelDialsPerPeer?: number;
|
|
30
|
+
dialTimeout?: number;
|
|
31
|
+
resolvers?: Record<string, Resolver>;
|
|
32
|
+
}
|
|
33
|
+
interface DialQueueComponents {
|
|
34
|
+
peerId: PeerId;
|
|
35
|
+
metrics?: Metrics;
|
|
36
|
+
peerStore: PeerStore;
|
|
37
|
+
transportManager: TransportManager;
|
|
38
|
+
connectionGater: ConnectionGater;
|
|
39
|
+
}
|
|
40
|
+
export declare class DialQueue {
|
|
41
|
+
pendingDials: PendingDial[];
|
|
42
|
+
queue: PQueue;
|
|
43
|
+
private readonly peerId;
|
|
44
|
+
private readonly peerStore;
|
|
45
|
+
private readonly connectionGater;
|
|
46
|
+
private readonly transportManager;
|
|
47
|
+
private readonly addressSorter;
|
|
48
|
+
private readonly maxPeerAddrsToDial;
|
|
49
|
+
private readonly maxParallelDialsPerPeer;
|
|
50
|
+
private readonly dialTimeout;
|
|
51
|
+
private readonly inProgressDialCount?;
|
|
52
|
+
private readonly pendingDialCount?;
|
|
53
|
+
private readonly shutDownController;
|
|
54
|
+
constructor(components: DialQueueComponents, init?: DialerInit);
|
|
55
|
+
/**
|
|
56
|
+
* Clears any pending dials
|
|
57
|
+
*/
|
|
58
|
+
stop(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Connects to a given peer, multiaddr or list of multiaddrs.
|
|
61
|
+
*
|
|
62
|
+
* If a peer is passed, all known multiaddrs will be tried. If a multiaddr or
|
|
63
|
+
* multiaddrs are passed only those will be dialled.
|
|
64
|
+
*
|
|
65
|
+
* Where a list of multiaddrs is passed, if any contain a peer id then all
|
|
66
|
+
* multiaddrs in the list must contain the same peer id.
|
|
67
|
+
*
|
|
68
|
+
* The dial to the first address that is successfully able to upgrade a connection
|
|
69
|
+
* will be used, all other dials will be aborted when that happens.
|
|
70
|
+
*/
|
|
71
|
+
dial(peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options?: DialOptions): Promise<Connection>;
|
|
72
|
+
private createDialAbortControllers;
|
|
73
|
+
private calculateMultiaddrs;
|
|
74
|
+
private performDial;
|
|
75
|
+
}
|
|
76
|
+
export {};
|
|
77
|
+
//# sourceMappingURL=dial-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dial-queue.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/dial-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAa,MAAM,yBAAyB,CAAA;AAUxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAEvD,OAAO,KAAK,EAAW,aAAa,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AACrF,OAAO,KAAK,EAAU,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAChE,OA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