diff --git a/src/connection-manager/index.ts b/src/connection-manager/index.ts index f4bc74e159..aa4a14a2ba 100644 --- a/src/connection-manager/index.ts +++ b/src/connection-manager/index.ts @@ -153,7 +153,7 @@ export class DefaultConnectionManager extends EventEmitter private started: boolean - private readonly latencyMonitor: LatencyMonitor + private readonly latencyMonitor?: LatencyMonitor private readonly startupReconnectTimeout: number private connectOnStartupController?: TimeoutController private readonly dialTimeout: number @@ -182,10 +182,12 @@ export class DefaultConnectionManager extends EventEmitter 0 && init.maxEventLoopDelay !== Infinity) { + this.latencyMonitor = new LatencyMonitor({ + latencyCheckIntervalMs: init.pollInterval, + dataEmitIntervalMs: init.pollInterval + }) + } try { // This emitter gets listened to a lot @@ -297,9 +299,9 @@ export class DefaultConnectionManager extends EventEmitter { expect(denyOutboundUpgradedConnection.getCall(0)).to.have.nested.property('args[0].multihash.digest').that.equalBytes(remoteLibp2p.peerId.multihash.digest) }) }) + + describe('latency monitor', () => { + let libp2p: Libp2pNode + + afterEach(async () => { + if (libp2p != null) { + await libp2p.stop() + } + }) + + it('should only start latency monitor if a limit is configured', async () => { + libp2p = await createNode({ + config: createBaseOptions({ + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/0/ws'] + } + }) + }) + + expect(libp2p).to.not.have.nested.property('connectionManager.latencyMonitor') + + await libp2p.stop() + + libp2p = await createNode({ + config: createBaseOptions({ + peerId: peerIds[0], + addresses: { + listen: ['/ip4/127.0.0.1/tcp/0/ws'] + }, + connectionManager: { + maxEventLoopDelay: 10000 + } + }) + }) + + expect(libp2p).to.have.nested.property('connectionManager.latencyMonitor') + }) + }) })