Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

1.1 KB
Raw Blame
/**
 * Lazy per-backend debounce (backfill/stats/notifications spec): the first
 * failure notifies immediately; later failures accumulate and the next one
 * past the window rides out as a summary carrying the suppressed count. No
 * timers — the summary is sent on the next failure, which is the only moment
 * anyone would care. Process-local, like the health ring itself.
 */
export class ReplicaFailureDebouncer {
  private readonly windows = new Map<string, { windowStart: number; suppressed: number }>();

  constructor(
    private readonly windowMs: number,
    private readonly now: () => number = Date.now,
  ) {}

  /** null = suppress; otherwise the suppressed-count to report with this send. */
  admit(backend: string): number | null {
    const now = this.now();
    const window = this.windows.get(backend);
    if (!window || now - window.windowStart > this.windowMs) {
      const suppressed = window?.suppressed ?? 0;
      this.windows.set(backend, { windowStart: now, suppressed: 0 });
      return suppressed;
    }
    window.suppressed += 1;
    return null;
  }
}