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 () => { 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) => { const statusChangePromise = new Promise<HealthStatus>((resolve) => {
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) => events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
resolve(e.detail) resolve(e.detail)

View File

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