chore: improve logs & add debug

This commit is contained in:
Danish Arora 2024-09-26 01:34:20 +05:30
parent aec9451486
commit 3647ec97ed
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 63 additions and 16 deletions

View File

@ -35,6 +35,9 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
const maintainPeersInterval = const maintainPeersInterval =
options?.maintainPeersInterval ?? DEFAULT_MAINTAIN_PEERS_INTERVAL; options?.maintainPeersInterval ?? DEFAULT_MAINTAIN_PEERS_INTERVAL;
this.log.info(
`Initializing BaseProtocolSDK with numPeersToUse: ${this.numPeersToUse}, maintainPeersInterval: ${maintainPeersInterval}ms`
);
// void this.setupEventListeners(); // void this.setupEventListeners();
void this.startMaintainPeersInterval(maintainPeersInterval); void this.startMaintainPeersInterval(maintainPeersInterval);
} }
@ -49,19 +52,26 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
* @returns The new peer that was found and connected to. * @returns The new peer that was found and connected to.
*/ */
public async renewPeer(peerToDisconnect: PeerId): Promise<Peer | undefined> { public async renewPeer(peerToDisconnect: PeerId): Promise<Peer | undefined> {
this.log.info(`Renewing peer ${peerToDisconnect}`); this.log.info(`Attempting to renew peer ${peerToDisconnect}`);
const success = await this.peerManager.disconnectPeer(peerToDisconnect); const success = await this.peerManager.disconnectPeer(peerToDisconnect);
if (!success) return undefined; if (!success) {
this.log.warn(`Failed to disconnect from peer ${peerToDisconnect}`);
return undefined;
}
this.log.debug(
`Successfully disconnected from peer ${peerToDisconnect}, searching for a new peer`
);
const newPeer = await this.peerManager.findAndAddPeers(1); const newPeer = await this.peerManager.findAndAddPeers(1);
if (newPeer.length === 0) { if (newPeer.length === 0) {
this.log.error( this.log.error(
"Failed to find a new peer to replace the disconnected one." "Failed to find a new peer to replace the disconnected one"
); );
return undefined; return undefined;
} }
this.log.info(`Successfully renewed peer. New peer: ${newPeer[0].id}`);
return newPeer[0]; return newPeer[0];
} }
@ -73,6 +83,8 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
clearInterval(this.maintainPeersIntervalId); clearInterval(this.maintainPeersIntervalId);
this.maintainPeersIntervalId = null; this.maintainPeersIntervalId = null;
this.log.info("Maintain peers interval stopped"); this.log.info("Maintain peers interval stopped");
} else {
this.log.debug("Maintain peers interval was not running");
} }
} }
@ -101,28 +113,47 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
): Promise<boolean> { ): Promise<boolean> {
const { forceUseAllPeers = false, maxAttempts = 3 } = options; const { forceUseAllPeers = false, maxAttempts = 3 } = options;
this.log.debug(
`Checking for peers. forceUseAllPeers: ${forceUseAllPeers}, maxAttempts: ${maxAttempts}`
);
if (!forceUseAllPeers && this.connectedPeers.length > 0) { 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; return true;
} }
if (!forceUseAllPeers) { if (!forceUseAllPeers) {
await this.maintainPeers(); await this.maintainPeers();
return this.connectedPeers.length > 0; 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++) { for (let attempts = 0; attempts < maxAttempts; attempts++) {
this.log.debug(
`Attempt ${attempts + 1}/${maxAttempts} to reach required number of peers`
);
await this.maintainPeers(); await this.maintainPeers();
if (this.connectedPeers.length >= this.numPeersToUse) { if (this.connectedPeers.length >= this.numPeersToUse) {
this.log.info(
`Required number of peers (${this.numPeersToUse}) reached`
);
return true; return true;
} }
this.log.warn( this.log.warn(
`Found only ${this.connectedPeers.length} peers, expected ${this.numPeersToUse}. Retrying...` `Found only ${this.connectedPeers.length}/${this.numPeersToUse} required peers. Retrying...`
); );
} }
this.log.error("Failed to find required number of peers"); this.log.error(
`Failed to find required number of peers (${this.numPeersToUse}) after ${maxAttempts} attempts`
);
return false; return false;
} }
@ -131,17 +162,18 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
* @param interval The interval in milliseconds to maintain the peers. * @param interval The interval in milliseconds to maintain the peers.
*/ */
private async startMaintainPeersInterval(interval: number): Promise<void> { private async startMaintainPeersInterval(interval: number): Promise<void> {
this.log.info("Starting maintain peers interval"); this.log.info(
`Starting maintain peers interval with ${interval}ms interval`
);
try { try {
// await this.maintainPeers(); // await this.maintainPeers();
this.maintainPeersIntervalId = setInterval(() => { this.maintainPeersIntervalId = setInterval(() => {
this.log.debug("Running scheduled peer maintenance");
this.maintainPeers().catch((error) => { this.maintainPeers().catch((error) => {
this.log.error("Error during maintain peers interval:", error); this.log.error("Error during scheduled peer maintenance:", error);
}); });
}, interval); }, interval);
this.log.info( this.log.info("Maintain peers interval started successfully");
`Maintain peers interval started with interval ${interval}ms`
);
} catch (error) { } catch (error) {
this.log.error("Error starting maintain peers interval:", error); this.log.error("Error starting maintain peers interval:", error);
throw error; throw error;
@ -156,22 +188,28 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
const currentPeerCount = await this.peerManager.getPeerCount(); const currentPeerCount = await this.peerManager.getPeerCount();
const numPeersToAdd = this.numPeersToUse - currentPeerCount; const numPeersToAdd = this.numPeersToUse - currentPeerCount;
this.log.debug(
`Current peer count: ${currentPeerCount}, target: ${this.numPeersToUse}`
);
if (numPeersToAdd === 0) { if (numPeersToAdd === 0) {
this.log.info("No maintenance required, peer count is sufficient"); this.log.info("Peer count is at target, no maintenance required");
return; return;
} }
this.log.info(`Maintaining peers, current count: ${currentPeerCount}`);
if (numPeersToAdd > 0) { if (numPeersToAdd > 0) {
this.log.info(`Attempting to add ${numPeersToAdd} peer(s)`);
await this.peerManager.findAndAddPeers(numPeersToAdd); await this.peerManager.findAndAddPeers(numPeersToAdd);
} else { } else {
this.log.info(
`Attempting to remove ${Math.abs(numPeersToAdd)} excess peer(s)`
);
await this.peerManager.removeExcessPeers(Math.abs(numPeersToAdd)); await this.peerManager.removeExcessPeers(Math.abs(numPeersToAdd));
} }
const finalPeerCount = await this.peerManager.getPeerCount(); const finalPeerCount = await this.peerManager.getPeerCount();
this.log.info( this.log.info(
`Peer maintenance completed, current count: ${finalPeerCount}` `Peer maintenance completed. Initial count: ${currentPeerCount}, Final count: ${finalPeerCount}`
); );
} catch (error) { } catch (error) {
this.log.error("Error during peer maintenance", { error }); this.log.error("Error during peer maintenance", { error });

View File

@ -3,6 +3,7 @@ import debug, { Debugger } from "debug";
const APP_NAME = "waku"; const APP_NAME = "waku";
export class Logger { export class Logger {
private _debug: Debugger;
private _info: Debugger; private _info: Debugger;
private _warn: Debugger; private _warn: Debugger;
private _error: Debugger; private _error: Debugger;
@ -12,11 +13,16 @@ export class Logger {
} }
public constructor(prefix?: string) { public constructor(prefix?: string) {
this._debug = debug(Logger.createDebugNamespace("debug", prefix));
this._info = debug(Logger.createDebugNamespace("info", prefix)); this._info = debug(Logger.createDebugNamespace("info", prefix));
this._warn = debug(Logger.createDebugNamespace("warn", prefix)); this._warn = debug(Logger.createDebugNamespace("warn", prefix));
this._error = debug(Logger.createDebugNamespace("error", prefix)); this._error = debug(Logger.createDebugNamespace("error", prefix));
} }
public get debug(): Debugger {
return this._debug;
}
public get info(): Debugger { public get info(): Debugger {
return this._info; return this._info;
} }
@ -29,7 +35,10 @@ export class Logger {
return this._error; return this._error;
} }
public log(level: "info" | "warn" | "error", ...args: unknown[]): void { public log(
level: "debug" | "info" | "warn" | "error",
...args: unknown[]
): void {
const logger = this[level] as (...args: unknown[]) => void; const logger = this[level] as (...args: unknown[]) => void;
logger(...args); logger(...args);
} }