chore: improve hasPeers()

This commit is contained in:
Danish Arora 2024-10-08 11:59:12 +05:30
parent 874d835117
commit b9bfaa27c3
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E

View File

@ -100,28 +100,19 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
`Checking for peers. forceUseAllPeers: ${forceUseAllPeers}, maxAttempts: ${maxAttempts}`
);
if (!forceUseAllPeers && this.connectedPeers.length > 0) {
this.log.debug(
`At least one peer connected (${this.connectedPeers.length}), not forcing use of all peers`
);
return true;
}
if (!forceUseAllPeers) {
await this.maintainPeers();
const hasPeers = this.connectedPeers.length > 0;
this.log.debug(
`After maintenance, connected peers: ${this.connectedPeers.length}`
);
return hasPeers;
}
for (let attempts = 0; attempts < maxAttempts; attempts++) {
this.log.debug(
`Attempt ${attempts + 1}/${maxAttempts} to reach required number of peers`
);
await this.maintainPeers();
if (!forceUseAllPeers && this.connectedPeers.length > 0) {
this.log.debug(
`At least one peer connected (${this.connectedPeers.length}), not forcing use of all peers`
);
return true;
}
if (this.connectedPeers.length >= this.numPeersToUse) {
this.log.info(
`Required number of peers (${this.numPeersToUse}) reached`