跳转到内容
简体中文

CircuitBreaker

此内容尚不支持你的语言。

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.

new CircuitBreaker(options): CircuitBreaker

Defined in: src/pattern/CircuitBreaker.ts:103

CircuitBreakerOptions

CircuitBreaker

readonly options: CircuitBreakerOptionsType

Defined in: src/pattern/CircuitBreaker.ts:85

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.

number


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.

number


get state(): CircuitState

Defined in: src/pattern/CircuitBreaker.ts:111

CircuitState

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.

T

() => Promise<T>

(error) => boolean

Promise<T>


onStateChange(listener): () => void

Defined in: src/pattern/CircuitBreaker.ts:172

Observe state transitions — useful for logging/metrics.

StateListener

() => void


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.)

CircuitState

void