Skip to content

Commit 60f6aa9

Browse files
authored
feat: add relay server reservation store metrics (#2722)
Tracks the reservation store map to observe how many reservation slots are currently occupied. Also reinstates tests that were accidentally removed by the linter.
1 parent 4cc316c commit 60f6aa9

File tree

6 files changed

+681
-12
lines changed

6 files changed

+681
-12
lines changed

packages/transport-circuit-relay-v2/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@
7272
"uint8arrays": "^5.1.0"
7373
},
7474
"devDependencies": {
75+
"@libp2p/crypto": "^5.0.4",
7576
"@libp2p/interface-compliance-tests": "^6.1.2",
77+
"@libp2p/logger": "^5.0.4",
7678
"aegir": "^44.0.1",
7779
"delay": "^6.0.0",
7880
"it-drain": "^3.0.7",
81+
"it-pair": "^2.0.6",
7982
"it-pushable": "^3.2.3",
8083
"it-to-buffer": "^4.0.7",
8184
"p-wait-for": "^5.0.2",

packages/transport-circuit-relay-v2/src/server/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { createLimitedRelay } from '../utils.js'
1818
import { ReservationStore, type ReservationStoreInit } from './reservation-store.js'
1919
import { ReservationVoucherRecord } from './reservation-voucher.js'
2020
import type { CircuitRelayService, RelayReservation } from '../index.js'
21-
import type { ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, Startable, PrivateKey } from '@libp2p/interface'
21+
import type { ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, Startable, PrivateKey, Metrics } from '@libp2p/interface'
2222
import type { AddressManager, ConnectionManager, IncomingStreamData, Registrar } from '@libp2p/interface-internal'
2323
import type { PeerMap } from '@libp2p/peer-collections'
2424

@@ -77,6 +77,7 @@ export interface CircuitRelayServerComponents {
7777
connectionManager: ConnectionManager
7878
connectionGater: ConnectionGater
7979
logger: ComponentLogger
80+
metrics?: Metrics
8081
}
8182

8283
export interface RelayServerEvents {
@@ -125,7 +126,7 @@ class CircuitRelayServer extends TypedEventEmitter<RelayServerEvents> implements
125126
this.maxInboundHopStreams = init.maxInboundHopStreams
126127
this.maxOutboundHopStreams = init.maxOutboundHopStreams
127128
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
128-
this.reservationStore = new ReservationStore(init.reservations)
129+
this.reservationStore = new ReservationStore(components, init.reservations)
129130

130131
this.shutdownController = new AbortController()
131132
setMaxListeners(Infinity, this.shutdownController.signal)

packages/transport-circuit-relay-v2/src/server/reservation-store.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { PeerMap } from '@libp2p/peer-collections'
1+
import { trackedPeerMap } from '@libp2p/peer-collections'
22
import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js'
33
import { type Limit, Status } from '../pb/index.js'
44
import type { RelayReservation } from '../index.js'
5-
import type { PeerId, Startable } from '@libp2p/interface'
5+
import type { Metrics, PeerId, Startable } from '@libp2p/interface'
6+
import type { PeerMap } from '@libp2p/peer-collections'
67
import type { Multiaddr } from '@multiformats/multiaddr'
78

89
export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED
910

11+
export interface ReservationStoreComponents {
12+
metrics?: Metrics
13+
}
14+
1015
export interface ReservationStoreInit {
1116
/**
1217
* maximum number of reservations allowed
@@ -48,7 +53,7 @@ export interface ReservationStoreInit {
4853
}
4954

5055
export class ReservationStore implements Startable {
51-
public readonly reservations = new PeerMap<RelayReservation>()
56+
public readonly reservations: PeerMap<RelayReservation>
5257
private _started = false
5358
private interval: any
5459
private readonly maxReservations: number
@@ -58,13 +63,18 @@ export class ReservationStore implements Startable {
5863
private readonly defaultDurationLimit: number
5964
private readonly defaultDataLimit: bigint
6065

61-
constructor (options: ReservationStoreInit = {}) {
62-
this.maxReservations = options.maxReservations ?? DEFAULT_MAX_RESERVATION_STORE_SIZE
63-
this.reservationClearInterval = options.reservationClearInterval ?? DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL
64-
this.applyDefaultLimit = options.applyDefaultLimit !== false
65-
this.reservationTtl = options.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL
66-
this.defaultDurationLimit = options.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT
67-
this.defaultDataLimit = options.defaultDataLimit ?? DEFAULT_DATA_LIMIT
66+
constructor (components: ReservationStoreComponents, init: ReservationStoreInit = {}) {
67+
this.maxReservations = init.maxReservations ?? DEFAULT_MAX_RESERVATION_STORE_SIZE
68+
this.reservationClearInterval = init.reservationClearInterval ?? DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL
69+
this.applyDefaultLimit = init.applyDefaultLimit !== false
70+
this.reservationTtl = init.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL
71+
this.defaultDurationLimit = init.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT
72+
this.defaultDataLimit = init.defaultDataLimit ?? DEFAULT_DATA_LIMIT
73+
74+
this.reservations = trackedPeerMap<RelayReservation>({
75+
metrics: components.metrics,
76+
name: 'libp2p_circuit_relay_server_reservations_total'
77+
})
6878
}
6979

7080
isStarted (): boolean {

0 commit comments

Comments
 (0)