fix: reduce emission of health events (#2554)

This commit is contained in:
fryorcraken 2025-08-13 23:42:22 +10:00 committed by GitHub
parent ada265731a
commit de972d6694
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 15 deletions

View File

@ -28,6 +28,10 @@ describe("HealthIndicator", () => {
});
it("should transition to Unhealthy when no connections", async () => {
// Only track transition, starting as healthy
(healthIndicator as any).value = HealthStatus.SufficientlyHealthy;
// Start monitoring
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
resolve(e.detail)

View File

@ -72,10 +72,11 @@ export class HealthIndicator implements IHealthIndicator {
log.info("onPeerDisconnected: has connections, ignoring");
}
this.value = HealthStatus.Unhealthy;
log.info(`onPeerDisconnected: node identified as ${this.value}`);
log.info(
`onPeerDisconnected: node identified as ${HealthStatus.Unhealthy}`
);
this.dispatchHealthEvent();
this.updateAndDispatchHealthEvent(HealthStatus.Unhealthy);
}
private async onPeerIdentify(
@ -101,27 +102,32 @@ export class HealthIndicator implements IHealthIndicator {
p?.protocols.includes(LightPushCodec)
).length;
let newValue;
if (filterPeers === 0 || lightPushPeers === 0) {
this.value = HealthStatus.Unhealthy;
newValue = HealthStatus.Unhealthy;
} else if (filterPeers >= 2 && lightPushPeers >= 2) {
this.value = HealthStatus.SufficientlyHealthy;
newValue = HealthStatus.SufficientlyHealthy;
} else if (filterPeers === 1 && lightPushPeers === 1) {
this.value = HealthStatus.MinimallyHealthy;
newValue = HealthStatus.MinimallyHealthy;
} else {
log.error(
`onPeerChange: unexpected state, cannot identify health status of the node: Filter:${filterPeers}; LightPush:${lightPushPeers}`
`onPeerIdentify: unexpected state, cannot identify health status of the node: Filter:${filterPeers}; LightPush:${lightPushPeers}`
);
newValue = this.value;
}
log.info(`onPeerChange: node identified as ${this.value}`);
this.dispatchHealthEvent();
log.info(`onPeerIdentify: node identified as ${newValue}`);
this.updateAndDispatchHealthEvent(newValue);
}
private dispatchHealthEvent(): void {
private updateAndDispatchHealthEvent(newValue: HealthStatus): void {
if (this.value !== newValue) {
this.value = newValue;
this.events.dispatchEvent(
new CustomEvent<HealthStatus>("waku:health", {
detail: this.value
})
);
}
}
}