@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.
58 lines (57 loc) • 2.44 kB
TypeScript
/**
* @module CircuitBreaker
*/
import { type HalfOpenTransitions, type CircuitBreakerTrackSettings, type CircuitBreakerTrackState, type ICircuitBreakerPolicy, type ClosedTransitions } from "../../../../circuit-breaker/contracts/_module.js";
/**
* Tracks in-memory failure and success counts for the consecutive circuit breaker.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"`
* @group Policies
*/
export type ConsecutiveBreakerState = {
failureCount: number;
successCount: number;
};
/**
* Configuration for the consecutive-failures circuit breaker policy.
* Opens the circuit when `failureThreshold` consecutive failures occur,
* and closes it again after `successThreshold` consecutive successes in half-open state.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"`
* @group Policies
*/
export type ConsecutiveBreakerSettings = {
/**
* Amount of consecutive failures before going from closed -> open.
*
* @default 5
*/
failureThreshold?: number;
/**
* Amount of consecutive success before going from half-open -> closed.
*
* @default settings.failureThreshold
*/
successThreshold?: number;
};
/**
* @internal
*/
export declare function resolveConsecutiveBreakerSettings(settings: ConsecutiveBreakerSettings): Required<ConsecutiveBreakerSettings>;
/**
* The `ConsecutiveBreaker` breaks after n requests in a row fail.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"`
* @group Policies
*/
export declare class ConsecutiveBreaker implements ICircuitBreakerPolicy<ConsecutiveBreakerState> {
private readonly failureThreshold;
private readonly successThreshold;
constructor(settings?: ConsecutiveBreakerSettings);
initialMetrics(): ConsecutiveBreakerState;
whenClosed(currentMetrics: ConsecutiveBreakerState, _currentDate: Date): ClosedTransitions;
whenHalfOpened(currentMetrics: ConsecutiveBreakerState, _currentDate: Date): HalfOpenTransitions;
trackFailure(currentState: CircuitBreakerTrackState<ConsecutiveBreakerState>, _settings: CircuitBreakerTrackSettings<ConsecutiveBreakerState>): ConsecutiveBreakerState;
trackSuccess(currentState: CircuitBreakerTrackState<ConsecutiveBreakerState>, settings: CircuitBreakerTrackSettings<ConsecutiveBreakerState>): ConsecutiveBreakerState;
isEqual(metricsA: ConsecutiveBreakerState, metricsB: ConsecutiveBreakerState): boolean;
}