Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
/** Periodic health-check poller for embedded services. */
import type { HealthState } from "./types";
const HEALTH_FETCH_TIMEOUT_MS = 5_000;
const FAILURE_THRESHOLD = 3;
export type OnHealthChange = (health: HealthState) => void;
export class HealthChecker {
private timer: ReturnType<typeof setInterval> | null = null;
private consecutiveFailures = 0;
private currentHealth: HealthState = "unknown";
private active = false;
constructor(
private readonly healthUrl: () => string,
private readonly intervalMs: number,
private readonly onChange: OnHealthChange
) {}
start(): void {
if (this.active) return;
this.active = true;
this.consecutiveFailures = 0;
this.currentHealth = "unknown";
this.timer = setInterval(() => void this.poll(), this.intervalMs);
// Don't keep the process alive solely for the health poller.
(this.timer as { unref?: () => void })?.unref?.();
}
stop(): void {
this.active = false;
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
this.setHealth("unknown");
}
getHealth(): HealthState {
return this.currentHealth;
}
private async poll(): Promise<void> {
if (!this.active) return;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), HEALTH_FETCH_TIMEOUT_MS);
try {
const res = await fetch(this.healthUrl(), { signal: controller.signal });
clearTimeout(timeout);
if (res.ok) {
this.consecutiveFailures = 0;
this.setHealth("healthy");
} else {
this.recordFailure();
}
} catch {
clearTimeout(timeout);
this.recordFailure();
}
}
private recordFailure(): void {
this.consecutiveFailures++;
if (this.consecutiveFailures >= FAILURE_THRESHOLD) {
this.setHealth("unhealthy");
}
}
private setHealth(health: HealthState): void {
if (health === this.currentHealth) return;
this.currentHealth = health;
this.onChange(health);
}
}