mirror of https://github.com/waku-org/js-waku.git
fix: hasPeers management
This commit is contained in:
parent
2e7954019f
commit
be7a0b05ec
|
@ -35,10 +35,6 @@ export type NetworkConfig = StaticSharding | AutoSharding;
|
||||||
* Options for using LightPush and Filter
|
* Options for using LightPush and Filter
|
||||||
*/
|
*/
|
||||||
export type ProtocolUseOptions = {
|
export type ProtocolUseOptions = {
|
||||||
/**
|
|
||||||
* Optional flag to enable auto-retry with exponential backoff
|
|
||||||
*/
|
|
||||||
autoRetry?: boolean;
|
|
||||||
/**
|
/**
|
||||||
* Optional flag to force using all available peers
|
* Optional flag to force using all available peers
|
||||||
*/
|
*/
|
||||||
|
@ -47,14 +43,6 @@ export type ProtocolUseOptions = {
|
||||||
* Optional maximum number of attempts for exponential backoff
|
* Optional maximum number of attempts for exponential backoff
|
||||||
*/
|
*/
|
||||||
maxAttempts?: number;
|
maxAttempts?: number;
|
||||||
/**
|
|
||||||
* Optional initial delay in milliseconds for exponential backoff
|
|
||||||
*/
|
|
||||||
initialDelay?: number;
|
|
||||||
/**
|
|
||||||
* Optional maximum delay in milliseconds for exponential backoff
|
|
||||||
*/
|
|
||||||
maxDelay?: number;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProtocolCreateOptions = {
|
export type ProtocolCreateOptions = {
|
||||||
|
|
|
@ -88,49 +88,41 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if there are peers to send a message to.
|
* Checks if there are sufficient peers to send a message to.
|
||||||
* If `forceUseAllPeers` is `false` (default) and there are connected peers, returns `true`.
|
* If `forceUseAllPeers` is `false` (default), returns `true` if there are any connected peers.
|
||||||
* If `forceUseAllPeers` is `true` or there are no connected peers, tries to find new peers from the ConnectionManager.
|
* If `forceUseAllPeers` is `true`, attempts to connect to `numPeersToUse` peers.
|
||||||
* If `autoRetry` is `false`, returns `false` if no peers are found.
|
|
||||||
* If `autoRetry` is `true`, tries to find new peers from the ConnectionManager with exponential backoff.
|
|
||||||
* Returns `true` if peers are found, `false` otherwise.
|
|
||||||
* @param options Optional options object
|
* @param options Optional options object
|
||||||
* @param options.autoRetry Optional flag to enable auto-retry with exponential backoff (default: false)
|
* @param options.forceUseAllPeers Optional flag to force connecting to `numPeersToUse` peers (default: false)
|
||||||
* @param options.forceUseAllPeers Optional flag to force using all available peers (default: false)
|
* @param options.maxAttempts Optional maximum number of attempts to reach the required number of peers (default: 3)
|
||||||
* @param options.initialDelay Optional initial delay in milliseconds for exponential backoff (default: 10)
|
* @returns `true` if the required number of peers are connected, `false` otherwise
|
||||||
* @param options.maxAttempts Optional maximum number of attempts for exponential backoff (default: 3)
|
|
||||||
* @param options.maxDelay Optional maximum delay in milliseconds for exponential backoff (default: 100)
|
|
||||||
*/
|
*/
|
||||||
protected async hasPeers(
|
protected async hasPeers(
|
||||||
options: Partial<ProtocolUseOptions> = {}
|
options: Partial<ProtocolUseOptions> = {}
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const {
|
const { forceUseAllPeers = false, maxAttempts = 3 } = options;
|
||||||
autoRetry = false,
|
|
||||||
forceUseAllPeers = false,
|
|
||||||
maxAttempts = 3
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
if (!forceUseAllPeers && this.connectedPeers.length > 0) {
|
if (!forceUseAllPeers && this.connectedPeers.length > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attempts = 0; attempts < maxAttempts; attempts++) {
|
if (!forceUseAllPeers) {
|
||||||
const success = await this.maintainPeers();
|
await this.maintainPeers();
|
||||||
if (success) {
|
return this.connectedPeers.length > 0;
|
||||||
if (this.connectedPeers.length < this.numPeersToUse) {
|
|
||||||
this.log.warn(
|
|
||||||
`Found only ${this.connectedPeers.length} peers, expected ${this.numPeersToUse}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!autoRetry) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//TODO: handle autoRetry
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log.error("Failed to find peers to send message to");
|
for (let attempts = 0; attempts < maxAttempts; attempts++) {
|
||||||
|
await this.maintainPeers();
|
||||||
|
|
||||||
|
if (this.connectedPeers.length >= this.numPeersToUse) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log.warn(
|
||||||
|
`Found only ${this.connectedPeers.length} peers, expected ${this.numPeersToUse}. Retrying...`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log.error("Failed to find required number of peers");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,14 +151,14 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
|
||||||
/**
|
/**
|
||||||
* Maintains the peers list to `numPeersToUse`.
|
* Maintains the peers list to `numPeersToUse`.
|
||||||
*/
|
*/
|
||||||
private async maintainPeers(): Promise<boolean> {
|
private async maintainPeers(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const currentPeerCount = await this.peerManager.getPeerCount();
|
const currentPeerCount = await this.peerManager.getPeerCount();
|
||||||
const numPeersToAdd = this.numPeersToUse - currentPeerCount;
|
const numPeersToAdd = this.numPeersToUse - currentPeerCount;
|
||||||
|
|
||||||
if (numPeersToAdd === 0) {
|
if (numPeersToAdd === 0) {
|
||||||
this.log.info("No maintenance required, peer count is sufficient");
|
this.log.info("No maintenance required, peer count is sufficient");
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log.info(`Maintaining peers, current count: ${currentPeerCount}`);
|
this.log.info(`Maintaining peers, current count: ${currentPeerCount}`);
|
||||||
|
@ -181,10 +173,8 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
|
||||||
this.log.info(
|
this.log.info(
|
||||||
`Peer maintenance completed, current count: ${finalPeerCount}`
|
`Peer maintenance completed, current count: ${finalPeerCount}`
|
||||||
);
|
);
|
||||||
return finalPeerCount >= this.numPeersToUse;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.log.error("Error during peer maintenance", { error });
|
this.log.error("Error during peer maintenance", { error });
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue