UNPKG

logs-interceptor

Version:

High-performance, production-ready log interceptor for Node.js applications with Loki integration. Built with Clean Architecture principles. Supports Node.js, Browser, and Node-RED.

61 lines 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CircuitBreakerService = void 0; class CircuitBreakerService { constructor(config) { this.config = config; this.state = 'closed'; this.failures = 0; this.successCount = 0; this.lastFailure = 0; this.nextAttempt = 0; } isOpen() { if (!this.config.enabled) return false; if (this.state === 'open') { if (Date.now() >= this.nextAttempt) { this.state = 'half-open'; this.successCount = 0; return false; } return true; } return false; } recordSuccess() { if (this.state === 'half-open') { this.successCount++; if (this.successCount >= this.config.halfOpenRequests) { this.state = 'closed'; this.failures = 0; this.successCount = 0; } } else if (this.state === 'closed') { this.failures = 0; } } recordFailure() { this.failures++; this.lastFailure = Date.now(); if (this.config.enabled) { if (this.failures >= this.config.failureThreshold) { this.state = 'open'; this.nextAttempt = Date.now() + this.config.resetTimeout; } } } getState() { return this.state; } reset() { this.state = 'closed'; this.failures = 0; this.successCount = 0; this.lastFailure = 0; this.nextAttempt = 0; } } exports.CircuitBreakerService = CircuitBreakerService; //# sourceMappingURL=CircuitBreakerService.js.map