@gyoridavid/circuit-breaker

1.0.3 • Public • Published

Circuit Breaker

Simple circuit breaker written in TypeScript. Port of gobreaker. No dependencies.

codecov jest

Install

    npm i -S @gyoridavid/circuit-breaker

Usage

    import CircuitBreaker from '@gyoridavid/circuit-breaker';

    const breaker = new CircuitBreaker({
        name: 'http-client breaker'
    });

    interface HTTPResponse {
        status: number,
        body: any
    }

    try {
        const response: HTTPResponse = breaker.execute<HTTPResponse>(async () => {
            // async call that could fail
            return { status: 200, body: { status: 'ok' } };
        });
    } catch(err: error) {
        // check the error type
        // if breaker is open it'll throw CBErrOpenState
        // if breaker is half-open and we exceeded the maxRequests it'll throw CBErrTooManyRequests
    }

Configuration

// the following configuration options are available
interface CBConfig {
    name: string;
    interval?: number; // cyclic period of the closed state for the circuit breaker to clear the internal counts. If interval is less than or equal to 0, the circuit breaker doesn't clear internal counts during the closed state.
    isSuccessful?: (err: unknown) => boolean; // to check if we consider the given call as a failure; the method receives either the error or the response data.
    maxRequests?: number; // the number of requests to try in the half-open state
    onStateChange?: (name: string, from: CBState, to: CBState) => void; // called whenever the internal state changes; add logging/stats collection
    readyToTrip?: (counts: CBCount) => boolean; // configure when we should put the circuit breaker to open state; the default behaviour is after 5 failed calls.
    timeout?: number; // for how long we should wait until we put the circuit breaker to half-open state and re-try the call.
}

Package Sidebar

Install

npm i @gyoridavid/circuit-breaker

Weekly Downloads

3

Version

1.0.3

License

Apache-2.0

Unpacked Size

26.2 kB

Total Files

9

Last publish

Collaborators

  • gyoridavid