CircuitBreaker class
Wraps an unreliable operation with the circuit-breaker resilience pattern: after failureThreshold consecutive failures the breaker "opens" and fast-fails calls for resetTimeout; one probe call is then admitted, and its outcome decides whether the breaker closes or reopens.
final breaker = CircuitBreaker(failureThreshold: 3);
final result = await breaker.execute(() => api.fetchUser(id));
// If the breaker is open, `result` is an Error with
// FailureKind.circuitOpen — the underlying call was not made.
Constructors
- CircuitBreaker({int failureThreshold = 5, Duration resetTimeout = const Duration(seconds: 30), bool shouldTrip(Failure failure)?, void onStateChange(CircuitBreakerState from, CircuitBreakerState to)?, DateTime clock()?})
- Creates a breaker.
Properties
- consecutiveFailures → int
-
Number of consecutive failures observed in the current closed window.
Resets on every Success and on a successful probe in half-open.
no setter
- failureThreshold → int
-
Number of consecutive trip-eligible failures that move the breaker to
CircuitBreakerState.open.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- resetTimeout → Duration
-
How long the breaker stays in CircuitBreakerState.open before
admitting a single probe call.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → CircuitBreakerState
-
The current state of the breaker.
no setter
Methods
-
execute<
T> (Future< Result< operation()) → Future<T> >Result< T> > -
Runs
operationsubject to the breaker's state machine. ReturnsError(Failure(kind: FailureKind.circuitOpen))if the breaker is open (or if a half-open probe is already in flight) without callingoperation. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Force the breaker back to CircuitBreakerState.closed, clearing failure counters. Useful after an out-of-band recovery (e.g. operator resets the dependency manually).
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited