libp2p 0.43.4 → 0.44.0-17eb162a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +29 -29
- package/dist/src/address-manager/index.d.ts +4 -4
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +9 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +4 -6
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.js +2 -2
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +7 -3
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +3 -1
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +3 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +6 -2
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +3 -0
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +5 -4
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +4 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +11 -6
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/circuit-relay/utils.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +8 -6
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +10 -10
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -9
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +39 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +131 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +32 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +111 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +78 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +374 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +70 -73
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +116 -248
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- package/dist/src/content-routing/index.js +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +3 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +4 -0
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +104 -128
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +2 -19
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +3 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +5 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +47 -50
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -2
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +25 -23
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +23 -9
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +14 -11
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +14 -10
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +46 -45
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +6 -9
- package/src/circuit-relay/server/advert-service.ts +2 -2
- package/src/circuit-relay/server/index.ts +7 -3
- package/src/circuit-relay/transport/discovery.ts +3 -1
- package/src/circuit-relay/transport/index.ts +10 -2
- package/src/circuit-relay/transport/listener.ts +8 -4
- package/src/circuit-relay/transport/reservation-store.ts +15 -7
- package/src/circuit-relay/utils.ts +3 -1
- package/src/components.ts +19 -17
- package/src/config.ts +0 -9
- package/src/connection-manager/auto-dial.ts +183 -0
- package/src/connection-manager/connection-pruner.ts +157 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +512 -0
- package/src/connection-manager/index.ts +191 -340
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/index.ts +1 -1
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +4 -4
- package/src/identify/index.ts +100 -140
- package/src/index.ts +2 -20
- package/src/insecure/index.ts +9 -5
- package/src/libp2p.ts +54 -60
- package/src/registrar.ts +29 -25
- package/src/transport-manager.ts +31 -11
- package/src/upgrader.ts +31 -22
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -121
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -324
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -1
- package/dist/src/peer-record-updater.d.ts +0 -24
- package/dist/src/peer-record-updater.d.ts.map +0 -1
- package/dist/src/peer-record-updater.js +0 -44
- package/dist/src/peer-record-updater.js.map +0 -1
- package/dist/typedoc-urls.json +0 -41
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -461
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
- package/src/peer-record-updater.ts +0 -65
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
3
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
4
|
+
import { PeerMap } from '@libp2p/peer-collections'
|
|
5
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
6
|
+
import { MAX_CONNECTIONS } from './constants.js'
|
|
7
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
8
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
9
|
+
|
|
10
|
+
const log = logger('libp2p:connection-manager:connection-pruner')
|
|
11
|
+
|
|
12
|
+
interface ConnectionPrunerInit {
|
|
13
|
+
maxConnections?: number
|
|
14
|
+
allow?: Multiaddr[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ConnectionPrunerComponents {
|
|
18
|
+
connectionManager: ConnectionManager
|
|
19
|
+
peerStore: PeerStore
|
|
20
|
+
events: EventEmitter<Libp2pEvents>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const defaultOptions = {
|
|
24
|
+
maxConnections: MAX_CONNECTIONS,
|
|
25
|
+
allow: []
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* If we go over the max connections limit, choose some connections to close
|
|
30
|
+
*/
|
|
31
|
+
export class ConnectionPruner {
|
|
32
|
+
private readonly maxConnections: number
|
|
33
|
+
private readonly connectionManager: ConnectionManager
|
|
34
|
+
private readonly peerStore: PeerStore
|
|
35
|
+
private readonly allow: Multiaddr[]
|
|
36
|
+
private readonly events: EventEmitter<Libp2pEvents>
|
|
37
|
+
|
|
38
|
+
constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) {
|
|
39
|
+
this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
|
|
40
|
+
this.allow = init.allow ?? defaultOptions.allow
|
|
41
|
+
this.connectionManager = components.connectionManager
|
|
42
|
+
this.peerStore = components.peerStore
|
|
43
|
+
this.events = components.events
|
|
44
|
+
|
|
45
|
+
// check the max connection limit whenever a peer connects
|
|
46
|
+
components.events.addEventListener('connection:open', () => {
|
|
47
|
+
this.maybePruneConnections()
|
|
48
|
+
.catch(err => {
|
|
49
|
+
log.error(err)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* If we have more connections than our maximum, select some excess connections
|
|
56
|
+
* to prune based on peer value
|
|
57
|
+
*/
|
|
58
|
+
async maybePruneConnections (): Promise<void> {
|
|
59
|
+
const connections = this.connectionManager.getConnections()
|
|
60
|
+
const numConnections = connections.length
|
|
61
|
+
const toPrune = Math.max(numConnections - this.maxConnections, 0)
|
|
62
|
+
|
|
63
|
+
log('checking max connections limit %d/%d', numConnections, this.maxConnections)
|
|
64
|
+
if (numConnections <= this.maxConnections) {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune)
|
|
69
|
+
const peerValues = new PeerMap<number>()
|
|
70
|
+
|
|
71
|
+
// work out peer values
|
|
72
|
+
for (const connection of connections) {
|
|
73
|
+
const remotePeer = connection.remotePeer
|
|
74
|
+
|
|
75
|
+
if (peerValues.has(remotePeer)) {
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
peerValues.set(remotePeer, 0)
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const peer = await this.peerStore.get(remotePeer)
|
|
83
|
+
|
|
84
|
+
// sum all tag values
|
|
85
|
+
peerValues.set(remotePeer, [...peer.tags.values()].reduce((acc, curr) => {
|
|
86
|
+
return acc + curr.value
|
|
87
|
+
}, 0))
|
|
88
|
+
} catch (err: any) {
|
|
89
|
+
if (err.code !== 'ERR_NOT_FOUND') {
|
|
90
|
+
log.error('error loading peer tags', err)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// sort by value, lowest to highest
|
|
96
|
+
const sortedConnections = connections.sort((a, b) => {
|
|
97
|
+
const peerAValue = peerValues.get(a.remotePeer) ?? 0
|
|
98
|
+
const peerBValue = peerValues.get(b.remotePeer) ?? 0
|
|
99
|
+
|
|
100
|
+
if (peerAValue > peerBValue) {
|
|
101
|
+
return 1
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (peerAValue < peerBValue) {
|
|
105
|
+
return -1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// if the peers have an equal tag value then we want to close short-lived connections first
|
|
109
|
+
const connectionALifespan = a.stat.timeline.open
|
|
110
|
+
const connectionBLifespan = b.stat.timeline.open
|
|
111
|
+
|
|
112
|
+
if (connectionALifespan < connectionBLifespan) {
|
|
113
|
+
return 1
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (connectionALifespan > connectionBLifespan) {
|
|
117
|
+
return -1
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return 0
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
// close some connections
|
|
124
|
+
const toClose = []
|
|
125
|
+
|
|
126
|
+
for (const connection of sortedConnections) {
|
|
127
|
+
log('too many connections open - closing a connection to %p', connection.remotePeer)
|
|
128
|
+
// check allow list
|
|
129
|
+
const connectionInAllowList = this.allow.some((ma) => {
|
|
130
|
+
return connection.remoteAddr.toString().startsWith(ma.toString())
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// Connections in the allow list should be excluded from pruning
|
|
134
|
+
if (!connectionInAllowList) {
|
|
135
|
+
toClose.push(connection)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (toClose.length === toPrune) {
|
|
139
|
+
break
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// close connections
|
|
144
|
+
await Promise.all(
|
|
145
|
+
toClose.map(async connection => {
|
|
146
|
+
try {
|
|
147
|
+
await connection.close()
|
|
148
|
+
} catch (err) {
|
|
149
|
+
log.error(err)
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
// despatch prune event
|
|
155
|
+
this.events.safeDispatchEvent('connection:prune', { detail: toClose })
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
|
|
4
|
+
*/
|
|
5
|
+
export const DIAL_TIMEOUT = 30e3
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
|
|
9
|
+
*/
|
|
10
|
+
export const INBOUND_UPGRADE_TIMEOUT = 30e3
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDials
|
|
14
|
+
*/
|
|
15
|
+
export const MAX_PARALLEL_DIALS = 100
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
|
|
19
|
+
*/
|
|
20
|
+
export const MAX_PEER_ADDRS_TO_DIAL = 25
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
|
|
24
|
+
*/
|
|
25
|
+
export const MAX_PARALLEL_DIALS_PER_PEER = 10
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#minConnections
|
|
29
|
+
*/
|
|
30
|
+
export const MIN_CONNECTIONS = 50
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxConnections
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_CONNECTIONS = 300
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
|
|
39
|
+
*/
|
|
40
|
+
export const AUTO_DIAL_INTERVAL = 5000
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialConcurrency
|
|
44
|
+
*/
|
|
45
|
+
export const AUTO_DIAL_CONCURRENCY = 25
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialPriority
|
|
49
|
+
*/
|
|
50
|
+
export const AUTO_DIAL_PRIORITY = 0
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
|
|
54
|
+
*/
|
|
55
|
+
export const INBOUND_CONNECTION_THRESHOLD = 5
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxIncomingPendingConnections
|
|
59
|
+
*/
|
|
60
|
+
export const MAX_INCOMING_PENDING_CONNECTIONS = 10
|