Skip to content

Class: ManualBreaker

Package: @litert/concurrentImport path: @litert/concurrentSource: packages/partials/concurrent/src/Classes/ManualBreaker.tsExtends: EventEmitterImplements: IBreaker

A manually controlled circuit breaker. The breaker starts closed (allowing all calls) by default. Call open() to block all traffic and close() to resume it.


Constructor

ts
new ManualBreaker(
    closedByDefault?: boolean,
    errorCtor?: IConstructor<Error>
)
ParameterTypeDefaultDescription
closedByDefaultboolean?trueInitial state: true = closed (passing), false = open (blocking)
errorCtorIConstructor<Error>?E_BREAKER_OPENEDError class to throw when open

Methods

close()

ts
close(): void

Closes the breaker — subsequent calls to call() will pass through.


open()

ts
open(): void

Opens the breaker — subsequent calls to call() will throw.


isClosed()

ts
isClosed(): boolean

Returns true if the breaker is closed (passing).


isOpened()

ts
isOpened(): boolean

Returns true if the breaker is open (blocking).


call(cb)

ts
call<TFn extends ISimpleFn>(cb: TFn): ReturnType<TFn>

Calls cb if the breaker is closed; throws otherwise.

Throws: E_BREAKER_OPENED (or custom error) when the breaker is open.


wrap(cb)

ts
wrap<T extends ISimpleFn>(cb: T): T

Returns a wrapper function that passes calls through the breaker on every invocation.


Events

Event 'error'

The 'error' event is emitted when an internal error occurs that the breaker cannot handle by itself.

WARNING

To prevent unhandled exceptions that may crash the program, you MUST ALWAYS listen on the 'error' event.

ts
type IErrorEventCallback = (error: unknown) => void;

Example

ts
import { ManualBreaker } from '@litert/concurrent';

const breaker = new ManualBreaker();

function handleRequest() {
    try {
        breaker.call(() => processRequest());
    } catch {
        return serviceUnavailable();
    }
}

// During maintenance
breaker.open();
// Resume after maintenance
breaker.close();