mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-16 09:01:58 +00:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ef54e0a10e | ||
|
61bf546c46 | ||
|
d2b7ec0f6b | ||
|
79b3cfc6ad | ||
|
f18fc80b70 | ||
|
b4b432406e | ||
|
bbdd559a02 | ||
|
4070dcdf55 | ||
|
cb0d7d6c99 | ||
|
d1c48dcbed | ||
|
c4a442788b | ||
|
70a4bb9451 | ||
|
a0516ebc85 | ||
|
b425fa1230 |
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
@@ -19,7 +19,7 @@ jobs:
|
||||
- run: npx aegir lint
|
||||
- run: npx aegir build
|
||||
- run: npx aegir dep-check
|
||||
- uses: ipfs/aegir/actions/bundle-size
|
||||
- uses: ipfs/aegir/actions/bundle-size@master
|
||||
name: size
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
32
CHANGELOG.md
32
CHANGELOG.md
@@ -1,3 +1,35 @@
|
||||
## [0.35.8](https://github.com/libp2p/js-libp2p/compare/v0.35.7...v0.35.8) (2021-12-29)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* do not wait for autodial start ([#1089](https://github.com/libp2p/js-libp2p/issues/1089)) ([79b3cfc](https://github.com/libp2p/js-libp2p/commit/79b3cfc6ad02ecc76fe23a3c3ff2d0b32a0ae4a8))
|
||||
* increase listeners on any-signal ([#1084](https://github.com/libp2p/js-libp2p/issues/1084)) ([f18fc80](https://github.com/libp2p/js-libp2p/commit/f18fc80b70bf7b6b26fffa70b0a8d0502a6c4801))
|
||||
* look for final peer event instead of peer response ([#1092](https://github.com/libp2p/js-libp2p/issues/1092)) ([d2b7ec0](https://github.com/libp2p/js-libp2p/commit/d2b7ec0f6be0ee80f2c963279a8ec2385059a889))
|
||||
* record tracked map clears ([#1085](https://github.com/libp2p/js-libp2p/issues/1085)) ([b4b4324](https://github.com/libp2p/js-libp2p/commit/b4b432406ebc08ef2fc3a1922c64cde7c9060cae))
|
||||
|
||||
|
||||
|
||||
## [0.35.7](https://github.com/libp2p/js-libp2p/compare/v0.35.2...v0.35.7) (2021-12-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add tracked map ([#1069](https://github.com/libp2p/js-libp2p/issues/1069)) ([b425fa1](https://github.com/libp2p/js-libp2p/commit/b425fa12304def2a007d43a0aa445c28b766ed02))
|
||||
* clean up pending dial targets ([#1059](https://github.com/libp2p/js-libp2p/issues/1059)) ([bdc9f16](https://github.com/libp2p/js-libp2p/commit/bdc9f16d0cbe56ccf26822f11068e7795bcef046))
|
||||
* fix uncaught promise rejection when finding peers ([#1044](https://github.com/libp2p/js-libp2p/issues/1044)) ([3b683e7](https://github.com/libp2p/js-libp2p/commit/3b683e715686163e229b7b5c3a892327dfd4fc63))
|
||||
* increase the maxlisteners for timeout controllers ([#1065](https://github.com/libp2p/js-libp2p/issues/1065)) ([09a0f94](https://github.com/libp2p/js-libp2p/commit/09a0f940df7fdb4ece34604e85693709df5c213e))
|
||||
* main ci ([#1079](https://github.com/libp2p/js-libp2p/issues/1079)) ([d1c48dc](https://github.com/libp2p/js-libp2p/commit/d1c48dcbeded828f2dd3044cc9aed3f17f02846d))
|
||||
* make error codes consistent ([#1054](https://github.com/libp2p/js-libp2p/issues/1054)) ([b25e0fe](https://github.com/libp2p/js-libp2p/commit/b25e0fe5312db58a06c39500ae84c50fed3a93bd))
|
||||
* type definitions for big dialrequest and persistent peerstore ([#1078](https://github.com/libp2p/js-libp2p/issues/1078)) ([cb0d7d6](https://github.com/libp2p/js-libp2p/commit/cb0d7d6c99d179498f04e76df76e70e4f7d41c4c))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* allow per-component metrics to be collected ([#1061](https://github.com/libp2p/js-libp2p/issues/1061)) ([2f0b311](https://github.com/libp2p/js-libp2p/commit/2f0b311df7127aa44512c2008142d4ca30268986)), closes [#1060](https://github.com/libp2p/js-libp2p/issues/1060)
|
||||
|
||||
|
||||
|
||||
## [0.35.6](https://github.com/libp2p/js-libp2p/compare/v0.35.5...v0.35.6) (2021-12-18)
|
||||
|
||||
|
||||
|
@@ -23,8 +23,8 @@
|
||||
<a href="https://david-dm.org/libp2p/js-libp2p"><img src="https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" /></a>
|
||||
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
|
||||
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D6.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D12.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D7.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D15.0.0-orange.svg?style=flat-square" /></a>
|
||||
<br>
|
||||
</p>
|
||||
|
||||
|
@@ -675,15 +675,15 @@ const { NOISE } = require('libp2p-noise')
|
||||
const LevelDatastore = require('datastore-level')
|
||||
|
||||
const datastore = new LevelDatastore('path/to/store')
|
||||
const dsInstant = await datastore.open()
|
||||
await datastore.open() // level database must be ready before node boot
|
||||
|
||||
const node = await Libp2p.create({
|
||||
datastore, // pass the opened datastore
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
datastore: dsInstant,
|
||||
peerStore: {
|
||||
persistence: true,
|
||||
threshold: 5
|
||||
@@ -806,8 +806,8 @@ protocols: [
|
||||
|
||||
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
|
||||
|
||||
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-nodejs.js) - libp2p configuration used by js-ipfs when running in Node.js
|
||||
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
|
||||
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core-config/src/libp2p.js) - libp2p configuration used by js-ipfs when running in Node.js
|
||||
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core-config/src/libp2p.browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
|
||||
|
||||
If you have developed a project using `js-libp2p`, please consider submitting your configuration to this list so that it can be found easily by other users.
|
||||
|
||||
|
19
package.json
19
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.35.6",
|
||||
"version": "0.35.8",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
@@ -182,33 +182,34 @@
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Hugo Dias <mail@hugodias.me>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||
"Volker Mische <volker.mische@gmail.com>",
|
||||
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||
"zeim839 <50573884+zeim839@users.noreply.github.com>",
|
||||
"Robert Kiel <robert.kiel@hoprnet.org>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"zeim839 <50573884+zeim839@users.noreply.github.com>",
|
||||
"a1300 <matthias-knopp@gmx.net>",
|
||||
"Ryan Bell <ryan@piing.net>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
|
||||
"Andrew Nesbitt <andrewnez@gmail.com>",
|
||||
"Franck Royer <franck@royer.one>",
|
||||
"Thomas Eizinger <thomas@eizinger.io>",
|
||||
"Vít Habada <vithabada93@gmail.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
"acolytec3 <17355484+acolytec3@users.noreply.github.com>",
|
||||
"Alan Smithee <ggnore.alan.smithee@gmail.com>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
"Samlior <samlior@foxmail.com>",
|
||||
"Didrik Nordström <didrik.nordstrom@gmail.com>",
|
||||
"Soeren <nikorpoulsen@gmail.com>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
|
||||
"TJKoury <TJKoury@gmail.com>",
|
||||
"TheStarBoys <41286328+TheStarBoys@users.noreply.github.com>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"Tim Daubenschütz <tim@daubenschuetz.de>",
|
||||
"XiaoZhang <zxinmyth@gmail.com>",
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
|
||||
"greenSnot <greenSnot@users.noreply.github.com>",
|
||||
"isan_rivkin <isanrivkin@gmail.com>",
|
||||
"mayerwin <mayerwin@users.noreply.github.com>",
|
||||
"mcclure <andi.m.mcclure@gmail.com>",
|
||||
@@ -217,7 +218,8 @@
|
||||
"robertkiel <robert.kiel@validitylabs.org>",
|
||||
"shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
|
||||
"swedneck <40505480+swedneck@users.noreply.github.com>",
|
||||
"greenSnot <greenSnot@users.noreply.github.com>",
|
||||
"tuyennhv <vutuyen2636@gmail.com>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Aleksei <vozhdb@gmail.com>",
|
||||
"Bernd Strehl <bernd.strehl@gmail.com>",
|
||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||
@@ -248,6 +250,7 @@
|
||||
"Nuno Nogueira <nunofmn@gmail.com>",
|
||||
"Philipp Muens <raute1337@gmx.de>",
|
||||
"RasmusErik Voel Jensen <github@solsort.com>",
|
||||
"Smite Chow <xiaopengyou@live.com>"
|
||||
"Smite Chow <xiaopengyou@live.com>",
|
||||
"Soeren <nikorpoulsen@gmail.com>"
|
||||
]
|
||||
}
|
||||
|
@@ -57,7 +57,9 @@ class AutoDialler {
|
||||
}
|
||||
|
||||
this._running = true
|
||||
this._autoDial()
|
||||
this._autoDial().catch(err => {
|
||||
log.error('could start autodial', err)
|
||||
})
|
||||
log('started')
|
||||
}
|
||||
|
||||
|
@@ -12,7 +12,7 @@ const LatencyMonitor = require('./latency-monitor')
|
||||
const retimer = require('retimer')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const trackedMap = require('../metrics/tracked-map')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const {
|
||||
@@ -34,7 +34,7 @@ const defaultOptions = {
|
||||
|
||||
const METRICS_COMPONENT = 'connection-manager'
|
||||
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
||||
const METRICS_ALL_CONNECTIONS = 'all-connections'
|
||||
const METRICS_PEER_VALUES = 'peer-values'
|
||||
|
||||
/**
|
||||
* @typedef {import('../')} Libp2p
|
||||
@@ -87,14 +87,14 @@ class ConnectionManager extends EventEmitter {
|
||||
*
|
||||
* @type {Map<string, number>}
|
||||
*/
|
||||
this._peerValues = new Map()
|
||||
this._peerValues = trackedMap(METRICS_COMPONENT, METRICS_PEER_VALUES, this._libp2p.metrics)
|
||||
|
||||
/**
|
||||
* Map of connections per peer
|
||||
*
|
||||
* @type {Map<string, Connection[]>}
|
||||
*/
|
||||
this.connections = new Map()
|
||||
this.connections = trackedMap(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this._libp2p.metrics)
|
||||
|
||||
this._started = false
|
||||
this._timer = null
|
||||
@@ -164,8 +164,6 @@ class ConnectionManager extends EventEmitter {
|
||||
|
||||
await Promise.all(tasks)
|
||||
this.connections.clear()
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, 0)
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, 0)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,8 +220,6 @@ class ConnectionManager extends EventEmitter {
|
||||
storedConn.push(connection)
|
||||
} else {
|
||||
this.connections.set(peerIdStr, [connection])
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
|
||||
}
|
||||
|
||||
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
||||
@@ -255,9 +251,6 @@ class ConnectionManager extends EventEmitter {
|
||||
|
||||
this._libp2p.metrics && this._libp2p.metrics.onPeerDisconnected(connection.remotePeer)
|
||||
}
|
||||
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
|
||||
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -27,12 +27,8 @@ class DHTPeerRouting {
|
||||
*/
|
||||
async findPeer (peerId, options = {}) {
|
||||
for await (const event of this._dht.findPeer(peerId, options)) {
|
||||
if (event.name === 'PEER_RESPONSE') {
|
||||
const peer = event.closer.find(peerData => peerData.id.equals(peerId))
|
||||
|
||||
if (peer) {
|
||||
return peer
|
||||
}
|
||||
if (event.name === 'FINAL_PEER') {
|
||||
return event.peer
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,7 @@ const { setMaxListeners } = require('events')
|
||||
const DialRequest = require('./dial-request')
|
||||
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
||||
const getPeer = require('../get-peer')
|
||||
|
||||
const trackedMap = require('../metrics/tracked-map')
|
||||
const { codes } = require('../errors')
|
||||
const {
|
||||
DIAL_TIMEOUT,
|
||||
@@ -56,8 +56,8 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
||||
* @property {Multiaddr[]} addrs
|
||||
*
|
||||
* @typedef PendingDial
|
||||
* @property {DialRequest} dialRequest
|
||||
* @property {TimeoutController} controller
|
||||
* @property {import('./dial-request')} dialRequest
|
||||
* @property {import('timeout-abort-controller').TimeoutController} controller
|
||||
* @property {Promise<Connection>} promise
|
||||
* @property {function():void} destroy
|
||||
*/
|
||||
@@ -86,9 +86,12 @@ class Dialer {
|
||||
this.timeout = dialTimeout
|
||||
this.maxDialsPerPeer = maxDialsPerPeer
|
||||
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
|
||||
this._pendingDials = new Map()
|
||||
this._pendingDialTargets = new Map()
|
||||
this._metrics = metrics
|
||||
|
||||
/** @type {Map<string, PendingDial>} */
|
||||
this._pendingDials = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIALS, metrics)
|
||||
|
||||
/** @type {Map<string, { resolve: (value: any) => void, reject: (err: Error) => void}>} */
|
||||
this._pendingDialTargets = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, metrics)
|
||||
|
||||
for (const [key, value] of Object.entries(resolvers)) {
|
||||
Multiaddr.resolvers.set(key, value)
|
||||
@@ -112,9 +115,6 @@ class Dialer {
|
||||
pendingTarget.reject(new AbortError('Dialer was destroyed'))
|
||||
}
|
||||
this._pendingDialTargets.clear()
|
||||
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, 0)
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, 0)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,7 +164,6 @@ class Dialer {
|
||||
const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString() + Date.now()}`
|
||||
const cancellablePromise = new Promise((resolve, reject) => {
|
||||
this._pendingDialTargets.set(id, { resolve, reject })
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
|
||||
})
|
||||
|
||||
try {
|
||||
@@ -176,7 +175,6 @@ class Dialer {
|
||||
return dialTarget
|
||||
} finally {
|
||||
this._pendingDialTargets.delete(id)
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,14 +252,15 @@ class Dialer {
|
||||
|
||||
// Combine the timeout signal and options.signal, if provided
|
||||
const timeoutController = new TimeoutController(this.timeout)
|
||||
// this controller will potentially be used while dialing lots of
|
||||
// peers so prevent MaxListenersExceededWarning appearing in the console
|
||||
setMaxListeners && setMaxListeners(Infinity, timeoutController.signal)
|
||||
|
||||
const signals = [timeoutController.signal]
|
||||
options.signal && signals.push(options.signal)
|
||||
const signal = anySignal(signals)
|
||||
|
||||
// this signal will potentially be used while dialing lots of
|
||||
// peers so prevent MaxListenersExceededWarning appearing in the console
|
||||
setMaxListeners && setMaxListeners(Infinity, signal)
|
||||
|
||||
const pendingDial = {
|
||||
dialRequest,
|
||||
controller: timeoutController,
|
||||
@@ -269,13 +268,10 @@ class Dialer {
|
||||
destroy: () => {
|
||||
timeoutController.clear()
|
||||
this._pendingDials.delete(dialTarget.id)
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
|
||||
}
|
||||
}
|
||||
this._pendingDials.set(dialTarget.id, pendingDial)
|
||||
|
||||
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
|
||||
|
||||
return pendingDial
|
||||
}
|
||||
|
||||
|
22
src/index.js
22
src/index.js
@@ -48,6 +48,8 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
||||
* @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
|
||||
* @typedef {import('interface-datastore').Datastore} Datastore
|
||||
* @typedef {import('./pnet')} Protector
|
||||
* @typedef {Object} PersistentPeerStoreOptions
|
||||
* @property {number} [threshold]
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -110,7 +112,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
||||
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
|
||||
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
|
||||
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
|
||||
* @property {PeerStoreOptions & import('./peer-store/persistent').PersistentPeerStoreOptions} [peerStore]
|
||||
* @property {PeerStoreOptions & PersistentPeerStoreOptions} [peerStore]
|
||||
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
|
||||
* @property {Libp2pConfig} [config]
|
||||
*
|
||||
@@ -161,6 +163,15 @@ class Libp2p extends EventEmitter {
|
||||
this.peerId = this._options.peerId
|
||||
this.datastore = this._options.datastore
|
||||
|
||||
// Create Metrics
|
||||
if (this._options.metrics.enabled) {
|
||||
const metrics = new Metrics({
|
||||
...this._options.metrics
|
||||
})
|
||||
|
||||
this.metrics = metrics
|
||||
}
|
||||
|
||||
this.peerStore = (this.datastore && this._options.peerStore.persistence)
|
||||
? new PersistentPeerStore({
|
||||
peerId: this.peerId,
|
||||
@@ -195,15 +206,6 @@ class Libp2p extends EventEmitter {
|
||||
autoDialInterval: this._options.connectionManager.autoDialInterval
|
||||
})
|
||||
|
||||
// Create Metrics
|
||||
if (this._options.metrics.enabled) {
|
||||
const metrics = new Metrics({
|
||||
...this._options.metrics
|
||||
})
|
||||
|
||||
this.metrics = metrics
|
||||
}
|
||||
|
||||
// Create keychain
|
||||
if (this._options.keychain && this._options.keychain.datastore) {
|
||||
log('creating keychain')
|
||||
|
@@ -8,6 +8,7 @@ const retimer = require('retimer')
|
||||
|
||||
/**
|
||||
* @typedef {import('@vascosantos/moving-average').IMovingAverage} IMovingAverage
|
||||
* @typedef {import('bignumber.js').BigNumber} Big
|
||||
*/
|
||||
|
||||
class Stats extends EventEmitter {
|
||||
|
68
src/metrics/tracked-map.js
Normal file
68
src/metrics/tracked-map.js
Normal file
@@ -0,0 +1,68 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* @template K
|
||||
* @template V
|
||||
*/
|
||||
class TrackedMap extends Map {
|
||||
/**
|
||||
* @param {string} component
|
||||
* @param {string} name
|
||||
* @param {import('.')} metrics
|
||||
*/
|
||||
constructor (component, name, metrics) {
|
||||
super()
|
||||
|
||||
this._component = component
|
||||
this._name = name
|
||||
this._metrics = metrics
|
||||
|
||||
this._metrics.updateComponentMetric(this._component, this._name, this.size)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {K} key
|
||||
* @param {V} value
|
||||
*/
|
||||
set (key, value) {
|
||||
super.set(key, value)
|
||||
this._metrics.updateComponentMetric(this._component, this._name, this.size)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {K} key
|
||||
*/
|
||||
delete (key) {
|
||||
const deleted = super.delete(key)
|
||||
this._metrics.updateComponentMetric(this._component, this._name, this.size)
|
||||
return deleted
|
||||
}
|
||||
|
||||
clear () {
|
||||
super.clear()
|
||||
|
||||
this._metrics.updateComponentMetric(this._component, this._name, this.size)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template K
|
||||
* @template V
|
||||
* @param {string} component
|
||||
* @param {string} name
|
||||
* @param {import('.')} [metrics]
|
||||
* @returns {Map<K, V>}
|
||||
*/
|
||||
module.exports = (component, name, metrics) => {
|
||||
/** @type {Map<K, V>} */
|
||||
let map
|
||||
|
||||
if (metrics) {
|
||||
map = new TrackedMap(component, name, metrics)
|
||||
} else {
|
||||
map = new Map()
|
||||
}
|
||||
|
||||
return map
|
||||
}
|
Reference in New Issue
Block a user