UNPKG

optivise

Version:

Optivise - The Ultimate Optimizely Development Assistant with AI-powered features, zero-config setup, and comprehensive development support

32 lines 993 B
export class CircuitBreaker { failureThreshold; cooldownMs; consecutiveFailures = 0; openUntil = 0; constructor(cfg) { this.failureThreshold = cfg?.failureThreshold ?? 3; this.cooldownMs = cfg?.cooldownMs ?? 30_000; } state(now = Date.now()) { if (this.openUntil > now) return 'open'; if (this.openUntil !== 0 && this.openUntil <= now && this.consecutiveFailures >= this.failureThreshold) return 'half-open'; return 'closed'; } canAttempt(now = Date.now()) { const s = this.state(now); return s === 'closed' || s === 'half-open'; } onSuccess() { this.consecutiveFailures = 0; this.openUntil = 0; } onFailure(now = Date.now()) { this.consecutiveFailures += 1; if (this.consecutiveFailures >= this.failureThreshold) { this.openUntil = now + this.cooldownMs; } } } //# sourceMappingURL=circuit-breaker.js.map