Who this is for: projects that wrap functions in opossum’s CircuitBreaker and want retries, hedging, rate limiting, typed errors, shared breaker state across instances, or ESM and TypeScript types in the package.
opossum is one object that bundles a circuit breaker, a timeout, a concurrency cap (capacity), a cache and a fallback. Here each of those is a separate strategy in a pipeline, and you pick the order. A pipeline wraps a call, not a function: pipeline.execute(() => fn(a, b)), or pipeline.wrap(fn) to get a function with the same arguments, like breaker.fire(a, b).
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Aborted when the caller cancels, or when a strategy (timeout, hedging) gives up on this execution.
signal));
Order matters: the fallback is first so it also catches the open circuit, and the timeout is inside the breaker so timeouts count as failures. That is the order opossum uses internally. Pass the signal on: when the timeout fires, it aborts, so the slow request stops instead of running in the background.
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Probes the dependency in the background while the circuit is closed; a failing probe opens it (opossum's healthCheck).
addCircuitBreaker({ extensions: [circuitHealthCheck({ check: () => db.ping(), interval: '10s' })] })
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Background behaviour plugged into the breaker, e.g. circuitHealthCheck({ check, interval }) or
sharedCircuitState({ store, key }) from this package. Kept out of the core breaker so you only bundle what you use.
Probes the dependency in the background while the circuit is closed; a failing probe opens it (opossum's healthCheck).
addCircuitBreaker({ extensions: [circuitHealthCheck({ check: () => db.ping(), interval: '10s' })] })
Runs the callback through the pipeline; throws the final error.
execute(() =>
functiongetUser(id:string):Promise<User>
getUser(
constid:string
id), {
ExecuteOptions.operationKey?: string |undefined
operationKey: `user:${
constid:string
id}` });
The cache key comes from the execution (operationKey, or a key: (context) => … option), not from the function’s arguments. Concurrent misses for the same key share one call by default (coalesce: true).
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Adds the listener function to the end of the listeners array for the event
named eventName. No checks are made to see if the listener has already
been added. Multiple calls passing the same combination of eventName and
listener will result in the listener being added, and called, multiple times.
server.on('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the
event listener to the beginning of the listeners array.
Threshold comparison. opossum opens when the error rate is aboveerrorThresholdPercentage; ours opens when the ratio reachesfailureRatio. At exactly 50% opossum stays closed and we open.
Filtered errors. An error for which opossum’s errorFilter returns true counts as a success and skips the fallback. An error our shouldHandle rejects is ignored: not a failure, not a success. Our default already ignores 4xx client errors (except 408/429), caller aborts and rejections from other strategies; opossum counts every rejection unless you filter it.
Half-open. opossum lets the next call through after resetTimeout and reopens on the first failure. We let halfOpenMaxCalls probes through (default 1), and a probe that runs longer than halfOpenMaxWait (30 s) counts as failed.
Fallback scope. opossum’s fallback runs for any failure of that breaker, gets the fire arguments, and keeps running while the circuit is open. Ours wraps whatever is after it, receives { outcome, context }, and by default skips caller aborts and 4xx client errors.
Cache key. opossum keys the cache by JSON.stringify(args). We have no access to the arguments: give each call an operationKey or a key function. Executions without a key bypass the cache.
Coalescing lifetime. opossum’s coalesce holds results for coalesceTTL (default: the timeout). addCoalesce() only shares calls that are in flight at the same time; use addCache({ ttl }) to keep results longer.
One signal per call. opossum’s abortController is shared by all calls of a breaker, which is why autoRenewAbortController exists. Here every execution and every attempt gets its own context.signal.
disable() bypasses every strategy of the pipeline, not only the breaker. To force the breaker alone, use manualControl.
No EventEmitter. Callbacks are options, and onTelemetry() returns an unsubscribe function, so there is no MaxListenersExceededWarning and no shutdown() needed to release listeners.