mollitia
Version:
JavaScript Resilience Library
189 lines (187 loc) • 6.53 kB
TypeScript
import { Module, ModuleOptions } from '../index.js';
import { Circuit, CircuitFunction } from '../../circuit.js';
import { SerializableRecord } from '../../helpers/serializable.js';
type ErrorCallback = (err: any) => boolean;
type BreakerResultResponse = {
requestResult: SlidingWindowRequestResult;
response: any;
shouldReportFailure: boolean;
};
/**
* Returned when a breaker module is in open state.
* @param message Circuit is opened
*/
export declare class BreakerError extends Error {
constructor();
}
/**
* Returned when a breaker module is in half-open state and the maximum number of requests in half-open has been sent.
* @param message Max allowed requests reached
*/
export declare class BreakerMaxAllowedRequestError extends Error {
constructor();
}
/**
* Breaker states.
*/
export declare enum BreakerState {
CLOSED = "closed",
HALF_OPENED = "half-opened",
OPENED = "opened"
}
export interface SlidingWindowBreakerState extends SerializableRecord {
requests: SlidingRequest[];
state: {
state: BreakerState;
timestamp: number;
};
}
/**
* Properties that customizes the sliding window breaker behavior.
*/
export declare abstract class SlidingWindowBreakerOptions extends ModuleOptions {
/**
* Specifies the circuit state
*/
state?: BreakerState;
/**
* Specifies the time (in ms) the circuit stay opened before switching to half-open
*/
openStateDelay?: number;
/**
* Specifies the maximum wait (in ms) in Half Open State, before switching back to open. 0 deactivates this
*/
halfOpenStateMaxDelay?: number;
/**
* Specifies the maximum number of calls (if count breaker is user),
* or the sliding duration (in ms, if time breaker is used) used to calculate failure and slow call rate percentages
*/
slidingWindowSize?: number;
/**
* Specifies the minimum number of calls used to calculate failure and slow call rate percentages
*/
minimumNumberOfCalls?: number;
/**
* Specifies the failure rate threshold in percentage
*/
failureRateThreshold?: number;
/**
* Specifies the slow call rate threshold. A call is considered as slow when the call duration is greater than slowCallDurationThreshold
*/
slowCallRateThreshold?: number;
/**
* Specifies the duration (in ms) threshold above which calls are considered as slow
*/
slowCallDurationThreshold?: number;
/**
* Specifies the number of permitted calls when the circuit is half open
*/
permittedNumberOfCallsInHalfOpenState?: number;
/**
* Allows filtering of the error to report as a failure or not.
*/
onError?: ErrorCallback;
}
export declare enum SlidingWindowRequestResult {
SUCCESS = 0,
FAILURE = 1,
TIMEOUT = 2
}
export interface SlidingRequest extends SerializableRecord {
result: SlidingWindowRequestResult;
timestamp?: number;
}
export interface SlidingState extends SerializableRecord {
state: BreakerState;
timestamp: number;
}
export declare abstract class SlidingWindowBreaker extends Module {
/**
* Specifies the circuit state
*/
state: BreakerState;
/**
* Specifies when the circuit state was set
*/
stateTimestamp: number;
/**
* Specifies the time (in ms) the circuit stay opened before switching to half-open
*/
openStateDelay: number;
/**
* Specifies the maximum wait (in ms) in Half Open State, before switching back to open. 0 deactivates this
*/
halfOpenStateMaxDelay: number;
/**
* Specifies the maximum number of calls (if count breaker is user),
* or the sliding duration (in ms, if time breaker is used) used to calculate failure and slow call rate percentages
*/
slidingWindowSize: number;
/**
* Specifies the minimum number of calls used to calculate failure and slow call rate percentages
*/
minimumNumberOfCalls: number;
/**
* Specifies the failure rate threshold in percentage
*/
failureRateThreshold: number;
/**
* Specifies the slow call rate threshold. A call is considered as slow when the call duration is greater than slowCallDurationThreshold
*/
slowCallRateThreshold: number;
/**
* Specifies the duration (in ms) threshold above which calls are considered as slow
*/
slowCallDurationThreshold: number;
/**
* Specifies the number of permitted calls when the circuit is half open
*/
permittedNumberOfCallsInHalfOpenState: number;
/**
* Allows filtering of the error to report as a failure or not.
*/
onError: ErrorCallback;
private halfOpenMaxDelayTimeout;
private openTimeout;
private nbRequestsInHalfOpenedState;
protected requests: SlidingRequest[];
private isInitialized;
constructor(options?: SlidingWindowBreakerOptions);
private reinitializeCounters;
onOpened(): void;
onClosed(): void;
onHalfOpened(): void;
private isSomeEnum;
private isValidTimestamp;
private isValidState;
private isValidRequest;
private isValidData;
execute<T>(circuit: Circuit, promise: CircuitFunction, ...params: any[]): Promise<T>;
private _promiseBreaker;
abstract executeInClosed<T>(promise: CircuitFunction, ...params: any[]): Promise<T>;
protected adjustRequestResult(requestResult: SlidingWindowRequestResult, shouldReportFailure: boolean): SlidingWindowRequestResult;
protected setStateSecure(state: SerializableRecord[], ttl?: number): Promise<void>;
protected executeInHalfOpened<T>(promise: CircuitFunction, ...params: any[]): Promise<T>;
protected executePromise(promise: CircuitFunction, ...params: any[]): Promise<BreakerResultResponse>;
protected checkCallRatesHalfOpen(callbackFailure: (() => void), callbackSuccess?: (() => void)): void;
private checkResult;
protected getNbSlowAndFailure(acc: {
nbSlow: number;
nbFailure: number;
}, current: SlidingRequest): {
nbSlow: number;
nbFailure: number;
};
protected checkCallRatesClosed(): boolean;
open(): Promise<void>;
halfOpen(): Promise<void>;
close(): Promise<void>;
private setHalfDelay;
private setOpenDelay;
private clearHalfOpenTimeout;
dispose(): void;
getState(): Promise<SlidingWindowBreakerState>;
setState(state: SerializableRecord[], ttl?: number): Promise<void>;
clearState(): Promise<void>;
}
export {};