UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

57 lines 2.44 kB
/** * @module CircuitBreaker */ import {} from "../../../../backoff-policies/_module.js"; import { CIRCUIT_BREAKER_STATE } from "../../../../circuit-breaker/contracts/_module.js"; import {} from "../../../../circuit-breaker/implementations/adapters/database-circuit-breaker-adapter/circuit-breaker-policy.js"; import {} from "../../../../circuit-breaker/implementations/adapters/database-circuit-breaker-adapter/types.js"; /** * @internal */ export class CircuitBreakerStateManager { circuitBreakerPolicy; backoffPolicy; constructor(circuitBreakerPolicy, backoffPolicy) { this.circuitBreakerPolicy = circuitBreakerPolicy; this.backoffPolicy = backoffPolicy; } updateState = (currentState, currentDate) => { if (currentState.type === CIRCUIT_BREAKER_STATE.CLOSED) { return this.circuitBreakerPolicy.whenClosed(currentState, currentDate); } if (currentState.type === CIRCUIT_BREAKER_STATE.OPEN) { return this.circuitBreakerPolicy.whenOpened(currentState, { currentDate: currentDate, backoffPolicy: this.backoffPolicy, }); } if (currentState.type === CIRCUIT_BREAKER_STATE.ISOLATED) { return currentState; } return this.circuitBreakerPolicy.whenHalfOpened(currentState, currentDate); }; trackFailure = (currentState, currentDate) => { if (currentState.type === CIRCUIT_BREAKER_STATE.CLOSED) { return this.circuitBreakerPolicy.trackFailureWhenClosed(currentState, currentDate); } if (currentState.type === CIRCUIT_BREAKER_STATE.HALF_OPEN) { return this.circuitBreakerPolicy.trackFailureWhenHalfOpened(currentState, currentDate); } return currentState; }; trackSuccess = (currentState, currentDate) => { if (currentState.type === CIRCUIT_BREAKER_STATE.CLOSED) { return this.circuitBreakerPolicy.trackSuccessWhenClosed(currentState, currentDate); } if (currentState.type === CIRCUIT_BREAKER_STATE.HALF_OPEN) { return this.circuitBreakerPolicy.trackSuccessWhenHalfOpened(currentState, currentDate); } return currentState; }; isolate = (_currentState, _currentDate) => { return { type: CIRCUIT_BREAKER_STATE.ISOLATED, }; }; } //# sourceMappingURL=circuit-breaker-state-manager.js.map