From cb139131e16d3372f9b293be899d2a5a4e6c2414 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 26 Jul 2022 23:28:12 -0400 Subject: [PATCH] *Call updateAvailable in loop *Check available length and abort and return if empty --- src/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 255fcf4..0b1314d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,13 +133,14 @@ export default class DHT { updateAvailable(); let relayPromises = []; - if (0 === available.length) { - return; - } while ( this._activeRelays.size <= Math.min(this._maxConnections, available.length) ) { + if (0 === available.length) { + break; + } + const relayIndex = await randomNumber(0, available.length - 1); const connection = this._relays.get(available[relayIndex]) as string; @@ -153,6 +154,7 @@ export default class DHT { this._options ); this._activeRelays.set(available[relayIndex], node); + updateAvailable(); relayPromises.push(node.ready()); }