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
new ManualBreaker(
closedByDefault?: boolean,
errorCtor?: IConstructor<Error>
)| Parameter | Type | Default | Description |
|---|---|---|---|
closedByDefault | boolean? | true | Initial state: true = closed (passing), false = open (blocking) |
errorCtor | IConstructor<Error>? | E_BREAKER_OPENED | Error class to throw when open |
Methods
close()
close(): voidCloses the breaker — subsequent calls to call() will pass through.
open()
open(): voidOpens the breaker — subsequent calls to call() will throw.
isClosed()
isClosed(): booleanReturns true if the breaker is closed (passing).
isOpened()
isOpened(): booleanReturns true if the breaker is open (blocking).
call(cb)
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)
wrap<T extends ISimpleFn>(cb: T): TReturns 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.
type IErrorEventCallback = (error: unknown) => void;Example
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();