Skip to content

Commit 1770c3d

Browse files
committed
feat: add config validation to perf service
1 parent cf3dd84 commit 1770c3d

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

packages/libp2p/src/connection-manager/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/mult
33
import { type ClearableSignal, anySignal } from 'any-signal'
44
import { type ObjectSchema, array, number, object, string } from 'yup'
55
import { validateMultiaddr } from '../config/helpers.js'
6-
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, INBOUND_UPGRADE_TIMEOUT, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
6+
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, INBOUND_UPGRADE_TIMEOUT, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
77
import type { ConnectionManagerInit } from '.'
88
import type { LoggerOptions } from '@libp2p/interface'
99

packages/protocol-fetch/src/fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
2222

2323
const configValidator = object({
2424
timeout: number().integer().default(TIMEOUT),
25-
maxInboundStreams: number().integer().default(MAX_INBOUND_STREAMS),
26-
maxOutboundStreams: number().integer().default(MAX_OUTBOUND_STREAMS)
25+
maxInboundStreams: number().integer().min(0).default(MAX_INBOUND_STREAMS),
26+
maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS)
2727
})
2828
export class Fetch implements Startable, FetchInterface {
2929
public readonly protocol: string

packages/protocol-perf/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"@libp2p/interface": "^0.1.6",
5252
"@libp2p/interface-internal": "^0.1.9",
5353
"@multiformats/multiaddr": "^12.1.10",
54-
"it-pushable": "^3.2.3"
54+
"it-pushable": "^3.2.3",
55+
"yup": "^1.3.2"
5556
},
5657
"devDependencies": {
5758
"@libp2p/interface-compliance-tests": "^4.1.5",

packages/protocol-perf/src/perf-service.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { pushable } from 'it-pushable'
2+
import { boolean, number, object, string } from 'yup'
23
import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, RUN_ON_TRANSIENT_CONNECTION, WRITE_BLOCK_SIZE } from './constants.js'
34
import type { PerfOptions, PerfOutput, PerfComponents, PerfInit, Perf as PerfInterface } from './index.js'
45
import type { Logger } from '@libp2p/interface'
56
import type { Startable } from '@libp2p/interface/startable'
67
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
78
import type { Multiaddr } from '@multiformats/multiaddr'
89

10+
const configValidator = object({
11+
maxInboundStreams: number().integer().min(0).default(MAX_INBOUND_STREAMS),
12+
maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS),
13+
protocolName: string().default(PROTOCOL_NAME),
14+
writeBlockSize: number().integer().min(1).default(WRITE_BLOCK_SIZE),
15+
runOnTransientConnection: boolean().default(RUN_ON_TRANSIENT_CONNECTION)
16+
})
17+
918
export class Perf implements Startable, PerfInterface {
1019
private readonly log: Logger
1120
public readonly protocol: string
@@ -21,12 +30,15 @@ export class Perf implements Startable, PerfInterface {
2130
this.components = components
2231
this.log = components.logger.forComponent('libp2p:perf')
2332
this.started = false
24-
this.protocol = init.protocolName ?? PROTOCOL_NAME
25-
this.writeBlockSize = init.writeBlockSize ?? WRITE_BLOCK_SIZE
33+
34+
const config = configValidator.validateSync(init)
35+
36+
this.protocol = config.protocolName
37+
this.writeBlockSize = config.writeBlockSize
38+
this.maxInboundStreams = config.maxInboundStreams
39+
this.maxOutboundStreams = config.maxOutboundStreams
40+
this.runOnTransientConnection = config.runOnTransientConnection
2641
this.databuf = new ArrayBuffer(this.writeBlockSize)
27-
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
28-
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
29-
this.runOnTransientConnection = init.runOnTransientConnection ?? RUN_ON_TRANSIENT_CONNECTION
3042
}
3143

3244
async start (): Promise<void> {

packages/transport-circuit-relay-v2/src/server/advert-service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TypedEventEmitter } from '@libp2p/interface/events'
22
import pRetry from 'p-retry'
3+
import { number, object } from 'yup'
34
import {
45
DEFAULT_ADVERT_BOOT_DELAY,
56
ERR_NO_ROUTERS_AVAILABLE,
@@ -30,6 +31,10 @@ export interface AdvertServiceEvents {
3031
'advert:error': CustomEvent<Error>
3132
}
3233

34+
const configValidator = object({
35+
bootDelay: number().integer().min(0).default(DEFAULT_ADVERT_BOOT_DELAY)
36+
})
37+
3338
export class AdvertService extends TypedEventEmitter<AdvertServiceEvents> implements Startable {
3439
private readonly contentRouting: ContentRouting
3540
private timeout?: any
@@ -45,8 +50,11 @@ export class AdvertService extends TypedEventEmitter<AdvertServiceEvents> implem
4550

4651
this.log = components.logger.forComponent('libp2p:circuit-relay:advert-service')
4752
this.contentRouting = components.contentRouting
48-
this.bootDelay = init?.bootDelay ?? DEFAULT_ADVERT_BOOT_DELAY
4953
this.started = false
54+
55+
const config = configValidator.validateSync(init)
56+
57+
this.bootDelay = config.bootDelay
5058
}
5159

5260
isStarted (): boolean {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PeerJobQueue } from '@libp2p/utils/peer-job-queue'
44
import { multiaddr } from '@multiformats/multiaddr'
55
import { pbStream } from 'it-protobuf-stream'
66
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
7+
import { number, object } from 'yup'
78
import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js'
89
import { HopMessage, Status } from '../pb/index.js'
910
import { getExpirationMilliseconds } from '../utils.js'
@@ -75,6 +76,13 @@ export interface ReservationStoreEvents {
7576
'relay:removed': CustomEvent<PeerId>
7677
}
7778

79+
const configValidator = object({
80+
discoverRelays: number().integer().min(0).default(0),
81+
maxReservationQueueLength: number().integer().min(0).default(100),
82+
reservationCompletionTimeout: number().integer().min(0).default(10000),
83+
reservationConcurrency: number().integer().min(0).default(DEFAULT_RESERVATION_CONCURRENCY)
84+
})
85+
7886
export class ReservationStore extends TypedEventEmitter<ReservationStoreEvents> implements Startable {
7987
private readonly peerId: PeerId
8088
private readonly connectionManager: ConnectionManager
@@ -99,14 +107,17 @@ export class ReservationStore extends TypedEventEmitter<ReservationStoreEvents>
99107
this.peerStore = components.peerStore
100108
this.events = components.events
101109
this.reservations = new PeerMap()
102-
this.maxDiscoveredRelays = init?.discoverRelays ?? 0
103-
this.maxReservationQueueLength = init?.maxReservationQueueLength ?? 100
104-
this.reservationCompletionTimeout = init?.reservationCompletionTimeout ?? 10000
110+
111+
const config = configValidator.validateSync(init)
112+
113+
this.maxDiscoveredRelays = config.discoverRelays
114+
this.maxReservationQueueLength = config.maxReservationQueueLength
115+
this.reservationCompletionTimeout = config.reservationCompletionTimeout
105116
this.started = false
106117

107118
// ensure we don't listen on multiple relays simultaneously
108119
this.reserveQueue = new PeerJobQueue({
109-
concurrency: init?.reservationConcurrency ?? DEFAULT_RESERVATION_CONCURRENCY
120+
concurrency: config.reservationConcurrency
110121
})
111122

112123
// When a peer disconnects, if we had a reservation on that peer

0 commit comments

Comments
 (0)