libp2p 0.45.5 → 0.45.6

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.
Files changed (41) hide show
  1. package/dist/index.min.js +21 -21
  2. package/dist/src/autonat/index.js +4 -4
  3. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  4. package/dist/src/circuit-relay/transport/discovery.js +11 -4
  5. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  6. package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -0
  7. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  8. package/dist/src/circuit-relay/transport/reservation-store.js +14 -2
  9. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  10. package/dist/src/connection-manager/auto-dial.d.ts +3 -0
  11. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  12. package/dist/src/connection-manager/auto-dial.js +64 -27
  13. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  14. package/dist/src/connection-manager/constants.d.ts +4 -0
  15. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  16. package/dist/src/connection-manager/constants.js +4 -0
  17. package/dist/src/connection-manager/constants.js.map +1 -1
  18. package/dist/src/connection-manager/index.d.ts +5 -0
  19. package/dist/src/connection-manager/index.d.ts.map +1 -1
  20. package/dist/src/connection-manager/index.js +11 -10
  21. package/dist/src/connection-manager/index.js.map +1 -1
  22. package/dist/src/upnp-nat/index.d.ts +1 -1
  23. package/dist/src/upnp-nat/index.d.ts.map +1 -1
  24. package/dist/src/upnp-nat/index.js +3 -3
  25. package/dist/src/upnp-nat/index.js.map +1 -1
  26. package/dist/src/utils/peer-job-queue.d.ts +33 -0
  27. package/dist/src/utils/peer-job-queue.d.ts.map +1 -0
  28. package/dist/src/utils/peer-job-queue.js +82 -0
  29. package/dist/src/utils/peer-job-queue.js.map +1 -0
  30. package/dist/src/version.d.ts +1 -1
  31. package/dist/src/version.js +1 -1
  32. package/package.json +7 -6
  33. package/src/autonat/index.ts +4 -4
  34. package/src/circuit-relay/transport/discovery.ts +11 -4
  35. package/src/circuit-relay/transport/reservation-store.ts +22 -3
  36. package/src/connection-manager/auto-dial.ts +75 -31
  37. package/src/connection-manager/constants.ts +5 -0
  38. package/src/connection-manager/index.ts +18 -12
  39. package/src/upnp-nat/index.ts +3 -3
  40. package/src/utils/peer-job-queue.ts +119 -0
  41. package/src/version.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/upnp-nat/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,8BAA8B,CAAA;AASnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACnE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAS7D,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IAErB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IAEnB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,cAAM,OAAQ,YAAW,SAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,U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PNAT {
76
76
  // eslint-disable-next-line no-continue
77
77
  continue;
78
78
  }
79
- const client = await this._getClient();
79
+ const client = this._getClient();
80
80
  const publicIp = this.externalAddress ?? await client.externalIp();
81
81
  const isPrivate = isPrivateIp(publicIp);
82
82
  if (isPrivate === true) {
@@ -100,11 +100,11 @@ class UPnPNAT {
100
100
  }, transport));
101
101
  }
102
102
  }
103
- async _getClient() {
103
+ _getClient() {
104
104
  if (this.client != null) {
105
105
  return this.client;
106
106
  }
107
- this.client = await upnpNat({
107
+ this.client = upnpNat({
108
108
  description: this.description,
109
109
  ttl: this.ttl,
110
110
  keepAlive: this.keepAlive,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/upnp-nat/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,8BAA8B,CAAA;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,WAAW,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AACpC,OAAO,KAAK,GAAG,MAAM,eAAe,CAAA;AAMpC,MAAM,GAAG,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAA;AACrC,MAAM,WAAW,GAAG,IAAI,CAAA;AAExB,SAAS,QAAQ,CAAE,GAAG,GAAG,IAAI,EAAE,GAAG,GAAG,KAAK;IACxC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA;AAC1D,CAAC;AA+CD,MAAM,OAAO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U;QACR,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE;YACvB,OAAO,IAAI,CAAC,MAAM,CAAA;SACnB;QAED,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;YACpB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC,CAAA;QAEF,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,IAAI,SAAS,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE;YACpC,OAAM;SACP;QAED,IAAI;YACF,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAA;YACzB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;SACxB;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;SACf;IACH,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAAE,OAAoB,EAAE;IACpD,OAAO,CAAC,UAA6B,EAAE,EAAE;QACvC,OAAO,IAAI,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,33 @@
1
+ import PQueue from 'p-queue';
2
+ import type { PeerId } from '@libp2p/interface-peer-id';
3
+ import type { QueueAddOptions, Options, Queue } from 'p-queue';
4
+ interface RunFunction {
5
+ (): Promise<unknown>;
6
+ }
7
+ export interface PeerPriorityQueueOptions extends QueueAddOptions {
8
+ peerId: PeerId;
9
+ }
10
+ /**
11
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
12
+ * that adds support for filtering jobs by peer id
13
+ */
14
+ declare class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
15
+ #private;
16
+ enqueue(run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void;
17
+ dequeue(): RunFunction | undefined;
18
+ filter(options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[];
19
+ get size(): number;
20
+ }
21
+ /**
22
+ * Extends PQueue to add support for querying queued jobs by peer id
23
+ */
24
+ export declare class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
25
+ constructor(options?: Options<PeerPriorityQueue, PeerPriorityQueueOptions>);
26
+ /**
27
+ * Returns true if this queue has a job for the passed peer id that has not yet
28
+ * started to run
29
+ */
30
+ hasJob(peerId: PeerId): boolean;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=peer-job-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.d.ts","sourceRoot":"","sources":["../../../src/utils/peer-job-queue.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,SAAS,CAAA;AAE5B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAuB9D,UAAU,WAAW;IAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;CAAE;AAE9C,MAAM,WAAW,wBAAyB,SAAQ,eAAe;IAC/D,MAAM,EAAE,MAAM,CAAA;CACf;AAQD;;;GAGG;AACH,cAAM,iBAAkB,YAAW,KAAK,CAAC,WAAW,EAAE,wBAAwB,CAAC;;IAG7E,OAAO,CAAE,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,IAAI;IA0B7E,OAAO,IAAK,WAAW,GAAG,SAAS;IAKnC,MAAM,CAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,GAAG,WAAW,EAAE;IAc5E,IAAI,IAAI,IAAK,MAAM,CAElB;CACF;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,MAAM,CAAC,iBAAiB,EAAE,wBAAwB,CAAC;gBACtE,OAAO,GAAE,OAAO,CAAC,iBAAiB,EAAE,wBAAwB,CAAM;IAO/E;;;OAGG;IACH,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;CAKjC"}
@@ -0,0 +1,82 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import { CodeError } from '@libp2p/interfaces/errors';
3
+ import PQueue from 'p-queue';
4
+ import { codes } from '../errors.js';
5
+ // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
6
+ // Used to compute insertion index to keep queue sorted after insertion
7
+ function lowerBound(array, value, comparator) {
8
+ let first = 0;
9
+ let count = array.length;
10
+ while (count > 0) {
11
+ const step = Math.trunc(count / 2);
12
+ let it = first + step;
13
+ if (comparator(array[it], value) <= 0) {
14
+ first = ++it;
15
+ count -= step + 1;
16
+ }
17
+ else {
18
+ count = step;
19
+ }
20
+ }
21
+ return first;
22
+ }
23
+ /**
24
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
25
+ * that adds support for filtering jobs by peer id
26
+ */
27
+ class PeerPriorityQueue {
28
+ #queue = [];
29
+ enqueue(run, options) {
30
+ const peerId = options?.peerId;
31
+ const priority = options?.priority ?? 0;
32
+ if (peerId == null) {
33
+ throw new CodeError('missing peer id', codes.ERR_INVALID_PARAMETERS);
34
+ }
35
+ const element = {
36
+ priority,
37
+ peerId,
38
+ run
39
+ };
40
+ if (this.size > 0 && this.#queue[this.size - 1].priority >= priority) {
41
+ this.#queue.push(element);
42
+ return;
43
+ }
44
+ const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority);
45
+ this.#queue.splice(index, 0, element);
46
+ }
47
+ dequeue() {
48
+ const item = this.#queue.shift();
49
+ return item?.run;
50
+ }
51
+ filter(options) {
52
+ if (options.peerId != null) {
53
+ const peerId = options.peerId;
54
+ return this.#queue.filter((element) => peerId.equals(element.peerId)).map((element) => element.run);
55
+ }
56
+ return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
57
+ }
58
+ get size() {
59
+ return this.#queue.length;
60
+ }
61
+ }
62
+ /**
63
+ * Extends PQueue to add support for querying queued jobs by peer id
64
+ */
65
+ export class PeerJobQueue extends PQueue {
66
+ constructor(options = {}) {
67
+ super({
68
+ ...options,
69
+ queueClass: PeerPriorityQueue
70
+ });
71
+ }
72
+ /**
73
+ * Returns true if this queue has a job for the passed peer id that has not yet
74
+ * started to run
75
+ */
76
+ hasJob(peerId) {
77
+ return this.sizeBy({
78
+ peerId
79
+ }) > 0;
80
+ }
81
+ }
82
+ //# sourceMappingURL=peer-job-queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.js","sourceRoot":"","sources":["../../../src/utils/peer-job-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAIpC,mFAAmF;AACnF,uEAAuE;AACvE,SAAS,UAAU,CAAK,KAAmB,EAAE,KAAQ,EAAE,UAAkC;IACvF,IAAI,KAAK,GAAG,CAAC,CAAA;IACb,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,CAAA;IAExB,OAAO,KAAK,GAAG,CAAC,EAAE;QAChB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAA;QAClC,IAAI,EAAE,GAAG,KAAK,GAAG,IAAI,CAAA;QAErB,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE;YACtC,KAAK,GAAG,EAAE,EAAE,CAAA;YACZ,KAAK,IAAI,IAAI,GAAG,CAAC,CAAA;SAClB;aAAM;YACL,KAAK,GAAG,IAAI,CAAA;SACb;KACF;IAED,OAAO,KAAK,CAAA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,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAC/E,CAAC,GAAG,CAAC,CAAC,OAAuC,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;SAChE;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CACvB,CAAC,OAA2C,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,QAAQ,CACvF,CAAC,GAAG,CAAC,CAAC,OAAuC,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACjE,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAA;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,MAAmD;IACnF,YAAa,UAAgE,EAAE;QAC7E,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,UAAU,EAAE,iBAAiB;SAC9B,CAAC,CAAA;IACJ,CAAC;IAED;;;OAGG;IACH,MAAM,CAAE,MAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC;YACjB,MAAM;SACP,CAAC,GAAG,CAAC,CAAA;IACR,CAAC;CACF"}
@@ -1,3 +1,3 @@
1
- export declare const version = "0.45.5";
1
+ export declare const version = "0.45.6";
2
2
  export declare const name = "libp2p";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -1,3 +1,3 @@
1
- export const version = '0.45.5';
1
+ export const version = '0.45.6';
2
2
  export const name = 'libp2p';
3
3
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.45.5",
3
+ "version": "0.45.6",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -115,12 +115,13 @@
115
115
  "test:chrome-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\"",
116
116
  "test:firefox": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
117
117
  "test:firefox-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
118
+ "test:webkit": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser webkit",
118
119
  "test:examples": "cd examples && npm run test:all",
119
120
  "test:interop": "aegir test -t node -f dist/test/interop.js"
120
121
  },
121
122
  "dependencies": {
122
- "@achingbrain/nat-port-mapper": "^1.0.3",
123
- "@libp2p/crypto": "^1.0.4",
123
+ "@achingbrain/nat-port-mapper": "^1.0.9",
124
+ "@libp2p/crypto": "^1.0.17",
124
125
  "@libp2p/interface-address-manager": "^3.0.0",
125
126
  "@libp2p/interface-connection": "^5.0.0",
126
127
  "@libp2p/interface-connection-encrypter": "^4.0.0",
@@ -134,7 +135,7 @@
134
135
  "@libp2p/interface-peer-id": "^2.0.1",
135
136
  "@libp2p/interface-peer-info": "^1.0.3",
136
137
  "@libp2p/interface-peer-routing": "^1.1.0",
137
- "@libp2p/interface-peer-store": "^2.0.3",
138
+ "@libp2p/interface-peer-store": "^2.0.4",
138
139
  "@libp2p/interface-pubsub": "^4.0.0",
139
140
  "@libp2p/interface-record": "^2.0.6",
140
141
  "@libp2p/interface-registrar": "^2.0.3",
@@ -142,13 +143,13 @@
142
143
  "@libp2p/interface-transport": "^4.0.0",
143
144
  "@libp2p/interfaces": "^3.2.0",
144
145
  "@libp2p/keychain": "^2.0.0",
145
- "@libp2p/logger": "^2.0.1",
146
+ "@libp2p/logger": "^2.1.1",
146
147
  "@libp2p/multistream-select": "^3.1.8",
147
148
  "@libp2p/peer-collections": "^3.0.0",
148
149
  "@libp2p/peer-id": "^2.0.0",
149
150
  "@libp2p/peer-id-factory": "^2.0.0",
150
151
  "@libp2p/peer-record": "^5.0.0",
151
- "@libp2p/peer-store": "^8.1.0",
152
+ "@libp2p/peer-store": "^8.2.0",
152
153
  "@libp2p/topology": "^4.0.1",
153
154
  "@libp2p/tracked-map": "^3.0.0",
154
155
  "@libp2p/utils": "^3.0.10",
@@ -508,7 +508,7 @@ class DefaultAutoNATService implements Startable {
508
508
  // they either told us which address worked/didn't work, or we only sent them one address
509
509
  const addr = dialResponse.addr == null ? multiaddrs[0] : multiaddr(dialResponse.addr)
510
510
 
511
- log('autonat response for %s is %s', addr, dialResponse.status)
511
+ log('autonat response for %a is %s', addr, dialResponse.status)
512
512
 
513
513
  if (dialResponse.status === Message.ResponseStatus.E_BAD_REQUEST) {
514
514
  // the remote could not parse our request
@@ -526,7 +526,7 @@ class DefaultAutoNATService implements Startable {
526
526
  }
527
527
 
528
528
  if (!multiaddrs.some(ma => ma.equals(addr))) {
529
- log('peer reported %s as %s but it was not in our observed address list', addr, dialResponse.status)
529
+ log('peer reported %a as %s but it was not in our observed address list', addr, dialResponse.status)
530
530
  continue
531
531
  }
532
532
 
@@ -544,14 +544,14 @@ class DefaultAutoNATService implements Startable {
544
544
 
545
545
  if (results[addrStr].success === REQUIRED_SUCCESSFUL_DIALS) {
546
546
  // we are now convinced
547
- log('%s is externally dialable', addr)
547
+ log('%a is externally dialable', addr)
548
548
  addressManager.confirmObservedAddr(addr)
549
549
  return
550
550
  }
551
551
 
552
552
  if (results[addrStr].failure === REQUIRED_SUCCESSFUL_DIALS) {
553
553
  // we are now unconvinced
554
- log('%s is not externally dialable', addr)
554
+ log('%a is not externally dialable', addr)
555
555
  addressManager.removeObservedAddr(addr)
556
556
  return
557
557
  }
@@ -89,10 +89,17 @@ export class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implement
89
89
  */
90
90
  async discover (): Promise<void> {
91
91
  log('searching peer store for relays')
92
- const peers = (await this.peerStore.all())
93
- // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
94
- .filter(({ protocols }) => protocols.includes(RELAY_V2_HOP_CODEC))
95
- .sort(() => Math.random() - 0.5)
92
+ const peers = (await this.peerStore.all({
93
+ filters: [
94
+ // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
95
+ (peer) => {
96
+ return peer.protocols.includes(RELAY_V2_HOP_CODEC)
97
+ }
98
+ ],
99
+ orders: [
100
+ () => Math.random() < 0.5 ? 1 : -1
101
+ ]
102
+ }))
96
103
 
97
104
  for (const peer of peers) {
98
105
  log('found relay peer %p in content peer store', peer.id)
@@ -3,7 +3,7 @@ import { logger } from '@libp2p/logger'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
4
  import { multiaddr } from '@multiformats/multiaddr'
5
5
  import { pbStream } from 'it-pb-stream'
6
- import PQueue from 'p-queue'
6
+ import { PeerJobQueue } from '../../utils/peer-job-queue.js'
7
7
  import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js'
8
8
  import { HopMessage, Status } from '../pb/index.js'
9
9
  import { getExpirationMilliseconds } from '../utils.js'
@@ -50,6 +50,11 @@ export interface RelayStoreInit {
50
50
  * How many discovered relays to allow in the reservation store
51
51
  */
52
52
  discoverRelays?: number
53
+
54
+ /**
55
+ * Limit the number of potential relays we will dial (default: 100)
56
+ */
57
+ maxReservationQueueLength?: number
53
58
  }
54
59
 
55
60
  export type RelayType = 'discovered' | 'configured'
@@ -71,9 +76,10 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
71
76
  private readonly transportManager: TransportManager
72
77
  private readonly peerStore: PeerStore
73
78
  private readonly events: EventEmitter<Libp2pEvents>
74
- private readonly reserveQueue: PQueue
79
+ private readonly reserveQueue: PeerJobQueue
75
80
  private readonly reservations: PeerMap<RelayEntry>
76
81
  private readonly maxDiscoveredRelays: number
82
+ private readonly maxReservationQueueLength: number
77
83
  private started: boolean
78
84
 
79
85
  constructor (components: RelayStoreComponents, init?: RelayStoreInit) {
@@ -86,10 +92,11 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
86
92
  this.events = components.events
87
93
  this.reservations = new PeerMap()
88
94
  this.maxDiscoveredRelays = init?.discoverRelays ?? 0
95
+ this.maxReservationQueueLength = init?.maxReservationQueueLength ?? 100
89
96
  this.started = false
90
97
 
91
98
  // ensure we don't listen on multiple relays simultaneously
92
- this.reserveQueue = new PQueue({
99
+ this.reserveQueue = new PeerJobQueue({
93
100
  concurrency: init?.reservationConcurrency ?? DEFAULT_RESERVATION_CONCURRENCY
94
101
  })
95
102
 
@@ -130,6 +137,16 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
130
137
  return
131
138
  }
132
139
 
140
+ if (this.reserveQueue.size > this.maxReservationQueueLength) {
141
+ log('not adding relay as the queue is full')
142
+ return
143
+ }
144
+
145
+ if (this.reserveQueue.hasJob(peerId)) {
146
+ log('relay peer is already in the reservation queue')
147
+ return
148
+ }
149
+
133
150
  log('add relay %p', peerId)
134
151
 
135
152
  await this.reserveQueue.add(async () => {
@@ -206,6 +223,8 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
206
223
  // if listening failed, remove the reservation
207
224
  this.reservations.delete(peerId)
208
225
  }
226
+ }, {
227
+ peerId
209
228
  })
210
229
  }
211
230
 
@@ -1,7 +1,7 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import { PeerMap, PeerSet } 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'
3
+ import { PeerJobQueue } from '../utils/peer-job-queue.js'
4
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
5
5
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
6
6
  import type { Libp2pEvents } from '@libp2p/interface-libp2p'
7
7
  import type { PeerStore } from '@libp2p/interface-peer-store'
@@ -12,6 +12,7 @@ const log = logger('libp2p:connection-manager:auto-dial')
12
12
 
13
13
  interface AutoDialInit {
14
14
  minConnections?: number
15
+ maxQueueLength?: number
15
16
  autoDialConcurrency?: number
16
17
  autoDialPriority?: number
17
18
  autoDialInterval?: number
@@ -25,6 +26,7 @@ interface AutoDialComponents {
25
26
 
26
27
  const defaultOptions = {
27
28
  minConnections: MIN_CONNECTIONS,
29
+ maxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH,
28
30
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
29
31
  autoDialPriority: AUTO_DIAL_PRIORITY,
30
32
  autoDialInterval: AUTO_DIAL_INTERVAL
@@ -33,12 +35,14 @@ const defaultOptions = {
33
35
  export class AutoDial implements Startable {
34
36
  private readonly connectionManager: ConnectionManager
35
37
  private readonly peerStore: PeerStore
36
- private readonly queue: PQueue
38
+ private readonly queue: PeerJobQueue
37
39
  private readonly minConnections: number
38
40
  private readonly autoDialPriority: number
39
41
  private readonly autoDialIntervalMs: number
42
+ private readonly autoDialMaxQueueLength: number
40
43
  private autoDialInterval?: ReturnType<typeof setInterval>
41
44
  private started: boolean
45
+ private running: boolean
42
46
 
43
47
  /**
44
48
  * Proactively tries to connect to known peers stored in the PeerStore.
@@ -51,8 +55,10 @@ export class AutoDial implements Startable {
51
55
  this.minConnections = init.minConnections ?? defaultOptions.minConnections
52
56
  this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
53
57
  this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
58
+ this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
54
59
  this.started = false
55
- this.queue = new PQueue({
60
+ this.running = false
61
+ this.queue = new PeerJobQueue({
56
62
  concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
57
63
  })
58
64
  this.queue.addListener('error', (err) => {
@@ -73,7 +79,7 @@ export class AutoDial implements Startable {
73
79
  }
74
80
 
75
81
  start (): void {
76
- this.autoDialInterval = setInterval(() => {
82
+ this.autoDialInterval = setTimeout(() => {
77
83
  this.autoDial()
78
84
  .catch(err => {
79
85
  log.error('error while autodialing', err)
@@ -92,8 +98,9 @@ export class AutoDial implements Startable {
92
98
  stop (): void {
93
99
  // clear the queue
94
100
  this.queue.clear()
95
- clearInterval(this.autoDialInterval)
101
+ clearTimeout(this.autoDialInterval)
96
102
  this.started = false
103
+ this.running = false
97
104
  }
98
105
 
99
106
  async autoDial (): Promise<void> {
@@ -103,12 +110,6 @@ export class AutoDial implements Startable {
103
110
 
104
111
  const connections = this.connectionManager.getConnectionsMap()
105
112
  const numConnections = connections.size
106
- const dialQueue = new PeerSet(
107
- // @ts-expect-error boolean filter removes falsy peer IDs
108
- this.connectionManager.getDialQueue()
109
- .map(queue => queue.peerId)
110
- .filter(Boolean)
111
- )
112
113
 
113
114
  // Already has enough connections
114
115
  if (numConnections >= this.minConnections) {
@@ -116,34 +117,64 @@ export class AutoDial implements Startable {
116
117
  return
117
118
  }
118
119
 
119
- log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
120
+ if (this.queue.size > this.autoDialMaxQueueLength) {
121
+ log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
122
+ return
123
+ }
120
124
 
121
- // Sort peers on whether we know protocols or public keys for them
122
- const peers = await this.peerStore.all()
125
+ if (this.running) {
126
+ log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
127
+ return
128
+ }
123
129
 
124
- // Remove some peers
125
- const filteredPeers = peers.filter((peer) => {
126
- // Remove peers without addresses
127
- if (peer.addresses.length === 0) {
128
- return false
129
- }
130
+ this.running = true
130
131
 
131
- // remove peers we are already connected to
132
- if (connections.has(peer.id)) {
133
- return false
134
- }
132
+ log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
135
133
 
136
- // remove peers we are already dialling
137
- if (dialQueue.has(peer.id)) {
138
- return false
139
- }
134
+ const dialQueue = new PeerSet(
135
+ // @ts-expect-error boolean filter removes falsy peer IDs
136
+ this.connectionManager.getDialQueue()
137
+ .map(queue => queue.peerId)
138
+ .filter(Boolean)
139
+ )
140
140
 
141
- return true
141
+ // Sort peers on whether we know protocols or public keys for them
142
+ const peers = await this.peerStore.all({
143
+ filters: [
144
+ // Remove some peers
145
+ (peer) => {
146
+ // Remove peers without addresses
147
+ if (peer.addresses.length === 0) {
148
+ log.trace('not autodialing %p because they have no addresses')
149
+ return false
150
+ }
151
+
152
+ // remove peers we are already connected to
153
+ if (connections.has(peer.id)) {
154
+ log.trace('not autodialing %p because they are already connected')
155
+ return false
156
+ }
157
+
158
+ // remove peers we are already dialling
159
+ if (dialQueue.has(peer.id)) {
160
+ log.trace('not autodialing %p because they are already being dialed')
161
+ return false
162
+ }
163
+
164
+ // remove peers already in the autodial queue
165
+ if (this.queue.hasJob(peer.id)) {
166
+ log.trace('not autodialing %p because they are already being autodialed')
167
+ return false
168
+ }
169
+
170
+ return true
171
+ }
172
+ ]
142
173
  })
143
174
 
144
175
  // shuffle the peers - this is so peers with the same tag values will be
145
176
  // dialled in a different order each time
146
- const shuffledPeers = filteredPeers.sort(() => Math.random() > 0.5 ? 1 : -1)
177
+ const shuffledPeers = peers.sort(() => Math.random() > 0.5 ? 1 : -1)
147
178
 
148
179
  // Sort shuffled peers by tag value
149
180
  const peerValues = new PeerMap<number>()
@@ -192,9 +223,22 @@ export class AutoDial implements Startable {
192
223
  // @ts-expect-error needs adding to the ConnectionManager interface
193
224
  priority: this.autoDialPriority
194
225
  })
226
+ }, {
227
+ peerId: peer.id
195
228
  }).catch(err => {
196
229
  log.error('could not connect to peerStore stored peer', err)
197
230
  })
198
231
  }
232
+
233
+ this.running = false
234
+
235
+ if (this.started) {
236
+ this.autoDialInterval = setTimeout(() => {
237
+ this.autoDial()
238
+ .catch(err => {
239
+ log.error('error while autodialing', err)
240
+ })
241
+ }, this.autoDialIntervalMs)
242
+ }
199
243
  }
200
244
  }
@@ -49,6 +49,11 @@ export const AUTO_DIAL_CONCURRENCY = 25
49
49
  */
50
50
  export const AUTO_DIAL_PRIORITY = 0
51
51
 
52
+ /**
53
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialMaxQueueLength
54
+ */
55
+ export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
56
+
52
57
  /**
53
58
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
54
59
  */
@@ -10,7 +10,7 @@ import { codes } from '../errors.js'
10
10
  import { getPeerAddress } from '../get-peer.js'
11
11
  import { AutoDial } from './auto-dial.js'
12
12
  import { ConnectionPruner } from './connection-pruner.js'
13
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
13
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
14
14
  import { DialQueue } from './dial-queue.js'
15
15
  import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
16
16
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
@@ -18,7 +18,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
18
18
  import type { PendingDial, AddressSorter, Libp2pEvents } from '@libp2p/interface-libp2p'
19
19
  import type { Metrics } from '@libp2p/interface-metrics'
20
20
  import type { PeerId } from '@libp2p/interface-peer-id'
21
- import type { PeerStore } from '@libp2p/interface-peer-store'
21
+ import type { Peer, PeerStore } from '@libp2p/interface-peer-store'
22
22
  import type { TransportManager } from '@libp2p/interface-transport'
23
23
  import type { AbortOptions } from '@libp2p/interfaces'
24
24
  import type { EventEmitter } from '@libp2p/interfaces/events'
@@ -61,6 +61,12 @@ export interface ConnectionManagerInit {
61
61
  */
62
62
  autoDialPriority?: number
63
63
 
64
+ /**
65
+ * Limit the maximum number of peers to dial when trying to keep the number of
66
+ * open connections above `minConnections`. (default: 100)
67
+ */
68
+ autoDialMaxQueueLength?: number
69
+
64
70
  /**
65
71
  * Sort the known addresses of a peer before trying to dial, By default public
66
72
  * addresses will be dialled before private (e.g. loopback or LAN) addresses.
@@ -136,7 +142,8 @@ const defaultOptions = {
136
142
  inboundConnectionThreshold: INBOUND_CONNECTION_THRESHOLD,
137
143
  maxIncomingPendingConnections: MAX_INCOMING_PENDING_CONNECTIONS,
138
144
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
139
- autoDialPriority: AUTO_DIAL_PRIORITY
145
+ autoDialPriority: AUTO_DIAL_PRIORITY,
146
+ autoDialMaxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH
140
147
  }
141
148
 
142
149
  export interface DefaultConnectionManagerComponents {
@@ -217,7 +224,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
217
224
  }, {
218
225
  minConnections,
219
226
  autoDialConcurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
220
- autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority
227
+ autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority,
228
+ maxQueueLength: init.autoDialMaxQueueLength ?? defaultOptions.autoDialMaxQueueLength
221
229
  })
222
230
 
223
231
  // controls what happens when we have too many connections
@@ -344,17 +352,15 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
344
352
  // re-connect to any peers with the KEEP_ALIVE tag
345
353
  void Promise.resolve()
346
354
  .then(async () => {
347
- const keepAlivePeers: PeerId[] = []
348
-
349
- for (const peer of await this.peerStore.all()) {
350
- if (peer.tags.has(KEEP_ALIVE)) {
351
- keepAlivePeers.push(peer.id)
352
- }
353
- }
355
+ const keepAlivePeers: Peer[] = await this.peerStore.all({
356
+ filters: [(peer) => {
357
+ return peer.tags.has(KEEP_ALIVE)
358
+ }]
359
+ })
354
360
 
355
361
  await Promise.all(
356
362
  keepAlivePeers.map(async peer => {
357
- await this.openConnection(peer)
363
+ await this.openConnection(peer.id)
358
364
  .catch(err => {
359
365
  log.error(err)
360
366
  })