libp2p 0.40.0 → 0.41.0
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/README.md +21 -2
- 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/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -11
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +83 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +96 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection-manager/dialer/index.d.ts +13 -14
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +82 -62
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +2 -10
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +21 -53
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +15 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/get-peer.d.ts +2 -2
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +2 -5
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +10 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/keychain/index.d.ts +1 -1
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +3 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -0
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +25 -0
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +1 -2
- 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 +46 -59
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +9 -10
- package/src/address-manager/index.ts +3 -2
- package/src/circuit/README.md +6 -6
- package/src/config.ts +0 -11
- package/src/connection/index.ts +171 -0
- package/src/connection-manager/dialer/index.ts +100 -75
- package/src/connection-manager/index.ts +21 -58
- package/src/fetch/index.ts +16 -3
- package/src/get-peer.ts +3 -7
- package/src/index.ts +15 -7
- package/src/keychain/index.ts +1 -1
- package/src/libp2p.ts +5 -6
- package/src/registrar.ts +29 -2
- package/src/transport-manager.ts +1 -2
- package/src/upgrader.ts +51 -64
- package/src/version.ts +1 -1
- package/dist/src/metrics/index.d.ts +0 -93
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -234
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/moving-average.d.ts +0 -14
- package/dist/src/metrics/moving-average.d.ts.map +0 -1
- package/dist/src/metrics/moving-average.js +0 -40
- package/dist/src/metrics/moving-average.js.map +0 -1
- package/dist/src/metrics/stats.d.ts +0 -87
- package/dist/src/metrics/stats.d.ts.map +0 -1
- package/dist/src/metrics/stats.js +0 -183
- package/dist/src/metrics/stats.js.map +0 -1
- package/src/metrics/index.ts +0 -311
- package/src/metrics/moving-average.ts +0 -53
- package/src/metrics/stats.ts +0 -238
package/src/metrics/stats.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
2
|
-
import { createMovingAverage } from './moving-average.js'
|
|
3
|
-
// @ts-expect-error no types
|
|
4
|
-
import retimer from 'retimer'
|
|
5
|
-
import type { MovingAverages, Stats, TransferStats } from '@libp2p/interface-metrics'
|
|
6
|
-
|
|
7
|
-
export interface StatsEvents {
|
|
8
|
-
'update': CustomEvent<TransferStats>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface StatsInit {
|
|
12
|
-
enabled: boolean
|
|
13
|
-
initialCounters: ['dataReceived', 'dataSent']
|
|
14
|
-
movingAverageIntervals: number[]
|
|
15
|
-
computeThrottleMaxQueueSize: number
|
|
16
|
-
computeThrottleTimeout: number
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class DefaultStats extends EventEmitter<StatsEvents> implements Stats {
|
|
20
|
-
private readonly enabled: boolean
|
|
21
|
-
public queue: Array<[string, number, number]>
|
|
22
|
-
private stats: TransferStats
|
|
23
|
-
private frequencyLastTime: number
|
|
24
|
-
private frequencyAccumulators: Record<string, number>
|
|
25
|
-
private movingAverages: MovingAverages
|
|
26
|
-
private timeout?: any
|
|
27
|
-
private readonly computeThrottleMaxQueueSize: number
|
|
28
|
-
private readonly computeThrottleTimeout: number
|
|
29
|
-
private readonly movingAverageIntervals: number[]
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* A queue based manager for stat processing
|
|
33
|
-
*/
|
|
34
|
-
constructor (init: StatsInit) {
|
|
35
|
-
super()
|
|
36
|
-
|
|
37
|
-
this.enabled = init.enabled
|
|
38
|
-
this.queue = []
|
|
39
|
-
this.stats = {
|
|
40
|
-
dataReceived: 0n,
|
|
41
|
-
dataSent: 0n
|
|
42
|
-
}
|
|
43
|
-
this.frequencyLastTime = Date.now()
|
|
44
|
-
this.frequencyAccumulators = {}
|
|
45
|
-
this.movingAverages = {
|
|
46
|
-
dataReceived: [],
|
|
47
|
-
dataSent: []
|
|
48
|
-
}
|
|
49
|
-
this.computeThrottleMaxQueueSize = init.computeThrottleMaxQueueSize
|
|
50
|
-
this.computeThrottleTimeout = init.computeThrottleTimeout
|
|
51
|
-
|
|
52
|
-
this._update = this._update.bind(this)
|
|
53
|
-
|
|
54
|
-
this.movingAverageIntervals = init.movingAverageIntervals
|
|
55
|
-
|
|
56
|
-
for (let i = 0; i < init.initialCounters.length; i++) {
|
|
57
|
-
const key = init.initialCounters[i]
|
|
58
|
-
this.stats[key] = 0n
|
|
59
|
-
this.movingAverages[key] = []
|
|
60
|
-
|
|
61
|
-
for (let k = 0; k < this.movingAverageIntervals.length; k++) {
|
|
62
|
-
const interval = this.movingAverageIntervals[k]
|
|
63
|
-
const ma = this.movingAverages[key][interval] = createMovingAverage(interval)
|
|
64
|
-
ma.push(this.frequencyLastTime, 0)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Initializes the internal timer if there are items in the queue. This
|
|
71
|
-
* should only need to be called if `Stats.stop` was previously called, as
|
|
72
|
-
* `Stats.push` will also start the processing
|
|
73
|
-
*/
|
|
74
|
-
start () {
|
|
75
|
-
if (!this.enabled) {
|
|
76
|
-
return
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (this.queue.length > 0) {
|
|
80
|
-
this._resetComputeTimeout()
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Stops processing and computing of stats by clearing the internal
|
|
86
|
-
* timer
|
|
87
|
-
*/
|
|
88
|
-
stop () {
|
|
89
|
-
if (this.timeout != null) {
|
|
90
|
-
this.timeout.clear()
|
|
91
|
-
this.timeout = null
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Returns a clone of the current stats.
|
|
97
|
-
*/
|
|
98
|
-
getSnapshot () {
|
|
99
|
-
return Object.assign({}, this.stats)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Returns a clone of the internal movingAverages
|
|
104
|
-
*/
|
|
105
|
-
getMovingAverages (): MovingAverages {
|
|
106
|
-
return Object.assign({}, this.movingAverages)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Pushes the given operation data to the queue, along with the
|
|
111
|
-
* current Timestamp, then resets the update timer.
|
|
112
|
-
*/
|
|
113
|
-
push (counter: string, inc: number) {
|
|
114
|
-
this.queue.push([counter, inc, Date.now()])
|
|
115
|
-
this._resetComputeTimeout()
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Resets the timeout for triggering updates.
|
|
120
|
-
*/
|
|
121
|
-
_resetComputeTimeout () {
|
|
122
|
-
this.timeout = retimer(this._update, this._nextTimeout())
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Calculates and returns the timeout for the next update based on
|
|
127
|
-
* the urgency of the update.
|
|
128
|
-
*/
|
|
129
|
-
_nextTimeout () {
|
|
130
|
-
// calculate the need for an update, depending on the queue length
|
|
131
|
-
const urgency = this.queue.length / this.computeThrottleMaxQueueSize
|
|
132
|
-
const timeout = Math.max(this.computeThrottleTimeout * (1 - urgency), 0)
|
|
133
|
-
return timeout
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* If there are items in the queue, they will will be processed and
|
|
138
|
-
* the frequency for all items will be updated based on the Timestamp
|
|
139
|
-
* of the last item in the queue. The `update` event will also be emitted
|
|
140
|
-
* with the latest stats.
|
|
141
|
-
*
|
|
142
|
-
* If there are no items in the queue, no action is taken.
|
|
143
|
-
*/
|
|
144
|
-
_update () {
|
|
145
|
-
this.timeout = null
|
|
146
|
-
if (this.queue.length > 0) {
|
|
147
|
-
let last: [string, number, number] = ['', 0, 0]
|
|
148
|
-
|
|
149
|
-
for (last of this.queue) {
|
|
150
|
-
this._applyOp(last)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.queue = []
|
|
154
|
-
|
|
155
|
-
if (last.length > 2 && last[0] !== '') {
|
|
156
|
-
this._updateFrequency(last[2]) // contains timestamp of last op
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
this.dispatchEvent(new CustomEvent<TransferStats>('update', {
|
|
160
|
-
detail: this.stats
|
|
161
|
-
}))
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* For each key in the stats, the frequency and moving averages
|
|
167
|
-
* will be updated via Stats._updateFrequencyFor based on the time
|
|
168
|
-
* difference between calls to this method.
|
|
169
|
-
*/
|
|
170
|
-
_updateFrequency (latestTime: number) {
|
|
171
|
-
const timeDiff = latestTime - this.frequencyLastTime
|
|
172
|
-
|
|
173
|
-
this._updateFrequencyFor('dataReceived', timeDiff, latestTime)
|
|
174
|
-
this._updateFrequencyFor('dataSent', timeDiff, latestTime)
|
|
175
|
-
|
|
176
|
-
this.frequencyLastTime = latestTime
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Updates the `movingAverages` for the given `key` and also
|
|
181
|
-
* resets the `frequencyAccumulator` for the `key`.
|
|
182
|
-
*/
|
|
183
|
-
_updateFrequencyFor (key: 'dataReceived' | 'dataSent', timeDiffMS: number, latestTime: number) {
|
|
184
|
-
const count = this.frequencyAccumulators[key] ?? 0
|
|
185
|
-
this.frequencyAccumulators[key] = 0
|
|
186
|
-
// if `timeDiff` is zero, `hz` becomes Infinity, so we fallback to 1ms
|
|
187
|
-
const safeTimeDiff = timeDiffMS ?? 1
|
|
188
|
-
const hz = (count / safeTimeDiff) * 1000
|
|
189
|
-
|
|
190
|
-
let movingAverages = this.movingAverages[key]
|
|
191
|
-
if (movingAverages == null) {
|
|
192
|
-
movingAverages = this.movingAverages[key] = []
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const intervals = this.movingAverageIntervals
|
|
196
|
-
|
|
197
|
-
for (let i = 0; i < intervals.length; i++) {
|
|
198
|
-
const movingAverageInterval = intervals[i]
|
|
199
|
-
let movingAverage = movingAverages[movingAverageInterval]
|
|
200
|
-
if (movingAverage == null) {
|
|
201
|
-
movingAverage = movingAverages[movingAverageInterval] = createMovingAverage(movingAverageInterval)
|
|
202
|
-
}
|
|
203
|
-
movingAverage.push(latestTime, hz)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* For the given operation, `op`, the stats and `frequencyAccumulator`
|
|
209
|
-
* will be updated or initialized if they don't already exist.
|
|
210
|
-
*/
|
|
211
|
-
_applyOp (op: [string, number, number]) {
|
|
212
|
-
const key = op[0]
|
|
213
|
-
const inc = op[1]
|
|
214
|
-
|
|
215
|
-
if (typeof inc !== 'number') {
|
|
216
|
-
throw new Error('invalid increment number')
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
let n: bigint
|
|
220
|
-
|
|
221
|
-
if (!Object.prototype.hasOwnProperty.call(this.stats, key)) {
|
|
222
|
-
// @ts-expect-error cannot index type with key
|
|
223
|
-
n = this.stats[key] = 0n
|
|
224
|
-
} else {
|
|
225
|
-
// @ts-expect-error cannot index type with key
|
|
226
|
-
n = this.stats[key]
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// @ts-expect-error cannot index type with key
|
|
230
|
-
this.stats[key] = n + BigInt(inc)
|
|
231
|
-
|
|
232
|
-
if (this.frequencyAccumulators[key] == null) {
|
|
233
|
-
this.frequencyAccumulators[key] = 0
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
this.frequencyAccumulators[key] += inc
|
|
237
|
-
}
|
|
238
|
-
}
|