feat: add debounce to health indicator (#2594)

This commit is contained in:
Sasha 2025-09-04 00:49:10 +02:00 committed by GitHub
parent 2fba052b8b
commit a7f30b1211
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 12 deletions

14
package-lock.json generated
View File

@ -7536,9 +7536,17 @@
"version": "4.17.18", "version": "4.17.18",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.18.tgz", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.18.tgz",
"integrity": "sha512-KJ65INaxqxmU6EoCiJmRPZC9H9RVWCRd349tXM2M3O5NA7cY6YL7c0bHAHQ93NOfTObEQ004kd2QVHs/r0+m4g==", "integrity": "sha512-KJ65INaxqxmU6EoCiJmRPZC9H9RVWCRd349tXM2M3O5NA7cY6YL7c0bHAHQ93NOfTObEQ004kd2QVHs/r0+m4g==",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/lodash.debounce": {
"version": "4.0.9",
"resolved": "https://registry.npmjs.org/@types/lodash.debounce/-/lodash.debounce-4.0.9.tgz",
"integrity": "sha512-Ma5JcgTREwpLRwMM+XwBR7DaWe96nC38uCBDFKZWbNKD+osjVzdpnUSwBcqCptrp16sSOLBAUb50Car5I0TCsQ==",
"license": "MIT",
"dependencies": {
"@types/lodash": "*"
}
},
"node_modules/@types/markdown-it": { "node_modules/@types/markdown-it": {
"version": "14.1.2", "version": "14.1.2",
"resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-14.1.2.tgz", "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-14.1.2.tgz",
@ -37608,12 +37616,14 @@
"@libp2p/ping": "2.0.35", "@libp2p/ping": "2.0.35",
"@libp2p/websockets": "9.2.16", "@libp2p/websockets": "9.2.16",
"@noble/hashes": "^1.3.3", "@noble/hashes": "^1.3.3",
"@types/lodash.debounce": "^4.0.9",
"@waku/core": "0.0.38", "@waku/core": "0.0.38",
"@waku/discovery": "0.0.11", "@waku/discovery": "0.0.11",
"@waku/interfaces": "0.0.33", "@waku/interfaces": "0.0.33",
"@waku/proto": "^0.0.13", "@waku/proto": "^0.0.13",
"@waku/utils": "0.0.26", "@waku/utils": "0.0.26",
"libp2p": "2.8.11" "libp2p": "2.8.11",
"lodash.debounce": "^4.0.8"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/interface": "2.10.4", "@libp2p/interface": "2.10.4",

View File

@ -67,20 +67,22 @@
"@libp2p/ping": "2.0.35", "@libp2p/ping": "2.0.35",
"@libp2p/websockets": "9.2.16", "@libp2p/websockets": "9.2.16",
"@noble/hashes": "^1.3.3", "@noble/hashes": "^1.3.3",
"@types/lodash.debounce": "^4.0.9",
"@waku/core": "0.0.38", "@waku/core": "0.0.38",
"@waku/discovery": "0.0.11", "@waku/discovery": "0.0.11",
"@waku/interfaces": "0.0.33", "@waku/interfaces": "0.0.33",
"@waku/proto": "^0.0.13", "@waku/proto": "^0.0.13",
"@waku/utils": "0.0.26", "@waku/utils": "0.0.26",
"libp2p": "2.8.11" "libp2p": "2.8.11",
"lodash.debounce": "^4.0.8"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/interface": "2.10.4", "@libp2p/interface": "2.10.4",
"@types/chai": "^4.3.11",
"@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0", "@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5", "@rollup/plugin-replace": "^5.0.5",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.9", "@types/mocha": "^10.0.9",
"@waku/build-utils": "*", "@waku/build-utils": "*",
"chai": "^5.1.1", "chai": "^5.1.1",
@ -104,4 +106,4 @@
"LICENSE", "LICENSE",
"README.md" "README.md"
] ]
} }

View File

@ -7,6 +7,7 @@ import {
WakuEvent WakuEvent
} from "@waku/interfaces"; } from "@waku/interfaces";
import { Logger } from "@waku/utils"; import { Logger } from "@waku/utils";
import debounce from "lodash.debounce";
type PeerEvent<T> = (_event: CustomEvent<T>) => void; type PeerEvent<T> = (_event: CustomEvent<T>) => void;
@ -24,10 +25,13 @@ interface IHealthIndicator {
} }
export class HealthIndicator implements IHealthIndicator { export class HealthIndicator implements IHealthIndicator {
private isStarted = false;
private readonly libp2p: Libp2p; private readonly libp2p: Libp2p;
private readonly events: IWakuEventEmitter; private readonly events: IWakuEventEmitter;
private value: HealthStatus = HealthStatus.Unhealthy; private value: HealthStatus = HealthStatus.Unhealthy;
private readonly debouncedAssessHealth: ReturnType<typeof debounce>;
public constructor(params: HealthIndicatorParams) { public constructor(params: HealthIndicatorParams) {
this.libp2p = params.libp2p; this.libp2p = params.libp2p;
@ -35,9 +39,18 @@ export class HealthIndicator implements IHealthIndicator {
this.onPeerIdentify = this.onPeerIdentify.bind(this); this.onPeerIdentify = this.onPeerIdentify.bind(this);
this.onPeerDisconnected = this.onPeerDisconnected.bind(this); this.onPeerDisconnected = this.onPeerDisconnected.bind(this);
this.debouncedAssessHealth = debounce(() => {
void this.assessHealth();
}, 100);
} }
public start(): void { public start(): void {
if (this.isStarted) {
return;
}
this.isStarted = true;
log.info("start: adding listeners to libp2p"); log.info("start: adding listeners to libp2p");
this.libp2p.addEventListener( this.libp2p.addEventListener(
@ -49,10 +62,15 @@ export class HealthIndicator implements IHealthIndicator {
this.onPeerDisconnected as PeerEvent<PeerId> this.onPeerDisconnected as PeerEvent<PeerId>
); );
void this.assessHealth(); this.debouncedAssessHealth();
} }
public stop(): void { public stop(): void {
if (!this.isStarted) {
return;
}
this.isStarted = false;
log.info("stop: removing listeners to libp2p"); log.info("stop: removing listeners to libp2p");
this.libp2p.removeEventListener( this.libp2p.removeEventListener(
@ -63,22 +81,22 @@ export class HealthIndicator implements IHealthIndicator {
"peer:disconnect", "peer:disconnect",
this.onPeerDisconnected as PeerEvent<PeerId> this.onPeerDisconnected as PeerEvent<PeerId>
); );
this.debouncedAssessHealth.cancel();
} }
public toValue(): HealthStatus { public toValue(): HealthStatus {
return this.value; return this.value;
} }
private async onPeerDisconnected(_event: CustomEvent<PeerId>): Promise<void> { private onPeerDisconnected(_event: CustomEvent<PeerId>): void {
log.info(`onPeerDisconnected: received libp2p event`); log.info(`onPeerDisconnected: received libp2p event`);
await this.assessHealth(); this.debouncedAssessHealth();
} }
private async onPeerIdentify( private onPeerIdentify(_event: CustomEvent<IdentifyResult>): void {
_event: CustomEvent<IdentifyResult>
): Promise<void> {
log.info(`onPeerIdentify: received libp2p event`); log.info(`onPeerIdentify: received libp2p event`);
await this.assessHealth(); this.debouncedAssessHealth();
} }
private async assessHealth(): Promise<void> { private async assessHealth(): Promise<void> {