UNPKG

rsxjs

Version:

Resilience Extensions for JS.

53 lines (52 loc) 1.5 kB
/** * @file src/breaker/types.ts * @copyright 2018-present Karim Alibhai. All rights reserved. */ import { Store } from '../store'; export interface BreakerOptions { name?: string; maxErrors: number; timeout: number; store: Store; } export declare type BreakerOptionsGiven = Partial<BreakerOptions>; export declare function defaults(config?: BreakerOptionsGiven): BreakerOptions; export declare const DefaultBreakerOptions: BreakerOptions; export interface BreakerStateObject { numErrors: number; lastError: string; lastErrorTime: number; } export declare const enum BreakerState { OPEN = 0, CLOSED = 1, HALFOPEN = 2 } interface StateCache { numErrors: number; lastErrorTime: number; state: BreakerState; } export declare class CircuitBreaker<T> { private readonly options; private readonly namespace; private readonly state; constructor(options: BreakerOptions); private lastState?; private lastStateTime?; lastStateIsFresh(): boolean; getFreshState(): Promise<StateCache>; getState(): Promise<StateCache>; shouldAllowRequest(): Promise<boolean>; attempt(fn: () => T | Promise<T>): Promise<T>; } export interface BreakerFunction<T> { (...args: any[]): Promise<T>; shouldAllowRequest(): Promise<boolean>; } export declare function getBreakerState({ numErrors, lastErrorTime, options, }: { numErrors: number; lastErrorTime: number; options: BreakerOptions; }): BreakerState; export {};