Skip to content

Commit ae340d5

Browse files
use Infinity instead of 0 to check already dialed
1 parent c7b1523 commit ae340d5

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/core/src/lib/connection_manager.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class ConnectionManager
3838
private dialAttemptsForPeer: Map<string, number> = new Map();
3939
private dialErrorsForPeer: Map<string, any> = new Map();
4040

41-
private currentActiveDialCount = 0;
41+
private currentActiveParallelDialCount = 0;
4242
private pendingPeerDialQueue: Array<PeerId> = [];
4343

4444
public static create(
@@ -181,7 +181,7 @@ export class ConnectionManager
181181
}
182182

183183
private async dialPeer(peerId: PeerId): Promise<void> {
184-
this.currentActiveDialCount += 1;
184+
this.currentActiveParallelDialCount += 1;
185185
let dialAttempt = 0;
186186
while (dialAttempt < this.options.maxDialAttemptsForPeer) {
187187
try {
@@ -195,9 +195,9 @@ export class ConnectionManager
195195
conn.tags = Array.from(new Set([...conn.tags, ...tags]));
196196
});
197197

198-
// instead of deleting the peer from the peer store, we set the dial attempt to 0
198+
// instead of deleting the peer from the peer store, we set the dial attempt to Infinity
199199
// this helps us keep track of peers that have been dialed before
200-
this.dialAttemptsForPeer.set(peerId.toString(), 0);
200+
this.dialAttemptsForPeer.set(peerId.toString(), Infinity);
201201

202202
// Dialing succeeded, break the loop
203203
break;
@@ -221,7 +221,7 @@ export class ConnectionManager
221221
}
222222

223223
// Always decrease the active dial count and process the dial queue
224-
this.currentActiveDialCount--;
224+
this.currentActiveParallelDialCount--;
225225
this.processDialQueue();
226226

227227
// If max dial attempts reached and dialing failed, delete the peer
@@ -273,7 +273,7 @@ export class ConnectionManager
273273
private processDialQueue(): void {
274274
if (
275275
this.pendingPeerDialQueue.length > 0 &&
276-
this.currentActiveDialCount < this.options.maxParallelDials
276+
this.currentActiveParallelDialCount < this.options.maxParallelDials
277277
) {
278278
const peerId = this.pendingPeerDialQueue.shift();
279279
if (!peerId) return;
@@ -319,7 +319,7 @@ export class ConnectionManager
319319
private async attemptDial(peerId: PeerId): Promise<void> {
320320
if (!(await this.shouldDialPeer(peerId))) return;
321321

322-
if (this.currentActiveDialCount >= this.options.maxParallelDials) {
322+
if (this.currentActiveParallelDialCount >= this.options.maxParallelDials) {
323323
this.pendingPeerDialQueue.push(peerId);
324324
return;
325325
}
@@ -438,7 +438,7 @@ export class ConnectionManager
438438
// If the peer is already in the peer store or has an active dial attempt, don't dial it
439439
if (
440440
this.dialAttemptsForPeer.has(peerId.toString()) &&
441-
this.dialAttemptsForPeer.get(peerId.toString()) === 0
441+
this.dialAttemptsForPeer.get(peerId.toString()) === Infinity
442442
) {
443443
log(
444444
`Peer ${peerId.toString()} has already been attempted dial before, or already has a dial attempt in progress, skipping dial`

0 commit comments

Comments
 (0)