CircuitBreaker
Ce contenu n’est pas encore disponible dans votre langue.
Defined in: src/pattern/CircuitBreaker.ts:78
Three-state circuit breaker. Wraps calls that might fail — when enough fail in a row the breaker “opens” and refuses further calls for a timeout window. The first call after the timeout probes the upstream (“half-open”); if it succeeds, the breaker closes and normal operation resumes.
Not tied to actors — works with any () => Promise<T> factory. For
actor-based usage, wrap ask(target, msg, timeout) in the factory.
The timeout window is flat by default and stays that way unless
backoffFactor or randomFactor is set: both ship neutral, so a breaker
built the way every release before #864 built one behaves identically. A
breaker whose settings should come from HOCON instead is resolved by id
through CircuitBreakerExtension; this constructor is unchanged and remains
the door for one configured in code.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new CircuitBreaker(
options):CircuitBreaker
Defined in: src/pattern/CircuitBreaker.ts:103
Parameters
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”CircuitBreaker
Properties
Section titled “Properties”options
Section titled “options”
readonlyoptions:CircuitBreakerOptionsType
Defined in: src/pattern/CircuitBreaker.ts:85
Accessors
Section titled “Accessors”consecutiveOpens
Section titled “consecutiveOpens”Get Signature
Section titled “Get Signature”get consecutiveOpens():
number
Defined in: src/pattern/CircuitBreaker.ts:128
Opens since the breaker last closed — the exponent backoffFactor is
raised to. 0 while closed, 1 on the first open.
Returns
Section titled “Returns”number
nextProbeAt
Section titled “nextProbeAt”Get Signature
Section titled “Get Signature”get nextProbeAt():
number
Defined in: src/pattern/CircuitBreaker.ts:122
Epoch milliseconds at which the next call is let through as a probe. 0
while the breaker has never opened.
Public because the reopen window is no longer a constant: with
backoffFactor or randomFactor set it differs per open, and this is the
only way for a metric — or a test — to see the schedule without waiting
out the delay it is trying to assert.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get state():
CircuitState
Defined in: src/pattern/CircuitBreaker.ts:111
Returns
Section titled “Returns”Methods
Section titled “Methods”call()
Section titled “call()”call<
T>(factory,isFailure?):Promise<T>
Defined in: src/pattern/CircuitBreaker.ts:150
Call factory under breaker supervision. Throws CircuitBreakerOpenError
when open.
isFailure classifies this call’s errors and takes precedence over the
instance’s CircuitBreakerOptionsType.isFailure. It exists because
a breaker resolved by id through CircuitBreakerExtension is shared:
the registry hands back the instance that already exists rather than
reconfiguring it, so a predicate supplied as a construction option reaches
the instance only when its supplier happened to be the first caller. A
classifier that belongs to the protected dependency rather than to one
caller therefore has to travel with the call, where no resolution order
can drop it (#874).
ignoredErrorNames is still consulted first and still wins — see there
for why the operator’s half of the classifier outranks a compiled one,
per-call or not. An excused error is neither a failure nor a success:
the consecutive-failure count survives it untouched, so one excused error
between every two real ones cannot hold a dead dependency open forever.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”factory
Section titled “factory”() => Promise<T>
isFailure?
Section titled “isFailure?”(error) => boolean
Returns
Section titled “Returns”Promise<T>
onStateChange()
Section titled “onStateChange()”onStateChange(
listener): () =>void
Defined in: src/pattern/CircuitBreaker.ts:172
Observe state transitions — useful for logging/metrics.
Parameters
Section titled “Parameters”listener
Section titled “listener”StateListener
Returns
Section titled “Returns”() => void
setState()
Section titled “setState()”setState(
next):void
Defined in: src/pattern/CircuitBreaker.ts:191
Force the breaker into a specific state (mostly for tests / admin).
Every transition into open goes through here — the failure-count path,
the failed half-open probe, and an admin call alike — so this is where the
consecutive-open counter grows and where the reopen window is computed.
A forced open therefore counts as an open cycle, which is the consistent
reading: an operator holding a dependency open again and again is the same
signal as the dependency failing again and again, and the counter is reset
by the thing that means recovery, closed. (Resetting it here alongside
failureCount would be the other consistent-looking choice and is not an
option at all: every open runs through this method, so the exponent could
never leave 1 and backoffFactor would be inert.)
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
