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.

117 lines (116 loc) 3.82 kB
/** * @module CircuitBreaker */ import { type HalfOpenTransitions, type CircuitBreakerTrackSettings, type CircuitBreakerTrackState, type ICircuitBreakerPolicy, type ClosedTransitions } from "../../../../circuit-breaker/contracts/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; /** * Configuration for the time-based sampling circuit breaker policy. * Tracks requests over a sliding time window and opens the circuit when the failure * percentage exceeds `failureThreshold`. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"` * @group Policies */ export type SamplingBreakerSettings = { /** * Percentage (from 0 to 1) failures before going from closed -> open. * * @default 0.2 */ failureThreshold?: number; /** * Percentage (from 0 to 1) successes before going from half-open -> closed. * * @default * ```ts * 1 - settings.failureThreshold * ``` */ successThreshold?: number; /** * Length of time over which to sample. * * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(1) * ``` */ timeSpan?: ITimeSpan; /** * The sample length time. * * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromTimeSpan(settings.timeSpan).divide(6) * ``` */ sampleTimeSpan?: ITimeSpan; /** * The minimum number of calls per seconds to go from closed -> open, half-opened -> closed or half-opened -> open. * * @default 5 */ minimumRps?: number; }; /** * @internal */ export declare function resolveSamplingBreakerSettings(settings: SamplingBreakerSettings): Required<SamplingBreakerSettings>; /** * @internal */ export type SerializedSamplingBreakerSettings = { failureThreshold?: number; successThreshold?: number; timeSpan?: number; sampleTimeSpan?: number; minimumRps?: number; }; /** * @internal */ export declare function serializeSamplingBreakerSettings(settings: SamplingBreakerSettings): Required<SerializedSamplingBreakerSettings>; /** * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"` * @group Policies */ export type Sample = { startedAt: number; failures: number; successes: number; }; /** * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"` * @group Policies */ export type SamplingBreakerState = { samples: Array<Sample>; }; /** * The `SamplingBreaker` breaks after a proportion of requests over a time period fail. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/policies"` * @group Policies */ export declare class SamplingBreaker implements ICircuitBreakerPolicy<SamplingBreakerState> { private readonly failureThreshold; private readonly successThreshold; private readonly timeSpan; private readonly sampleTimeSpan; private readonly minimumRps; constructor(settings?: SamplingBreakerSettings); initialMetrics(): SamplingBreakerState; private static getProccesedMetricData; private isMiniumNotMet; whenClosed(currentMetrics: SamplingBreakerState, _currentDate: Date): ClosedTransitions; whenHalfOpened(currentMetrics: SamplingBreakerState, _currentDate: Date): HalfOpenTransitions; private isNotOverlapping; private track; trackFailure(currentState: CircuitBreakerTrackState<SamplingBreakerState>, settings: CircuitBreakerTrackSettings<SamplingBreakerState>): SamplingBreakerState; trackSuccess(currentState: CircuitBreakerTrackState<SamplingBreakerState>, settings: CircuitBreakerTrackSettings<SamplingBreakerState>): SamplingBreakerState; isEqual(metricsA: SamplingBreakerState, metricsB: SamplingBreakerState): boolean; }