*Call updateAvailable in loop

*Check available length and abort and return if empty
This commit is contained in:
Derrick Hammer 2022-07-26 23:28:12 -04:00
parent 00b0fe29f3
commit cb139131e1
1 changed files with 5 additions and 3 deletions

View File

@ -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());
}