Skip to content

Commit 1147550

Browse files
authored
fix: only configure the latency monitor if a limit is configured (#1527)
If the user does not care about the latency monitor, we should not run an interval sampling the delay as it's expensive and unecessary. The fix here is to only create a latency monitor in the connection manager if the user sets a `maxEventLoopDelay` value.
1 parent 340e2dd commit 1147550

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/connection-manager/index.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
153153
private readonly opts: ConnectionManagerInit
154154
private readonly connections: Map<string, Connection[]>
155155
private started: boolean
156-
private readonly latencyMonitor: LatencyMonitor
156+
private readonly latencyMonitor?: LatencyMonitor
157157
private readonly startupReconnectTimeout: number
158158
private connectOnStartupController?: TimeoutController
159159
private readonly dialTimeout: number
@@ -182,10 +182,12 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
182182

183183
this.started = false
184184

185-
this.latencyMonitor = new LatencyMonitor({
186-
latencyCheckIntervalMs: init.pollInterval,
187-
dataEmitIntervalMs: init.pollInterval
188-
})
185+
if (init.maxEventLoopDelay != null && init.maxEventLoopDelay > 0 && init.maxEventLoopDelay !== Infinity) {
186+
this.latencyMonitor = new LatencyMonitor({
187+
latencyCheckIntervalMs: init.pollInterval,
188+
dataEmitIntervalMs: init.pollInterval
189+
})
190+
}
189191

190192
try {
191193
// This emitter gets listened to a lot
@@ -297,9 +299,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
297299
})
298300

299301
// latency monitor
300-
this.latencyMonitor.start()
302+
this.latencyMonitor?.start()
301303
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
302-
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
304+
this.latencyMonitor?.addEventListener('data', this._onLatencyMeasure)
303305

304306
this.started = true
305307
log('started')
@@ -361,8 +363,8 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
361363
* Stops the Connection Manager
362364
*/
363365
async stop () {
364-
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
365-
this.latencyMonitor.stop()
366+
this.latencyMonitor?.removeEventListener('data', this._onLatencyMeasure)
367+
this.latencyMonitor?.stop()
366368

367369
this.started = false
368370
await this._close()

test/connection-manager/index.node.ts

+39
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,43 @@ describe('libp2p.connections', () => {
570570
expect(denyOutboundUpgradedConnection.getCall(0)).to.have.nested.property('args[0].multihash.digest').that.equalBytes(remoteLibp2p.peerId.multihash.digest)
571571
})
572572
})
573+
574+
describe('latency monitor', () => {
575+
let libp2p: Libp2pNode
576+
577+
afterEach(async () => {
578+
if (libp2p != null) {
579+
await libp2p.stop()
580+
}
581+
})
582+
583+
it('should only start latency monitor if a limit is configured', async () => {
584+
libp2p = await createNode({
585+
config: createBaseOptions({
586+
peerId: peerIds[0],
587+
addresses: {
588+
listen: ['/ip4/127.0.0.1/tcp/0/ws']
589+
}
590+
})
591+
})
592+
593+
expect(libp2p).to.not.have.nested.property('connectionManager.latencyMonitor')
594+
595+
await libp2p.stop()
596+
597+
libp2p = await createNode({
598+
config: createBaseOptions({
599+
peerId: peerIds[0],
600+
addresses: {
601+
listen: ['/ip4/127.0.0.1/tcp/0/ws']
602+
},
603+
connectionManager: {
604+
maxEventLoopDelay: 10000
605+
}
606+
})
607+
})
608+
609+
expect(libp2p).to.have.nested.property('connectionManager.latencyMonitor')
610+
})
611+
})
573612
})

0 commit comments

Comments
 (0)