-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add versions of peer lists/sets/maps that report their sizes (#…
…2300) Adds collections that report their sizes to the libp2p metrics service. Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com> --------- Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com>
- Loading branch information
1 parent
3bf6387
commit 57944fa
Showing
11 changed files
with
607 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,3 +122,7 @@ export class PeerSet { | |
return output | ||
} | ||
} | ||
|
||
export function peerSet (): PeerSet { | ||
return new PeerSet() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { PeerList } from './list.js' | ||
import type { Metric, Metrics, PeerId } from '@libp2p/interface' | ||
|
||
export interface TrackedPeerListInit { | ||
name: string | ||
metrics: Metrics | ||
} | ||
|
||
class TrackedPeerList extends PeerList { | ||
private readonly metric: Metric | ||
|
||
constructor (init: TrackedPeerListInit) { | ||
super() | ||
|
||
const { name, metrics } = init | ||
|
||
this.metric = metrics.registerMetric(name) | ||
this.updateComponentMetric() | ||
} | ||
|
||
pop (): PeerId | undefined { | ||
const peerId = super.pop() | ||
this.updateComponentMetric() | ||
return peerId | ||
} | ||
|
||
push (...peerIds: PeerId[]): void { | ||
super.push(...peerIds) | ||
this.updateComponentMetric() | ||
} | ||
|
||
shift (): PeerId | undefined { | ||
const peerId = super.shift() | ||
this.updateComponentMetric() | ||
return peerId | ||
} | ||
|
||
unshift (...peerIds: PeerId[]): number { | ||
const result = super.unshift(...peerIds) | ||
this.updateComponentMetric() | ||
return result | ||
} | ||
|
||
clear (): void { | ||
super.clear() | ||
this.updateComponentMetric() | ||
} | ||
|
||
private updateComponentMetric (): void { | ||
this.metric.update(this.length) | ||
} | ||
} | ||
|
||
export interface CreateTrackedPeerListInit { | ||
/** | ||
* The metric name to use | ||
*/ | ||
name: string | ||
|
||
/** | ||
* A metrics implementation | ||
*/ | ||
metrics?: Metrics | ||
} | ||
|
||
/** | ||
* Creates a PeerList that reports it's size to the libp2p Metrics service | ||
* | ||
* @example | ||
* | ||
* * ```Typescript | ||
* import { trackedPeerList } from '@libp2p/peer-collections' | ||
* import { createLibp2p } from 'libp2p' | ||
* | ||
* const libp2p = await createLibp2p() | ||
* | ||
* const list = trackedPeerList({ name: 'my_metric_name', metrics: libp2p.metrics }) | ||
* list.push(peerId) | ||
* ``` | ||
*/ | ||
export function trackedPeerList (config: CreateTrackedPeerListInit): PeerList { | ||
const { name, metrics } = config | ||
let map: PeerList | ||
|
||
if (metrics != null) { | ||
map = new TrackedPeerList({ name, metrics }) | ||
} else { | ||
map = new PeerList() | ||
} | ||
|
||
return map | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { PeerMap } from './map.js' | ||
import type { Metric, Metrics, PeerId } from '@libp2p/interface' | ||
|
||
export interface TrackedPeerMapInit { | ||
name: string | ||
metrics: Metrics | ||
} | ||
|
||
class TrackedPeerMap<V> extends PeerMap<V> { | ||
private readonly metric: Metric | ||
|
||
constructor (init: TrackedPeerMapInit) { | ||
super() | ||
|
||
const { name, metrics } = init | ||
|
||
this.metric = metrics.registerMetric(name) | ||
this.updateComponentMetric() | ||
} | ||
|
||
set (key: PeerId, value: V): this { | ||
super.set(key, value) | ||
this.updateComponentMetric() | ||
return this | ||
} | ||
|
||
delete (key: PeerId): boolean { | ||
const deleted = super.delete(key) | ||
this.updateComponentMetric() | ||
return deleted | ||
} | ||
|
||
clear (): void { | ||
super.clear() | ||
this.updateComponentMetric() | ||
} | ||
|
||
private updateComponentMetric (): void { | ||
this.metric.update(this.size) | ||
} | ||
} | ||
|
||
export interface CreateTrackedPeerMapInit { | ||
/** | ||
* The metric name to use | ||
*/ | ||
name: string | ||
|
||
/** | ||
* A metrics implementation | ||
*/ | ||
metrics?: Metrics | ||
} | ||
|
||
/** | ||
* Creates a PeerMap that reports it's size to the libp2p Metrics service | ||
* | ||
* @example | ||
* | ||
* * ```Typescript | ||
* import { trackedPeerMap } from '@libp2p/peer-collections' | ||
* import { createLibp2p } from 'libp2p' | ||
* | ||
* const libp2p = await createLibp2p() | ||
* | ||
* const list = trackedPeerMap({ name: 'my_metric_name', metrics: libp2p.metrics }) | ||
* map.set(peerId, 'value') | ||
* ``` | ||
*/ | ||
export function trackedPeerMap <V> (config: CreateTrackedPeerMapInit): PeerMap<V> { | ||
const { name, metrics } = config | ||
let map: PeerMap<V> | ||
|
||
if (metrics != null) { | ||
map = new TrackedPeerMap<V>({ name, metrics }) | ||
} else { | ||
map = new PeerMap<V>() | ||
} | ||
|
||
return map | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { PeerSet } from './set.js' | ||
import type { Metric, Metrics, PeerId } from '@libp2p/interface' | ||
|
||
export interface TrackedPeerSetInit { | ||
name: string | ||
metrics: Metrics | ||
} | ||
|
||
class TrackedPeerSet extends PeerSet { | ||
private readonly metric: Metric | ||
|
||
constructor (init: TrackedPeerSetInit) { | ||
super() | ||
|
||
const { name, metrics } = init | ||
|
||
this.metric = metrics.registerMetric(name) | ||
this.updateComponentMetric() | ||
} | ||
|
||
add (peer: PeerId): void { | ||
super.add(peer) | ||
this.updateComponentMetric() | ||
} | ||
|
||
delete (peer: PeerId): void { | ||
super.delete(peer) | ||
this.updateComponentMetric() | ||
} | ||
|
||
clear (): void { | ||
super.clear() | ||
this.updateComponentMetric() | ||
} | ||
|
||
private updateComponentMetric (): void { | ||
this.metric.update(this.size) | ||
} | ||
} | ||
|
||
export interface CreateTrackedPeerSetInit { | ||
/** | ||
* The metric name to use | ||
*/ | ||
name: string | ||
|
||
/** | ||
* A metrics implementation | ||
*/ | ||
metrics?: Metrics | ||
} | ||
|
||
/** | ||
* Creates a PeerSet that reports it's size to the libp2p Metrics service | ||
* | ||
* @example Tracked peer sets | ||
* | ||
* * ```Typescript | ||
* import { trackedPeerSet } from '@libp2p/peer-collections' | ||
* import { createLibp2p } from 'libp2p' | ||
* | ||
* const libp2p = await createLibp2p() | ||
* | ||
* const list = trackedPeerSet({ name: 'my_metric_name', metrics: libp2p.metrics }) | ||
* map.add(peerId) | ||
* ``` | ||
*/ | ||
export function trackedPeerSet (config: CreateTrackedPeerSetInit): PeerSet { | ||
const { name, metrics } = config | ||
let map: PeerSet | ||
|
||
if (metrics != null) { | ||
map = new TrackedPeerSet({ name, metrics }) | ||
} else { | ||
map = new PeerSet() | ||
} | ||
|
||
return map | ||
} |
Oops, something went wrong.