1
- import { PeerMap } from '@libp2p/peer-collections'
1
+ import { trackedPeerMap } from '@libp2p/peer-collections'
2
2
import { DEFAULT_DATA_LIMIT , DEFAULT_DURATION_LIMIT , DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL , DEFAULT_MAX_RESERVATION_STORE_SIZE , DEFAULT_MAX_RESERVATION_TTL } from '../constants.js'
3
3
import { type Limit , Status } from '../pb/index.js'
4
4
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'
6
7
import type { Multiaddr } from '@multiformats/multiaddr'
7
8
8
9
export type ReservationStatus = Status . OK | Status . PERMISSION_DENIED | Status . RESERVATION_REFUSED
9
10
11
+ export interface ReservationStoreComponents {
12
+ metrics ?: Metrics
13
+ }
14
+
10
15
export interface ReservationStoreInit {
11
16
/**
12
17
* maximum number of reservations allowed
@@ -48,7 +53,7 @@ export interface ReservationStoreInit {
48
53
}
49
54
50
55
export class ReservationStore implements Startable {
51
- public readonly reservations = new PeerMap < RelayReservation > ( )
56
+ public readonly reservations : PeerMap < RelayReservation >
52
57
private _started = false
53
58
private interval : any
54
59
private readonly maxReservations : number
@@ -58,13 +63,18 @@ export class ReservationStore implements Startable {
58
63
private readonly defaultDurationLimit : number
59
64
private readonly defaultDataLimit : bigint
60
65
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
+ } )
68
78
}
69
79
70
80
isStarted ( ) : boolean {
0 commit comments