libp2p 0.46.4 → 0.46.5

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 (50) hide show
  1. package/dist/index.min.js +22 -22
  2. package/dist/src/connection-manager/auto-dial.d.ts +2 -0
  3. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  4. package/dist/src/connection-manager/auto-dial.js +20 -5
  5. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  6. package/dist/src/connection-manager/constants.browser.d.ts +18 -0
  7. package/dist/src/connection-manager/constants.browser.d.ts.map +1 -0
  8. package/dist/src/connection-manager/constants.browser.js +18 -0
  9. package/dist/src/connection-manager/constants.browser.js.map +1 -0
  10. package/dist/src/connection-manager/constants.d.ts +1 -36
  11. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  12. package/dist/src/connection-manager/constants.defaults.d.ts +50 -0
  13. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -0
  14. package/dist/src/connection-manager/constants.defaults.js +50 -0
  15. package/dist/src/connection-manager/constants.defaults.js.map +1 -0
  16. package/dist/src/connection-manager/constants.js +1 -36
  17. package/dist/src/connection-manager/constants.js.map +1 -1
  18. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  19. package/dist/src/connection-manager/dial-queue.js +16 -2
  20. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  21. package/dist/src/connection-manager/index.d.ts +6 -4
  22. package/dist/src/connection-manager/index.d.ts.map +1 -1
  23. package/dist/src/connection-manager/index.js +1 -1
  24. package/dist/src/connection-manager/index.js.map +1 -1
  25. package/dist/src/dcutr/dcutr.d.ts +48 -0
  26. package/dist/src/dcutr/dcutr.d.ts.map +1 -0
  27. package/dist/src/dcutr/dcutr.js +335 -0
  28. package/dist/src/dcutr/dcutr.js.map +1 -0
  29. package/dist/src/dcutr/index.d.ts +92 -0
  30. package/dist/src/dcutr/index.d.ts.map +1 -0
  31. package/dist/src/dcutr/index.js +63 -0
  32. package/dist/src/dcutr/index.js.map +1 -0
  33. package/dist/src/dcutr/pb/message.d.ts +20 -0
  34. package/dist/src/dcutr/pb/message.d.ts.map +1 -0
  35. package/dist/src/dcutr/pb/message.js +77 -0
  36. package/dist/src/dcutr/pb/message.js.map +1 -0
  37. package/dist/src/version.d.ts +1 -1
  38. package/dist/src/version.js +1 -1
  39. package/package.json +18 -12
  40. package/src/connection-manager/auto-dial.ts +26 -5
  41. package/src/connection-manager/constants.browser.ts +21 -0
  42. package/src/connection-manager/constants.defaults.ts +59 -0
  43. package/src/connection-manager/constants.ts +1 -44
  44. package/src/connection-manager/dial-queue.ts +17 -2
  45. package/src/connection-manager/index.ts +8 -6
  46. package/src/dcutr/dcutr.ts +406 -0
  47. package/src/dcutr/index.ts +102 -0
  48. package/src/dcutr/pb/message.proto +12 -0
  49. package/src/dcutr/pb/message.ts +96 -0
  50. package/src/version.ts +1 -1
@@ -0,0 +1,77 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable complexity */
3
+ /* eslint-disable @typescript-eslint/no-namespace */
4
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
6
+ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime';
7
+ export var HolePunch;
8
+ (function (HolePunch) {
9
+ let Type;
10
+ (function (Type) {
11
+ Type["UNUSED"] = "UNUSED";
12
+ Type["CONNECT"] = "CONNECT";
13
+ Type["SYNC"] = "SYNC";
14
+ })(Type = HolePunch.Type || (HolePunch.Type = {}));
15
+ let __TypeValues;
16
+ (function (__TypeValues) {
17
+ __TypeValues[__TypeValues["UNUSED"] = 0] = "UNUSED";
18
+ __TypeValues[__TypeValues["CONNECT"] = 100] = "CONNECT";
19
+ __TypeValues[__TypeValues["SYNC"] = 300] = "SYNC";
20
+ })(__TypeValues || (__TypeValues = {}));
21
+ (function (Type) {
22
+ Type.codec = () => {
23
+ return enumeration(__TypeValues);
24
+ };
25
+ })(Type = HolePunch.Type || (HolePunch.Type = {}));
26
+ let _codec;
27
+ HolePunch.codec = () => {
28
+ if (_codec == null) {
29
+ _codec = message((obj, w, opts = {}) => {
30
+ if (opts.lengthDelimited !== false) {
31
+ w.fork();
32
+ }
33
+ if (obj.type != null) {
34
+ w.uint32(8);
35
+ HolePunch.Type.codec().encode(obj.type, w);
36
+ }
37
+ if (obj.observedAddresses != null) {
38
+ for (const value of obj.observedAddresses) {
39
+ w.uint32(18);
40
+ w.bytes(value);
41
+ }
42
+ }
43
+ if (opts.lengthDelimited !== false) {
44
+ w.ldelim();
45
+ }
46
+ }, (reader, length) => {
47
+ const obj = {
48
+ observedAddresses: []
49
+ };
50
+ const end = length == null ? reader.len : reader.pos + length;
51
+ while (reader.pos < end) {
52
+ const tag = reader.uint32();
53
+ switch (tag >>> 3) {
54
+ case 1:
55
+ obj.type = HolePunch.Type.codec().decode(reader);
56
+ break;
57
+ case 2:
58
+ obj.observedAddresses.push(reader.bytes());
59
+ break;
60
+ default:
61
+ reader.skipType(tag & 7);
62
+ break;
63
+ }
64
+ }
65
+ return obj;
66
+ });
67
+ }
68
+ return _codec;
69
+ };
70
+ HolePunch.encode = (obj) => {
71
+ return encodeMessage(obj, HolePunch.codec());
72
+ };
73
+ HolePunch.decode = (buf) => {
74
+ return decodeMessage(buf, HolePunch.codec());
75
+ };
76
+ })(HolePunch || (HolePunch = {}));
77
+ //# sourceMappingURL=message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.js","sourceRoot":"","sources":["../../../../src/dcutr/pb/message.ts"],"names":[],"mappings":"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"}
@@ -1,3 +1,3 @@
1
- export declare const version = "0.46.4";
1
+ export declare const version = "0.46.5";
2
2
  export declare const name = "libp2p";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -1,3 +1,3 @@
1
- export const version = '0.46.4';
1
+ export const version = '0.46.5';
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.46.4",
3
+ "version": "0.46.5",
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/tree/master/packages/libp2p#readme",
@@ -56,6 +56,10 @@
56
56
  "types": "./dist/src/circuit-relay/index.d.ts",
57
57
  "import": "./dist/src/circuit-relay/index.js"
58
58
  },
59
+ "./dcutr": {
60
+ "types": "./dist/src/dcutr/index.d.ts",
61
+ "import": "./dist/src/dcutr/index.js"
62
+ },
59
63
  "./fetch": {
60
64
  "types": "./dist/src/fetch/index.d.ts",
61
65
  "import": "./dist/src/fetch/index.js"
@@ -100,7 +104,8 @@
100
104
  "build": "aegir build",
101
105
  "generate": "run-s generate:proto:*",
102
106
  "generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
103
- "generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
107
+ "generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto",
108
+ "generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto",
104
109
  "generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
105
110
  "generate:proto:identify": "protons ./src/identify/pb/message.proto",
106
111
  "generate:proto:plaintext": "protons ./src/insecure/pb/proto.proto",
@@ -117,7 +122,7 @@
117
122
  "@achingbrain/nat-port-mapper": "^1.0.9",
118
123
  "@libp2p/crypto": "^2.0.3",
119
124
  "@libp2p/interface": "^0.1.2",
120
- "@libp2p/interface-internal": "^0.1.3",
125
+ "@libp2p/interface-internal": "^0.1.4",
121
126
  "@libp2p/keychain": "^3.0.3",
122
127
  "@libp2p/logger": "^3.0.2",
123
128
  "@libp2p/multistream-select": "^4.0.2",
@@ -129,6 +134,7 @@
129
134
  "@libp2p/utils": "^4.0.2",
130
135
  "@multiformats/mafmt": "^12.1.2",
131
136
  "@multiformats/multiaddr": "^12.1.5",
137
+ "@multiformats/multiaddr-matcher": "^1.0.0",
132
138
  "abortable-iterator": "^5.0.1",
133
139
  "any-signal": "^4.1.1",
134
140
  "datastore-core": "^9.0.1",
@@ -163,18 +169,17 @@
163
169
  "@chainsafe/libp2p-gossipsub": "^10.0.0",
164
170
  "@chainsafe/libp2p-noise": "^13.0.0",
165
171
  "@chainsafe/libp2p-yamux": "^5.0.0",
166
- "@libp2p/bootstrap": "^9.0.3",
172
+ "@libp2p/bootstrap": "^9.0.4",
167
173
  "@libp2p/daemon-client": "^7.0.0",
168
174
  "@libp2p/daemon-server": "^6.0.0",
169
- "@libp2p/floodsub": "^8.0.4",
170
- "@libp2p/interface-compliance-tests": "^4.0.3",
175
+ "@libp2p/floodsub": "^8.0.5",
176
+ "@libp2p/interface-compliance-tests": "^4.0.4",
171
177
  "@libp2p/interop": "^9.0.0",
172
- "@libp2p/kad-dht": "^10.0.3",
173
- "@libp2p/mdns": "^9.0.3",
174
- "@libp2p/mplex": "^9.0.3",
175
- "@libp2p/tcp": "^8.0.3",
176
- "@libp2p/websockets": "^7.0.3",
177
- "@types/varint": "^6.0.0",
178
+ "@libp2p/kad-dht": "^10.0.4",
179
+ "@libp2p/mdns": "^9.0.4",
180
+ "@libp2p/mplex": "^9.0.4",
181
+ "@libp2p/tcp": "^8.0.4",
182
+ "@libp2p/websockets": "^7.0.4",
178
183
  "@types/xsalsa20": "^1.1.0",
179
184
  "aegir": "^40.0.8",
180
185
  "delay": "^6.0.0",
@@ -191,6 +196,7 @@
191
196
  "sinon-ts": "^1.0.0"
192
197
  },
193
198
  "browser": {
199
+ "./dist/src/connection-manager/constants.js": "./dist/src/connection-manager/constants.browser.js",
194
200
  "./dist/src/config/connection-gater.js": "./dist/src/config/connection-gater.browser.js"
195
201
  }
196
202
  }
@@ -1,7 +1,8 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import { PeerMap, PeerSet } from '@libp2p/peer-collections'
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
3
4
  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
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
5
6
  import type { Libp2pEvents } from '@libp2p/interface'
6
7
  import type { EventEmitter } from '@libp2p/interface/events'
7
8
  import type { PeerStore } from '@libp2p/interface/peer-store'
@@ -16,6 +17,7 @@ interface AutoDialInit {
16
17
  autoDialConcurrency?: number
17
18
  autoDialPriority?: number
18
19
  autoDialInterval?: number
20
+ autoDialPeerRetryThreshold?: number
19
21
  }
20
22
 
21
23
  interface AutoDialComponents {
@@ -29,7 +31,8 @@ const defaultOptions = {
29
31
  maxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH,
30
32
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
31
33
  autoDialPriority: AUTO_DIAL_PRIORITY,
32
- autoDialInterval: AUTO_DIAL_INTERVAL
34
+ autoDialInterval: AUTO_DIAL_INTERVAL,
35
+ autoDialPeerRetryThreshold: AUTO_DIAL_PEER_RETRY_THRESHOLD
33
36
  }
34
37
 
35
38
  export class AutoDial implements Startable {
@@ -40,6 +43,7 @@ export class AutoDial implements Startable {
40
43
  private readonly autoDialPriority: number
41
44
  private readonly autoDialIntervalMs: number
42
45
  private readonly autoDialMaxQueueLength: number
46
+ private readonly autoDialPeerRetryThresholdMs: number
43
47
  private autoDialInterval?: ReturnType<typeof setInterval>
44
48
  private started: boolean
45
49
  private running: boolean
@@ -56,6 +60,7 @@ export class AutoDial implements Startable {
56
60
  this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
57
61
  this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
58
62
  this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
63
+ this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
59
64
  this.started = false
60
65
  this.running = false
61
66
  this.queue = new PeerJobQueue({
@@ -207,9 +212,26 @@ export class AutoDial implements Startable {
207
212
  return 0
208
213
  })
209
214
 
210
- log('selected %d/%d peers to dial', sortedPeers.length, peers.length)
215
+ const peersThatHaveNotFailed = sortedPeers.filter(peer => {
216
+ const lastDialFailure = peer.metadata.get(LAST_DIAL_FAILURE_KEY)
211
217
 
212
- for (const peer of sortedPeers) {
218
+ if (lastDialFailure == null) {
219
+ return true
220
+ }
221
+
222
+ const lastDialFailureTimestamp = parseInt(uint8ArrayToString(lastDialFailure))
223
+
224
+ if (isNaN(lastDialFailureTimestamp)) {
225
+ return true
226
+ }
227
+
228
+ // only dial if the time since the last failure is above the retry threshold
229
+ return Date.now() - lastDialFailureTimestamp > this.autoDialPeerRetryThresholdMs
230
+ })
231
+
232
+ log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
233
+
234
+ for (const peer of peersThatHaveNotFailed) {
213
235
  this.queue.add(async () => {
214
236
  const numConnections = this.connectionManager.getConnectionsMap().size
215
237
 
@@ -222,7 +244,6 @@ export class AutoDial implements Startable {
222
244
 
223
245
  log('connecting to a peerStore stored peer %p', peer.id)
224
246
  await this.connectionManager.openConnection(peer.id, {
225
- // @ts-expect-error needs adding to the ConnectionManager interface
226
247
  priority: this.autoDialPriority
227
248
  })
228
249
  }, {
@@ -0,0 +1,21 @@
1
+ export * from './constants.defaults.js'
2
+
3
+ /**
4
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
5
+ */
6
+ export const MAX_PARALLEL_DIALS = 10
7
+
8
+ /**
9
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
10
+ */
11
+ export const MIN_CONNECTIONS = 5
12
+
13
+ /**
14
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxConnections
15
+ */
16
+ export const MAX_CONNECTIONS = 100
17
+
18
+ /**
19
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
20
+ */
21
+ export const AUTO_DIAL_CONCURRENCY = 10
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
3
+ */
4
+ export const DIAL_TIMEOUT = 30e3
5
+
6
+ /**
7
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
8
+ */
9
+ export const INBOUND_UPGRADE_TIMEOUT = 30e3
10
+
11
+ /**
12
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
13
+ */
14
+ export const MAX_PEER_ADDRS_TO_DIAL = 25
15
+
16
+ /**
17
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
18
+ */
19
+ export const MAX_PARALLEL_DIALS_PER_PEER = 10
20
+
21
+ /**
22
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
23
+ */
24
+ export const AUTO_DIAL_INTERVAL = 5000
25
+
26
+ /**
27
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
28
+ */
29
+ export const AUTO_DIAL_PRIORITY = 0
30
+
31
+ /**
32
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialMaxQueueLength
33
+ */
34
+ export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
35
+
36
+ /**
37
+ * @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialPeerRetryThreshold
38
+ */
39
+ export const AUTO_DIAL_PEER_RETRY_THRESHOLD = 1000 * 60
40
+
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
+ /**
47
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxIncomingPendingConnections
48
+ */
49
+ export const MAX_INCOMING_PENDING_CONNECTIONS = 10
50
+
51
+ /**
52
+ * Store as part of the peer store metadata for a given peer, the value for this
53
+ * key is a timestamp of the last time a dial attempted failed with the relevant
54
+ * peer stored as a string.
55
+ *
56
+ * Used to insure we do not endlessly try to auto dial peers we have recently
57
+ * failed to dial.
58
+ */
59
+ export const LAST_DIAL_FAILURE_KEY = 'last-dial-failure'
@@ -1,28 +1,10 @@
1
- /**
2
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
3
- */
4
- export const DIAL_TIMEOUT = 30e3
5
-
6
- /**
7
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
8
- */
9
- export const INBOUND_UPGRADE_TIMEOUT = 30e3
1
+ export * from './constants.defaults.js'
10
2
 
11
3
  /**
12
4
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
13
5
  */
14
6
  export const MAX_PARALLEL_DIALS = 100
15
7
 
16
- /**
17
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
18
- */
19
- export const MAX_PEER_ADDRS_TO_DIAL = 25
20
-
21
- /**
22
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
23
- */
24
- export const MAX_PARALLEL_DIALS_PER_PEER = 10
25
-
26
8
  /**
27
9
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
28
10
  */
@@ -33,32 +15,7 @@ export const MIN_CONNECTIONS = 50
33
15
  */
34
16
  export const MAX_CONNECTIONS = 300
35
17
 
36
- /**
37
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
38
- */
39
- export const AUTO_DIAL_INTERVAL = 5000
40
-
41
18
  /**
42
19
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
43
20
  */
44
21
  export const AUTO_DIAL_CONCURRENCY = 25
45
-
46
- /**
47
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
48
- */
49
- export const AUTO_DIAL_PRIORITY = 0
50
-
51
- /**
52
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialMaxQueueLength
53
- */
54
- export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
55
-
56
- /**
57
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
58
- */
59
- export const INBOUND_CONNECTION_THRESHOLD = 5
60
-
61
- /**
62
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxIncomingPendingConnections
63
- */
64
- export const MAX_INCOMING_PENDING_CONNECTIONS = 10
@@ -7,13 +7,15 @@ import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
7
7
  import { type ClearableSignal, anySignal } from 'any-signal'
8
8
  import pDefer from 'p-defer'
9
9
  import PQueue from 'p-queue'
10
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
11
  import { codes } from '../errors.js'
11
12
  import { getPeerAddress } from '../get-peer.js'
12
13
  import {
13
14
  DIAL_TIMEOUT,
14
15
  MAX_PARALLEL_DIALS_PER_PEER,
15
16
  MAX_PARALLEL_DIALS,
16
- MAX_PEER_ADDRS_TO_DIAL
17
+ MAX_PEER_ADDRS_TO_DIAL,
18
+ LAST_DIAL_FAILURE_KEY
17
19
  } from './constants.js'
18
20
  import { combineSignals, resolveMultiaddrs } from './utils.js'
19
21
  import type { AddressSorter, AbortOptions, PendingDial } from '@libp2p/interface'
@@ -230,9 +232,22 @@ export class DialQueue {
230
232
  // clean up abort signals/controllers
231
233
  signal.clear()
232
234
  })
233
- .catch(err => {
235
+ .catch(async err => {
234
236
  log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err)
235
237
 
238
+ if (peerId != null) {
239
+ // record the last failed dial
240
+ try {
241
+ await this.peerStore.patch(peerId, {
242
+ metadata: {
243
+ [LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString())
244
+ }
245
+ })
246
+ } catch (err: any) {
247
+ log.error('could not update last dial failure key for %p', peerId, err)
248
+ }
249
+ }
250
+
236
251
  // Error is a timeout
237
252
  if (signal.aborted) {
238
253
  const error = new CodeError(err.message, codes.ERR_TIMEOUT)
@@ -20,7 +20,7 @@ import type { Metrics } from '@libp2p/interface/metrics'
20
20
  import type { PeerId } from '@libp2p/interface/peer-id'
21
21
  import type { Peer, PeerStore } from '@libp2p/interface/peer-store'
22
22
  import type { Startable } from '@libp2p/interface/startable'
23
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
23
+ import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
24
24
  import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
25
25
 
26
26
  const log = logger('libp2p:connection-manager')
@@ -66,6 +66,12 @@ export interface ConnectionManagerInit {
66
66
  */
67
67
  autoDialMaxQueueLength?: number
68
68
 
69
+ /**
70
+ * When we've failed to dial a peer, do not autodial them again within this
71
+ * number of ms. (default: 1 minute)
72
+ */
73
+ autoDialPeerRetryThreshold?: number
74
+
69
75
  /**
70
76
  * Sort the known addresses of a peer before trying to dial, By default public
71
77
  * addresses will be dialled before private (e.g. loopback or LAN) addresses.
@@ -154,10 +160,6 @@ export interface DefaultConnectionManagerComponents {
154
160
  events: EventEmitter<Libp2pEvents>
155
161
  }
156
162
 
157
- export interface OpenConnectionOptions extends AbortOptions {
158
- priority?: number
159
- }
160
-
161
163
  /**
162
164
  * Responsible for managing known connections.
163
165
  */
@@ -492,7 +494,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
492
494
 
493
495
  const { peerId } = getPeerAddress(peerIdOrMultiaddr)
494
496
 
495
- if (peerId != null) {
497
+ if (peerId != null && options.force !== true) {
496
498
  log('dial %p', peerId)
497
499
  const existingConnections = this.getConnections(peerId)
498
500