-
Notifications
You must be signed in to change notification settings - Fork 440
/
index.ts
737 lines (599 loc) · 20.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import { logger } from '@libp2p/logger'
import errCode from 'err-code'
import mergeOptions from 'merge-options'
import { LatencyMonitor, SummaryObject } from './latency-monitor.js'
import type { AbortOptions } from '@libp2p/interfaces'
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
import type { Startable } from '@libp2p/interfaces/startable'
import { codes } from '../errors.js'
import { isPeerId, PeerId } from '@libp2p/interface-peer-id'
import { setMaxListeners } from 'events'
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
import type { ConnectionManager, Dialer } from '@libp2p/interface-connection-manager'
import * as STATUS from '@libp2p/interface-connection/status'
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
import { isMultiaddr, multiaddr, Multiaddr, Resolver } from '@multiformats/multiaddr'
import { PeerMap } from '@libp2p/peer-collections'
import { TimeoutController } from 'timeout-abort-controller'
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags'
import { RateLimiterMemory } from 'rate-limiter-flexible'
import type { Metrics } from '@libp2p/interface-metrics'
import type { Upgrader } from '@libp2p/interface-transport'
import { getPeer } from '../get-peer.js'
const log = logger('libp2p:connection-manager')
const defaultOptions: Partial<ConnectionManagerInit> = {
maxConnections: Infinity,
minConnections: 0,
maxData: Infinity,
maxSentData: Infinity,
maxReceivedData: Infinity,
maxEventLoopDelay: Infinity,
pollInterval: 2000,
autoDialInterval: 10000,
movingAverageInterval: 60000,
inboundConnectionThreshold: 5,
maxIncomingPendingConnections: 10
}
const STARTUP_RECONNECT_TIMEOUT = 60000
export interface ConnectionManagerInit {
/**
* The maximum number of connections to keep open
*/
maxConnections: number
/**
* The minimum number of connections to keep open
*/
minConnections: number
/**
* The max data (in and out), per average interval to allow
*/
maxData?: number
/**
* The max outgoing data, per average interval to allow
*/
maxSentData?: number
/**
* The max incoming data, per average interval to allow
*/
maxReceivedData?: number
/**
* The upper limit the event loop can take to run
*/
maxEventLoopDelay?: number
/**
* How often, in milliseconds, metrics and latency should be checked
*/
pollInterval?: number
/**
* How often, in milliseconds, to compute averages
*/
movingAverageInterval?: number
/**
* If true, try to connect to all discovered peers up to the connection manager limit
*/
autoDial?: boolean
/**
* How long to wait between attempting to keep our number of concurrent connections
* above minConnections
*/
autoDialInterval: number
/**
* Sort the known addresses of a peer before trying to dial
*/
addressSorter?: AddressSorter
/**
* Number of max concurrent dials
*/
maxParallelDials?: number
/**
* Number of max addresses to dial for a given peer
*/
maxAddrsToDial?: number
/**
* How long a dial attempt is allowed to take, including DNS resolution
* of the multiaddr, opening a socket and upgrading it to a Connection.
*/
dialTimeout?: number
/**
* When a new inbound connection is opened, the upgrade process (e.g. protect,
* encrypt, multiplex etc) must complete within this number of ms.
*/
inboundUpgradeTimeout: number
/**
* Number of max concurrent dials per peer
*/
maxDialsPerPeer?: number
/**
* Multiaddr resolvers to use when dialing
*/
resolvers?: Record<string, Resolver>
/**
* On startup we try to dial any peer that has previously been
* tagged with KEEP_ALIVE up to this timeout in ms. (default: 60000)
*/
startupReconnectTimeout?: number
/**
* A list of multiaddrs that will always be allowed (except if they are in the
* deny list) to open connections to this node even if we've reached maxConnections
*/
allow?: string[]
/**
* A list of multiaddrs that will never be allowed to open connections to
* this node under any circumstances
*/
deny?: string[]
/**
* If more than this many connections are opened per second by a single
* host, reject subsequent connections
*/
inboundConnectionThreshold?: number
/**
* The maximum number of parallel incoming connections allowed that have yet to
* complete the connection upgrade - e.g. choosing connection encryption, muxer, etc
*/
maxIncomingPendingConnections?: number
}
export interface ConnectionManagerEvents {
'peer:connect': CustomEvent<PeerId>
'peer:disconnect': CustomEvent<PeerId>
}
export interface DefaultConnectionManagerComponents {
peerId: PeerId
metrics?: Metrics
upgrader: Upgrader
peerStore: PeerStore
dialer: Dialer
}
/**
* Responsible for managing known connections.
*/
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
private readonly components: DefaultConnectionManagerComponents
private readonly opts: Required<ConnectionManagerInit>
private readonly connections: Map<string, Connection[]>
private started: boolean
private readonly latencyMonitor: LatencyMonitor
private readonly startupReconnectTimeout: number
private connectOnStartupController?: TimeoutController
private readonly dialTimeout: number
private readonly allow: Multiaddr[]
private readonly deny: Multiaddr[]
private readonly inboundConnectionRateLimiter: RateLimiterMemory
private incomingPendingConnections: number
constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit) {
super()
this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
if (this.opts.maxConnections < this.opts.minConnections) {
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
}
log('options: %o', this.opts)
this.components = components
/**
* Map of connections per peer
*/
this.connections = new Map()
this.started = false
this.latencyMonitor = new LatencyMonitor({
latencyCheckIntervalMs: init.pollInterval,
dataEmitIntervalMs: init.pollInterval
})
try {
// This emitter gets listened to a lot
setMaxListeners?.(Infinity, this)
} catch {}
this.onConnect = this.onConnect.bind(this)
this.onDisconnect = this.onDisconnect.bind(this)
this.startupReconnectTimeout = init.startupReconnectTimeout ?? STARTUP_RECONNECT_TIMEOUT
this.dialTimeout = init.dialTimeout ?? 30000
this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
this.deny = (init.deny ?? []).map(ma => multiaddr(ma))
this.inboundConnectionRateLimiter = new RateLimiterMemory({
points: this.opts.inboundConnectionThreshold,
duration: 1
})
this.incomingPendingConnections = 0
}
isStarted () {
return this.started
}
/**
* Starts the Connection Manager. If Metrics are not enabled on libp2p
* only event loop and connection limits will be monitored.
*/
async start () {
// track inbound/outbound connections
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_connections', {
calculate: () => {
const metric = {
inbound: 0,
outbound: 0
}
for (const conns of this.connections.values()) {
for (const conn of conns) {
if (conn.stat.direction === 'inbound') {
metric.inbound++
} else {
metric.outbound++
}
}
}
return metric
}
})
// track total number of streams per protocol
this.components.metrics?.registerMetricGroup('libp2p_protocol_streams_total', {
label: 'protocol',
calculate: () => {
const metric: Record<string, number> = {}
for (const conns of this.connections.values()) {
for (const conn of conns) {
for (const stream of conn.streams) {
const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}`
metric[key] = (metric[key] ?? 0) + 1
}
}
}
return metric
}
})
// track 90th percentile of streams per protocol
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_protocol_streams_per_connection_90th_percentile', {
label: 'protocol',
calculate: () => {
const allStreams: Record<string, number[]> = {}
for (const conns of this.connections.values()) {
for (const conn of conns) {
const streams: Record<string, number> = {}
for (const stream of conn.streams) {
const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}`
streams[key] = (streams[key] ?? 0) + 1
}
for (const [protocol, count] of Object.entries(streams)) {
allStreams[protocol] = allStreams[protocol] ?? []
allStreams[protocol].push(count)
}
}
}
const metric: Record<string, number> = {}
for (let [protocol, counts] of Object.entries(allStreams)) {
counts = counts.sort((a, b) => a - b)
const index = Math.floor(counts.length * 0.9)
metric[protocol] = counts[index]
}
return metric
}
})
// latency monitor
this.latencyMonitor.start()
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
this.started = true
log('started')
}
async afterStart () {
this.components.upgrader.addEventListener('connection', this.onConnect)
this.components.upgrader.addEventListener('connectionEnd', this.onDisconnect)
// re-connect to any peers with the KEEP_ALIVE tag
void Promise.resolve()
.then(async () => {
const keepAlivePeers: PeerId[] = []
for (const peer of await this.components.peerStore.all()) {
const tags = await this.components.peerStore.getTags(peer.id)
const hasKeepAlive = tags.filter(tag => tag.name === KEEP_ALIVE).length > 0
if (hasKeepAlive) {
keepAlivePeers.push(peer.id)
}
}
this.connectOnStartupController?.clear()
this.connectOnStartupController = new TimeoutController(this.startupReconnectTimeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, this.connectOnStartupController.signal)
} catch {}
await Promise.all(
keepAlivePeers.map(async peer => {
await this.openConnection(peer, {
signal: this.connectOnStartupController?.signal
})
.catch(err => {
log.error(err)
})
})
)
})
.catch(err => {
log.error(err)
})
.finally(() => {
this.connectOnStartupController?.clear()
})
}
async beforeStop () {
// if we are still dialing KEEP_ALIVE peers, abort those dials
this.connectOnStartupController?.abort()
this.components.upgrader.removeEventListener('connection', this.onConnect)
this.components.upgrader.removeEventListener('connectionEnd', this.onDisconnect)
}
/**
* Stops the Connection Manager
*/
async stop () {
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
this.latencyMonitor.stop()
this.started = false
await this._close()
log('stopped')
}
/**
* Cleans up the connections
*/
async _close () {
// Close all connections we're tracking
const tasks: Array<Promise<void>> = []
for (const connectionList of this.connections.values()) {
for (const connection of connectionList) {
tasks.push((async () => {
try {
await connection.close()
} catch (err) {
log.error(err)
}
})())
}
}
log('closing %d connections', tasks.length)
await Promise.all(tasks)
this.connections.clear()
}
onConnect (evt: CustomEvent<Connection>) {
void this._onConnect(evt).catch(err => {
log.error(err)
})
}
/**
* Tracks the incoming connection and check the connection limit
*/
async _onConnect (evt: CustomEvent<Connection>) {
const { detail: connection } = evt
if (!this.started) {
// This can happen when we are in the process of shutting down the node
await connection.close()
return
}
const peerId = connection.remotePeer
const peerIdStr = peerId.toString()
const storedConns = this.connections.get(peerIdStr)
if (storedConns != null) {
storedConns.push(connection)
} else {
this.connections.set(peerIdStr, [connection])
}
if (peerId.publicKey != null) {
await this.components.peerStore.keyBook.set(peerId, peerId.publicKey)
}
const numConnections = this.getConnections().length
const toPrune = numConnections - this.opts.maxConnections
await this._checkMaxLimit('maxConnections', numConnections, toPrune)
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
}
/**
* Removes the connection from tracking
*/
onDisconnect (evt: CustomEvent<Connection>) {
const { detail: connection } = evt
if (!this.started) {
// This can happen when we are in the process of shutting down the node
return
}
const peerId = connection.remotePeer.toString()
let storedConn = this.connections.get(peerId)
if (storedConn != null && storedConn.length > 1) {
storedConn = storedConn.filter((conn) => conn.id !== connection.id)
this.connections.set(peerId, storedConn)
} else if (storedConn != null) {
this.connections.delete(peerId)
this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: connection }))
}
}
getConnections (peerId?: PeerId): Connection[] {
if (peerId != null) {
return this.connections.get(peerId.toString()) ?? []
}
let conns: Connection[] = []
for (const c of this.connections.values()) {
conns = conns.concat(c)
}
return conns
}
async openConnection (peerIdOrMultiaddr: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
let peerId: PeerId
if (isPeerId(peerIdOrMultiaddr)) {
peerId = peerIdOrMultiaddr
} else if (isMultiaddr(peerIdOrMultiaddr)) {
const info = getPeer(peerIdOrMultiaddr)
peerId = info.id
} else {
throw errCode(new TypeError('Can only open connections to PeerIds or Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
}
log('dial to %p', peerId)
const existingConnections = this.getConnections(peerId)
if (existingConnections.length > 0) {
log('had an existing connection to %p', peerId)
return existingConnections[0]
}
let timeoutController: TimeoutController | undefined
if (options?.signal == null) {
timeoutController = new TimeoutController(this.dialTimeout)
options.signal = timeoutController.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
}
try {
const connection = await this.components.dialer.dial(peerIdOrMultiaddr, options)
let peerConnections = this.connections.get(peerId.toString())
if (peerConnections == null) {
peerConnections = []
this.connections.set(peerId.toString(), peerConnections)
}
// we get notified of connections via the Upgrader emitting "connection"
// events, double check we aren't already tracking this connection before
// storing it
let trackedConnection = false
for (const conn of peerConnections) {
if (conn.id === connection.id) {
trackedConnection = true
}
}
if (!trackedConnection) {
peerConnections.push(connection)
}
return connection
} finally {
if (timeoutController != null) {
timeoutController.clear()
}
}
}
async closeConnections (peerId: PeerId): Promise<void> {
const connections = this.connections.get(peerId.toString()) ?? []
await Promise.all(
connections.map(async connection => {
return await connection.close()
})
)
}
/**
* Get all open connections with a peer
*/
getAll (peerId: PeerId): Connection[] {
if (!isPeerId(peerId)) {
throw errCode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
}
const id = peerId.toString()
const connections = this.connections.get(id)
// Return all open connections
if (connections != null) {
return connections.filter(connection => connection.stat.status === STATUS.OPEN)
}
return []
}
/**
* If the event loop is slow, maybe close a connection
*/
_onLatencyMeasure (evt: CustomEvent<SummaryObject>) {
const { detail: summary } = evt
this._checkMaxLimit('maxEventLoopDelay', summary.avgMs, 1)
.catch(err => {
log.error(err)
})
}
/**
* If the `value` of `name` has exceeded its limit, maybe close a connection
*/
async _checkMaxLimit (name: keyof ConnectionManagerInit, value: number, toPrune: number = 1) {
const limit = this.opts[name]
log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
if (value > limit) {
log('%s: limit exceeded: %p, %d/%d, pruning %d connection(s)', this.components.peerId, name, value, limit, toPrune)
await this._pruneConnections(toPrune)
}
}
/**
* If we have more connections than our maximum, select some excess connections
* to prune based on peer value
*/
async _pruneConnections (toPrune: number) {
const connections = this.getConnections()
const peerValues = new PeerMap<number>()
// work out peer values
for (const connection of connections) {
const remotePeer = connection.remotePeer
if (peerValues.has(remotePeer)) {
continue
}
const tags = await this.components.peerStore.getTags(remotePeer)
// sum all tag values
peerValues.set(remotePeer, tags.reduce((acc, curr) => {
return acc + curr.value
}, 0))
}
// sort by value, lowest to highest
const sortedConnections = connections.sort((a, b) => {
const peerAValue = peerValues.get(a.remotePeer) ?? 0
const peerBValue = peerValues.get(b.remotePeer) ?? 0
if (peerAValue > peerBValue) {
return 1
}
if (peerAValue < peerBValue) {
return -1
}
return 0
})
// close some connections
const toClose = []
for (const connection of sortedConnections) {
log('too many connections open - closing a connection to %p', connection.remotePeer)
toClose.push(connection)
if (toClose.length === toPrune) {
break
}
}
// close connections
await Promise.all(
toClose.map(async connection => {
try {
await connection.close()
} catch (err) {
log.error(err)
}
// TODO: should not need to invoke this manually
this.onDisconnect(new CustomEvent<Connection>('connectionEnd', {
detail: connection
}))
})
)
}
async acceptIncomingConnection (maConn: MultiaddrConnection): Promise<boolean> {
// check deny list
const denyConnection = this.deny.some(ma => {
return maConn.remoteAddr.toString().startsWith(ma.toString())
})
if (denyConnection) {
log('connection from %s refused - connection remote address was in deny list', maConn.remoteAddr)
return false
}
// check allow list
const allowConnection = this.allow.some(ma => {
return maConn.remoteAddr.toString().startsWith(ma.toString())
})
if (allowConnection) {
this.incomingPendingConnections++
return true
}
// check pending connections
if (this.incomingPendingConnections === this.opts.maxIncomingPendingConnections) {
log('connection from %s refused - incomingPendingConnections exceeded by peer %s', maConn.remoteAddr)
return false
}
if (maConn.remoteAddr.isThinWaistAddress()) {
const host = maConn.remoteAddr.nodeAddress().address
try {
await this.inboundConnectionRateLimiter.consume(host, 1)
} catch {
log('connection from %s refused - inboundConnectionThreshold exceeded by host %s', host, maConn.remoteAddr)
return false
}
}
if (this.getConnections().length < this.opts.maxConnections) {
this.incomingPendingConnections++
return true
}
log('connection from %s refused - maxConnections exceeded', maConn.remoteAddr)
return false
}
afterUpgradeInbound () {
this.incomingPendingConnections--
}
}