mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-02 13:53:12 +00:00
fix: reduce emission of health events (#2554)
This commit is contained in:
parent
ada265731a
commit
de972d6694
@ -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)
|
||||
|
||||
@ -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,23 +102,27 @@ 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
|
||||
@ -125,3 +130,4 @@ export class HealthIndicator implements IHealthIndicator {
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user