Skip to content

Commit

Permalink
chore: improve hasPeers()
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Oct 9, 2024
1 parent 874d835 commit b9bfaa2
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions packages/sdk/src/protocols/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit b9bfaa2

Please sign in to comment.