libp2p 0.36.2-d16817c → 0.36.2-da3d19b
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/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +6 -4
- 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 +78 -40
- 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 +1 -1
- package/dist/src/dht/dummy-dht.d.ts.map +1 -1
- package/dist/src/dht/dummy-dht.js +1 -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/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/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/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 +1 -1
- package/dist/src/nat-manager.d.ts.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/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.js +1 -1
- 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 +13 -12
- 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/transport.ts +7 -4
- 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 +144 -57
- 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 +1 -1
- package/src/fetch/index.ts +2 -2
- package/src/identify/index.ts +5 -5
- package/src/index.ts +7 -28
- 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 +1 -1
- package/src/peer-record-updater.ts +1 -1
- package/src/peer-routing.ts +2 -1
- package/src/ping/index.ts +3 -2
- package/src/pubsub/dummy-pubsub.ts +1 -1
- package/src/registrar.ts +1 -1
- 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
|
@@ -4,16 +4,21 @@ import mergeOptions from 'merge-options'
|
|
|
4
4
|
import { LatencyMonitor, SummaryObject } from './latency-monitor.js'
|
|
5
5
|
// @ts-expect-error retimer does not have types
|
|
6
6
|
import retimer from 'retimer'
|
|
7
|
-
import {
|
|
7
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
8
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
8
10
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
9
11
|
import { codes } from '../errors.js'
|
|
10
12
|
import { isPeerId, PeerId } from '@libp2p/interfaces/peer-id'
|
|
11
13
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
12
14
|
import { setMaxListeners } from 'events'
|
|
13
15
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
14
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
15
|
-
import
|
|
16
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
17
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
16
18
|
import * as STATUS from '@libp2p/interfaces/connection/status'
|
|
19
|
+
import { Dialer } from './dialer/index.js'
|
|
20
|
+
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
21
|
+
import type { Resolver } from '@multiformats/multiaddr'
|
|
17
22
|
|
|
18
23
|
const log = logger('libp2p:connection-manager')
|
|
19
24
|
|
|
@@ -34,21 +39,16 @@ const METRICS_COMPONENT = 'connection-manager'
|
|
|
34
39
|
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
|
35
40
|
const METRICS_PEER_VALUES = 'peer-values'
|
|
36
41
|
|
|
37
|
-
export interface ConnectionManagerEvents {
|
|
38
|
-
'peer:connect': CustomEvent<PeerId>
|
|
39
|
-
'peer:disconnect': CustomEvent<PeerId>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
42
|
export interface ConnectionManagerInit {
|
|
43
43
|
/**
|
|
44
|
-
* The maximum number of connections
|
|
44
|
+
* The maximum number of connections to keep open
|
|
45
45
|
*/
|
|
46
|
-
maxConnections
|
|
46
|
+
maxConnections: number
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
* The minimum number of connections to
|
|
49
|
+
* The minimum number of connections to keep open
|
|
50
50
|
*/
|
|
51
|
-
minConnections
|
|
51
|
+
minConnections: number
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
54
|
* The max data (in and out), per average interval to allow
|
|
@@ -86,39 +86,75 @@ export interface ConnectionManagerInit {
|
|
|
86
86
|
defaultPeerValue?: number
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
|
-
*
|
|
89
|
+
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
90
90
|
*/
|
|
91
91
|
autoDial?: boolean
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
* How
|
|
94
|
+
* How long to wait between attempting to keep our number of concurrent connections
|
|
95
|
+
* above minConnections
|
|
96
|
+
*/
|
|
97
|
+
autoDialInterval: number
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Sort the known addresses of a peer before trying to dial
|
|
95
101
|
*/
|
|
96
|
-
|
|
102
|
+
addressSorter?: AddressSorter
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Number of max concurrent dials
|
|
106
|
+
*/
|
|
107
|
+
maxParallelDials?: number
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Number of max addresses to dial for a given peer
|
|
111
|
+
*/
|
|
112
|
+
maxAddrsToDial?: number
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* How long a dial attempt is allowed to take
|
|
116
|
+
*/
|
|
117
|
+
dialTimeout?: number
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Number of max concurrent dials per peer
|
|
121
|
+
*/
|
|
122
|
+
maxDialsPerPeer?: number
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Multiaddr resolvers to use when dialing
|
|
126
|
+
*/
|
|
127
|
+
resolvers?: Record<string, Resolver>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ConnectionManagerEvents {
|
|
131
|
+
'peer:connect': CustomEvent<PeerId>
|
|
132
|
+
'peer:disconnect': CustomEvent<PeerId>
|
|
97
133
|
}
|
|
98
134
|
|
|
99
135
|
/**
|
|
100
136
|
* Responsible for managing known connections.
|
|
101
137
|
*/
|
|
102
|
-
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
|
|
103
|
-
|
|
104
|
-
private
|
|
138
|
+
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable, Initializable {
|
|
139
|
+
public readonly dialer: Dialer
|
|
140
|
+
private components = new Components()
|
|
141
|
+
private readonly opts: Required<ConnectionManagerInit>
|
|
105
142
|
private readonly peerValues: Map<string, number>
|
|
106
143
|
private readonly connections: Map<string, Connection[]>
|
|
107
144
|
private started: boolean
|
|
108
145
|
private timer?: ReturnType<retimer>
|
|
109
146
|
private readonly latencyMonitor: LatencyMonitor
|
|
110
147
|
|
|
111
|
-
constructor (
|
|
148
|
+
constructor (init: ConnectionManagerInit) {
|
|
112
149
|
super()
|
|
113
150
|
|
|
114
|
-
this.
|
|
115
|
-
this.init = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
151
|
+
this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
116
152
|
|
|
117
|
-
if (this.
|
|
153
|
+
if (this.opts.maxConnections < this.opts.minConnections) {
|
|
118
154
|
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
|
|
119
155
|
}
|
|
120
156
|
|
|
121
|
-
log('options: %o', this.
|
|
157
|
+
log('options: %o', this.opts)
|
|
122
158
|
|
|
123
159
|
/**
|
|
124
160
|
* Map of peer identifiers to their peer value for pruning connections.
|
|
@@ -153,12 +189,16 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
153
189
|
setMaxListeners?.(Infinity, this)
|
|
154
190
|
} catch {}
|
|
155
191
|
|
|
156
|
-
this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
192
|
+
this.dialer = new Dialer(this.opts)
|
|
193
|
+
|
|
194
|
+
this.onConnect = this.onConnect.bind(this)
|
|
195
|
+
this.onDisconnect = this.onDisconnect.bind(this)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
init (components: Components): void {
|
|
199
|
+
this.components = components
|
|
200
|
+
|
|
201
|
+
this.dialer.init(components)
|
|
162
202
|
}
|
|
163
203
|
|
|
164
204
|
isStarted () {
|
|
@@ -171,18 +211,29 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
171
211
|
*/
|
|
172
212
|
async start () {
|
|
173
213
|
if (this.components.getMetrics() != null) {
|
|
174
|
-
this.timer = this.timer ?? retimer(this._checkMetrics, this.
|
|
214
|
+
this.timer = this.timer ?? retimer(this._checkMetrics, this.opts.pollInterval)
|
|
175
215
|
}
|
|
176
216
|
|
|
177
217
|
// latency monitor
|
|
178
218
|
this.latencyMonitor.start()
|
|
179
219
|
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
|
180
220
|
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
|
|
221
|
+
await this.dialer.start()
|
|
181
222
|
|
|
182
223
|
this.started = true
|
|
183
224
|
log('started')
|
|
184
225
|
}
|
|
185
226
|
|
|
227
|
+
async afterStart () {
|
|
228
|
+
this.components.getUpgrader().addEventListener('connection', this.onConnect)
|
|
229
|
+
this.components.getUpgrader().addEventListener('connectionEnd', this.onDisconnect)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async beforeStop () {
|
|
233
|
+
this.components.getUpgrader().removeEventListener('connection', this.onConnect)
|
|
234
|
+
this.components.getUpgrader().removeEventListener('connectionEnd', this.onDisconnect)
|
|
235
|
+
}
|
|
236
|
+
|
|
186
237
|
/**
|
|
187
238
|
* Stops the Connection Manager
|
|
188
239
|
*/
|
|
@@ -191,6 +242,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
191
242
|
|
|
192
243
|
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
|
|
193
244
|
this.latencyMonitor.stop()
|
|
245
|
+
await this.dialer.stop()
|
|
194
246
|
|
|
195
247
|
this.started = false
|
|
196
248
|
await this._close()
|
|
@@ -238,23 +290,29 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
238
290
|
if (metrics != null) {
|
|
239
291
|
try {
|
|
240
292
|
const movingAverages = metrics.getGlobal().getMovingAverages()
|
|
241
|
-
const received = movingAverages.dataReceived[this.
|
|
293
|
+
const received = movingAverages.dataReceived[this.opts.movingAverageInterval].movingAverage
|
|
242
294
|
await this._checkMaxLimit('maxReceivedData', received)
|
|
243
|
-
const sent = movingAverages.dataSent[this.
|
|
295
|
+
const sent = movingAverages.dataSent[this.opts.movingAverageInterval].movingAverage
|
|
244
296
|
await this._checkMaxLimit('maxSentData', sent)
|
|
245
297
|
const total = received + sent
|
|
246
298
|
await this._checkMaxLimit('maxData', total)
|
|
247
299
|
log('metrics update', total)
|
|
248
300
|
} finally {
|
|
249
|
-
this.timer = retimer(this._checkMetrics, this.
|
|
301
|
+
this.timer = retimer(this._checkMetrics, this.opts.pollInterval)
|
|
250
302
|
}
|
|
251
303
|
}
|
|
252
304
|
}
|
|
253
305
|
|
|
306
|
+
onConnect (evt: CustomEvent<Connection>) {
|
|
307
|
+
void this._onConnect(evt).catch(err => {
|
|
308
|
+
log.error(err)
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
|
|
254
312
|
/**
|
|
255
313
|
* Tracks the incoming connection and check the connection limit
|
|
256
314
|
*/
|
|
257
|
-
async
|
|
315
|
+
async _onConnect (evt: CustomEvent<Connection>) {
|
|
258
316
|
const { detail: connection } = evt
|
|
259
317
|
|
|
260
318
|
if (!this.started) {
|
|
@@ -278,10 +336,10 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
278
336
|
}
|
|
279
337
|
|
|
280
338
|
if (!this.peerValues.has(peerIdStr)) {
|
|
281
|
-
this.peerValues.set(peerIdStr, this.
|
|
339
|
+
this.peerValues.set(peerIdStr, this.opts.defaultPeerValue)
|
|
282
340
|
}
|
|
283
341
|
|
|
284
|
-
await this._checkMaxLimit('maxConnections', this.
|
|
342
|
+
await this._checkMaxLimit('maxConnections', this.getConnections().length)
|
|
285
343
|
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
|
|
286
344
|
}
|
|
287
345
|
|
|
@@ -311,35 +369,64 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
311
369
|
}
|
|
312
370
|
}
|
|
313
371
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
372
|
+
getConnections (peerId?: PeerId): Connection[] {
|
|
373
|
+
if (peerId != null) {
|
|
374
|
+
return this.connections.get(peerId.toString()) ?? []
|
|
375
|
+
}
|
|
317
376
|
|
|
318
|
-
|
|
319
|
-
let output: Connection[] = []
|
|
377
|
+
let conns: Connection[] = []
|
|
320
378
|
|
|
321
|
-
for (const
|
|
322
|
-
|
|
379
|
+
for (const c of this.connections.values()) {
|
|
380
|
+
conns = conns.concat(c)
|
|
323
381
|
}
|
|
324
382
|
|
|
325
|
-
return
|
|
383
|
+
return conns
|
|
326
384
|
}
|
|
327
385
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
386
|
+
async openConnection (peerId: PeerId, options?: AbortOptions): Promise<Connection> {
|
|
387
|
+
log('dial to %p', peerId)
|
|
388
|
+
const existingConnections = this.getConnections(peerId)
|
|
331
389
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
390
|
+
if (existingConnections.length > 0) {
|
|
391
|
+
log('had an existing connection to %p', peerId)
|
|
392
|
+
|
|
393
|
+
return existingConnections[0]
|
|
394
|
+
}
|
|
337
395
|
|
|
338
|
-
|
|
339
|
-
|
|
396
|
+
const connection = await this.dialer.dial(peerId, options)
|
|
397
|
+
let peerConnections = this.connections.get(peerId.toString())
|
|
398
|
+
|
|
399
|
+
if (peerConnections == null) {
|
|
400
|
+
peerConnections = []
|
|
401
|
+
this.connections.set(peerId.toString(), peerConnections)
|
|
340
402
|
}
|
|
341
403
|
|
|
342
|
-
|
|
404
|
+
// we get notified of connections via the Upgrader emitting "connection"
|
|
405
|
+
// events, double check we aren't already tracking this connection before
|
|
406
|
+
// storing it
|
|
407
|
+
let trackedConnection = false
|
|
408
|
+
|
|
409
|
+
for (const conn of peerConnections) {
|
|
410
|
+
if (conn.id === connection.id) {
|
|
411
|
+
trackedConnection = true
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (!trackedConnection) {
|
|
416
|
+
peerConnections.push(connection)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return connection
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async closeConnections (peerId: PeerId): Promise<void> {
|
|
423
|
+
const connections = this.connections.get(peerId.toString()) ?? []
|
|
424
|
+
|
|
425
|
+
await Promise.all(
|
|
426
|
+
connections.map(async connection => {
|
|
427
|
+
return await connection.close()
|
|
428
|
+
})
|
|
429
|
+
)
|
|
343
430
|
}
|
|
344
431
|
|
|
345
432
|
/**
|
|
@@ -377,7 +464,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
377
464
|
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
|
378
465
|
*/
|
|
379
466
|
async _checkMaxLimit (name: keyof ConnectionManagerInit, value: number) {
|
|
380
|
-
const limit = this.
|
|
467
|
+
const limit = this.opts[name]
|
|
381
468
|
log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
382
469
|
if (value > limit) {
|
|
383
470
|
log('%s: limit exceeded: %p, %d', this.components.getPeerId(), name, value)
|
|
@@ -390,7 +477,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
390
477
|
* to the lowest valued peer.
|
|
391
478
|
*/
|
|
392
479
|
async _maybeDisconnectOne () {
|
|
393
|
-
if (this.
|
|
480
|
+
if (this.opts.minConnections < this.connections.size) {
|
|
394
481
|
const peerValues = Array.from(new Map([...this.peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
395
482
|
|
|
396
483
|
log('%p: sorted peer values: %j', this.components.getPeerId(), peerValues)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
|
|
5
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
6
6
|
import { VisibilityChangeEmitter } from './visibility-change-emitter.js'
|
|
7
7
|
import { logger } from '@libp2p/logger'
|
|
8
8
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
|
|
5
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
6
6
|
import { logger } from '@libp2p/logger'
|
|
7
7
|
|
|
8
8
|
const log = logger('libp2p:connection-manager:latency-monitor:visibility-change-emitter')
|
|
@@ -9,7 +9,8 @@ import drain from 'it-drain'
|
|
|
9
9
|
import merge from 'it-merge'
|
|
10
10
|
import { pipe } from 'it-pipe'
|
|
11
11
|
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
12
|
-
import type { AbortOptions
|
|
12
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
13
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
13
14
|
import type { CID } from 'multiformats/cid'
|
|
14
15
|
import type { Components } from '@libp2p/interfaces/components'
|
|
15
16
|
|
package/src/dht/dummy-dht.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { DualDHT, QueryEvent, SingleDHT } from '@libp2p/interfaces/dht'
|
|
|
2
2
|
import type { PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
|
|
3
3
|
import errCode from 'err-code'
|
|
4
4
|
import { messages, codes } from '../errors.js'
|
|
5
|
-
import { EventEmitter } from '@libp2p/interfaces'
|
|
5
|
+
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
6
6
|
|
|
7
7
|
export class DummyDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
|
|
8
8
|
get wan (): SingleDHT {
|
package/src/fetch/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { FetchRequest, FetchResponse } from './pb/proto.js'
|
|
|
6
6
|
import { handshake } from 'it-handshake'
|
|
7
7
|
import { PROTOCOL } from './constants.js'
|
|
8
8
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
9
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
10
10
|
import type { Stream } from '@libp2p/interfaces/connection'
|
|
11
11
|
import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
|
|
12
12
|
import type { Components } from '@libp2p/interfaces/components'
|
|
@@ -70,7 +70,7 @@ export class FetchService implements Startable {
|
|
|
70
70
|
async fetch (peer: PeerId, key: string): Promise<Uint8Array | null> {
|
|
71
71
|
log('dialing %s to %p', this.protocol, peer)
|
|
72
72
|
|
|
73
|
-
const connection = await this.components.
|
|
73
|
+
const connection = await this.components.getConnectionManager().openConnection(peer)
|
|
74
74
|
const { stream } = await connection.newStream([this.protocol])
|
|
75
75
|
const shake = handshake(stream)
|
|
76
76
|
|
package/src/identify/index.ts
CHANGED
|
@@ -22,8 +22,8 @@ import {
|
|
|
22
22
|
import { codes } from '../errors.js'
|
|
23
23
|
import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
|
|
24
24
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
25
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
26
|
-
import { peerIdFromKeys
|
|
25
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
26
|
+
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
27
27
|
import type { Components } from '@libp2p/interfaces/components'
|
|
28
28
|
|
|
29
29
|
const log = logger('libp2p:identify')
|
|
@@ -161,15 +161,15 @@ export class IdentifyService implements Startable {
|
|
|
161
161
|
|
|
162
162
|
const connections: Connection[] = []
|
|
163
163
|
|
|
164
|
-
for (const
|
|
165
|
-
const peerId =
|
|
164
|
+
for (const conn of this.components.getConnectionManager().getConnections()) {
|
|
165
|
+
const peerId = conn.remotePeer
|
|
166
166
|
const peer = await this.components.getPeerStore().get(peerId)
|
|
167
167
|
|
|
168
168
|
if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
|
|
169
169
|
continue
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
connections.push(
|
|
172
|
+
connections.push(conn)
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
await this.push(connections)
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { createLibp2pNode } from './libp2p.js'
|
|
2
|
-
import type { AbortOptions,
|
|
2
|
+
import type { AbortOptions, RecursivePartial } from '@libp2p/interfaces'
|
|
3
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
4
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
3
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
6
|
import type { FaultTolerance } from './transport-manager.js'
|
|
5
7
|
import type { HostProperties } from './identify/index.js'
|
|
@@ -16,11 +18,12 @@ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypte
|
|
|
16
18
|
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
17
19
|
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
18
20
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
19
|
-
import type {
|
|
21
|
+
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
22
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
20
23
|
import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
|
|
21
24
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
22
|
-
import type { DialerInit } from '@libp2p/interfaces/dialer'
|
|
23
25
|
import type { KeyChain } from './keychain/index.js'
|
|
26
|
+
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
24
27
|
|
|
25
28
|
export interface PersistentPeerStoreOptions {
|
|
26
29
|
threshold?: number
|
|
@@ -71,29 +74,6 @@ export interface AddressesConfig {
|
|
|
71
74
|
announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
|
|
72
75
|
}
|
|
73
76
|
|
|
74
|
-
export interface ConnectionManagerConfig {
|
|
75
|
-
/**
|
|
76
|
-
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
77
|
-
*/
|
|
78
|
-
autoDial?: boolean
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The maximum number of connections to keep open
|
|
82
|
-
*/
|
|
83
|
-
maxConnections: number
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The minimum number of connections to keep open
|
|
87
|
-
*/
|
|
88
|
-
minConnections: number
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* How long to wait between attempting to keep our number of concurrent connections
|
|
92
|
-
* above minConnections
|
|
93
|
-
*/
|
|
94
|
-
autoDialInterval: number
|
|
95
|
-
}
|
|
96
|
-
|
|
97
77
|
export interface TransportManagerConfig {
|
|
98
78
|
faultTolerance?: FaultTolerance
|
|
99
79
|
}
|
|
@@ -117,11 +97,10 @@ export interface Libp2pInit {
|
|
|
117
97
|
peerId: PeerId
|
|
118
98
|
host: HostProperties
|
|
119
99
|
addresses: AddressesConfig
|
|
120
|
-
connectionManager:
|
|
100
|
+
connectionManager: ConnectionManagerInit
|
|
121
101
|
connectionGater: Partial<ConnectionGater>
|
|
122
102
|
transportManager: TransportManagerConfig
|
|
123
103
|
datastore: Datastore
|
|
124
|
-
dialer: DialerInit
|
|
125
104
|
metrics: MetricsInit
|
|
126
105
|
peerStore: PeerStoreInit
|
|
127
106
|
peerRouting: PeerRoutingConfig
|