libp2p 0.43.4 → 0.44.0-0d46c78f
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 +19 -19
- 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 +7 -69
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +60 -174
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +8 -0
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
- package/dist/src/config/connection-gater.browser.js +27 -0
- package/dist/src/config/connection-gater.browser.js.map +1 -0
- package/dist/src/config/connection-gater.d.ts +6 -0
- package/dist/src/config/connection-gater.d.ts.map +1 -0
- package/dist/src/config/connection-gater.js +18 -0
- package/dist/src/config/connection-gater.js.map +1 -0
- package/dist/src/config.d.ts +2 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -71
- 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 +136 -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 +372 -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 +118 -247
- 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.d.ts +0 -10
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +7 -42
- 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.d.ts +13 -35
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +10 -12
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +16 -48
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +203 -201
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -67
- 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 +12 -35
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +141 -206
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -38
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +0 -59
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/constants.d.ts +4 -0
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +10 -0
- package/dist/src/ping/constants.js.map +1 -1
- package/dist/src/ping/index.d.ts +10 -27
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +18 -21
- package/dist/src/ping/index.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 +27 -25
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +15 -4
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +37 -12
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +16 -11
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +38 -47
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +42 -0
- package/dist/src/upnp-nat/index.d.ts.map +1 -0
- package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
- package/dist/src/upnp-nat/index.js.map +1 -0
- package/package.json +70 -54
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +20 -22
- 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 +72 -251
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +3 -73
- package/src/connection-manager/auto-dial.ts +189 -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 +506 -0
- package/src/connection-manager/index.ts +194 -339
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/index.ts +7 -55
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +55 -18
- package/src/identify/index.ts +238 -228
- package/src/index.ts +12 -76
- package/src/insecure/index.ts +9 -5
- package/src/libp2p.ts +146 -265
- package/src/peer-routing.ts +1 -101
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +32 -28
- package/src/registrar.ts +31 -27
- package/src/transport-manager.ts +49 -15
- package/src/upgrader.ts +62 -57
- package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
- 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/dht/dht-content-routing.d.ts +0 -17
- package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-content-routing.js +0 -32
- package/dist/src/dht/dht-content-routing.js.map +0 -1
- package/dist/src/dht/dht-peer-routing.d.ts +0 -15
- package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-peer-routing.js +0 -26
- package/dist/src/dht/dht-peer-routing.js.map +0 -1
- package/dist/src/dht/dummy-dht.d.ts +0 -20
- package/dist/src/dht/dummy-dht.d.ts.map +0 -1
- package/dist/src/dht/dummy-dht.js +0 -46
- package/dist/src/dht/dummy-dht.js.map +0 -1
- package/dist/src/nat-manager.d.ts +0 -74
- package/dist/src/nat-manager.d.ts.map +0 -1
- package/dist/src/nat-manager.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/dht/dht-content-routing.ts +0 -44
- package/src/dht/dht-peer-routing.ts +0 -36
- package/src/dht/dummy-dht.ts +0 -60
- package/src/peer-record-updater.ts +0 -65
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
3
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
4
|
+
import { PeerMap } from '@libp2p/peer-collections'
|
|
5
|
+
import PQueue from 'p-queue'
|
|
6
|
+
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
|
|
7
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
8
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
9
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
10
|
+
|
|
11
|
+
const log = logger('libp2p:connection-manager:auto-dial')
|
|
12
|
+
|
|
13
|
+
interface AutoDialInit {
|
|
14
|
+
minConnections?: number
|
|
15
|
+
autoDialConcurrency?: number
|
|
16
|
+
autoDialPriority?: number
|
|
17
|
+
autoDialInterval?: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface AutoDialComponents {
|
|
21
|
+
connectionManager: ConnectionManager
|
|
22
|
+
peerStore: PeerStore
|
|
23
|
+
events: EventEmitter<Libp2pEvents>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const defaultOptions = {
|
|
27
|
+
minConnections: MIN_CONNECTIONS,
|
|
28
|
+
autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
|
|
29
|
+
autoDialPriority: AUTO_DIAL_PRIORITY,
|
|
30
|
+
autoDialInterval: AUTO_DIAL_INTERVAL
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class AutoDial implements Startable {
|
|
34
|
+
private readonly connectionManager: ConnectionManager
|
|
35
|
+
private readonly peerStore: PeerStore
|
|
36
|
+
private readonly queue: PQueue
|
|
37
|
+
private readonly minConnections: number
|
|
38
|
+
private readonly autoDialPriority: number
|
|
39
|
+
private readonly autoDialIntervalMs: number
|
|
40
|
+
private autoDialInterval?: ReturnType<typeof setInterval>
|
|
41
|
+
private started: boolean
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
45
|
+
* It will keep the number of connections below the upper limit and sort
|
|
46
|
+
* the peers to connect based on whether we know their keys and protocols.
|
|
47
|
+
*/
|
|
48
|
+
constructor (components: AutoDialComponents, init: AutoDialInit) {
|
|
49
|
+
this.connectionManager = components.connectionManager
|
|
50
|
+
this.peerStore = components.peerStore
|
|
51
|
+
this.minConnections = init.minConnections ?? defaultOptions.minConnections
|
|
52
|
+
this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
|
|
53
|
+
this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
|
|
54
|
+
this.started = false
|
|
55
|
+
this.queue = new PQueue({
|
|
56
|
+
concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
|
|
57
|
+
})
|
|
58
|
+
this.queue.addListener('error', (err) => {
|
|
59
|
+
log.error('error during auto-dial', err)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// check the min connection limit whenever a peer disconnects
|
|
63
|
+
components.events.addEventListener('connection:close', () => {
|
|
64
|
+
this.autoDial()
|
|
65
|
+
.catch(err => {
|
|
66
|
+
log.error(err)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
isStarted (): boolean {
|
|
72
|
+
return this.started
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
start (): void {
|
|
76
|
+
this.autoDialInterval = setInterval(() => {
|
|
77
|
+
this.autoDial()
|
|
78
|
+
.catch(err => {
|
|
79
|
+
log.error('error while autodialing', err)
|
|
80
|
+
})
|
|
81
|
+
}, this.autoDialIntervalMs)
|
|
82
|
+
this.started = true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
afterStart (): void {
|
|
86
|
+
this.autoDial()
|
|
87
|
+
.catch(err => {
|
|
88
|
+
log.error('error while autodialing', err)
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
stop (): void {
|
|
93
|
+
// clear the queue
|
|
94
|
+
this.queue.clear()
|
|
95
|
+
clearInterval(this.autoDialInterval)
|
|
96
|
+
this.started = false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async autoDial (): Promise<void> {
|
|
100
|
+
if (!this.started) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const connections = this.connectionManager.getConnectionsMap()
|
|
105
|
+
const numConnections = connections.size
|
|
106
|
+
|
|
107
|
+
// Already has enough connections
|
|
108
|
+
if (numConnections >= this.minConnections) {
|
|
109
|
+
log('have enough connections %d/%d', numConnections, this.minConnections)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
|
|
114
|
+
|
|
115
|
+
// Sort peers on whether we know protocols or public keys for them
|
|
116
|
+
const peers = await this.peerStore.all()
|
|
117
|
+
|
|
118
|
+
// Remove some peers
|
|
119
|
+
const filteredPeers = peers.filter((peer) => {
|
|
120
|
+
// Remove peers without addresses
|
|
121
|
+
if (peer.addresses.length === 0) {
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// remove peers we are already connected to
|
|
126
|
+
if (connections.has(peer.id)) {
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return true
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// shuffle the peers - this is so peers with the same tag values will be
|
|
134
|
+
// dialled in a different order each time
|
|
135
|
+
const shuffledPeers = filteredPeers.sort(() => Math.random() > 0.5 ? 1 : -1)
|
|
136
|
+
|
|
137
|
+
// Sort shuffled peers by tag value
|
|
138
|
+
const peerValues = new PeerMap<number>()
|
|
139
|
+
for (const peer of shuffledPeers) {
|
|
140
|
+
if (peerValues.has(peer.id)) {
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// sum all tag values
|
|
145
|
+
peerValues.set(peer.id, [...peer.tags.values()].reduce((acc, curr) => {
|
|
146
|
+
return acc + curr.value
|
|
147
|
+
}, 0))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// sort by value, highest to lowest
|
|
151
|
+
const sortedPeers = shuffledPeers.sort((a, b) => {
|
|
152
|
+
const peerAValue = peerValues.get(a.id) ?? 0
|
|
153
|
+
const peerBValue = peerValues.get(b.id) ?? 0
|
|
154
|
+
|
|
155
|
+
if (peerAValue > peerBValue) {
|
|
156
|
+
return -1
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (peerAValue < peerBValue) {
|
|
160
|
+
return 1
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return 0
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
log('selected %d/%d peers to dial', sortedPeers.length, peers.length)
|
|
167
|
+
|
|
168
|
+
for (const peer of sortedPeers) {
|
|
169
|
+
this.queue.add(async () => {
|
|
170
|
+
const numConnections = this.connectionManager.getConnectionsMap().size
|
|
171
|
+
|
|
172
|
+
// Check to see if we still need to auto dial
|
|
173
|
+
if (numConnections >= this.minConnections) {
|
|
174
|
+
log('got enough connections now %d/%d', numConnections, this.minConnections)
|
|
175
|
+
this.queue.clear()
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
log('connecting to a peerStore stored peer %p', peer.id)
|
|
180
|
+
await this.connectionManager.openConnection(peer.id, {
|
|
181
|
+
// @ts-expect-error needs adding to the ConnectionManager interface
|
|
182
|
+
priority: this.autoDialPriority
|
|
183
|
+
})
|
|
184
|
+
}).catch(err => {
|
|
185
|
+
log.error('could not connect to peerStore stored peer', err)
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -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
|