From 2da0e441c50a722749cc19d74ddf671b21d30c16 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Tue, 30 Apr 2024 17:21:40 +0530 Subject: [PATCH] chore: some cleaning --- packages/core/src/lib/stream_manager.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/stream_manager.ts b/packages/core/src/lib/stream_manager.ts index 0ade66d153..4497ecc5c8 100644 --- a/packages/core/src/lib/stream_manager.ts +++ b/packages/core/src/lib/stream_manager.ts @@ -9,7 +9,7 @@ const RETRY_BACKOFF_BASE = 1_000; const MAX_RETRIES = 3; export class StreamManager { - private streamPool: Map>; + private readonly streamPool: Map>; private readonly log: Logger; constructor( @@ -18,12 +18,10 @@ export class StreamManager { public addEventListener: Libp2p["addEventListener"] ) { this.log = new Logger(`stream-manager:${multicodec}`); - this.addEventListener( - "peer:update", - this.handlePeerUpdateStreamPool.bind(this) - ); - this.getStream = this.getStream.bind(this); this.streamPool = new Map(); + + this.addEventListener("peer:update", this.handlePeerUpdateStreamPool); + this.getStream = this.getStream.bind(this); } public async getStream(peer: Peer): Promise { @@ -70,16 +68,14 @@ export class StreamManager { } try { - const stream = await connection.newStream(this.multicodec); - return stream; + return await connection.newStream(this.multicodec); } catch (error) { if (retries < MAX_RETRIES) { const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries); await new Promise((resolve) => setTimeout(resolve, backoff)); return this.newStream(peer, retries + 1); - } else { - throw error; } + throw error; } } @@ -105,8 +101,10 @@ export class StreamManager { private handlePeerUpdateStreamPool = (evt: CustomEvent): void => { const { peer } = evt.detail; + if (peer.protocols.includes(this.multicodec)) { const status = this.getConnectionStatus(peer.id); + if (status === "connected") { this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`); this.prepareNewStream(peer);