Class CircuitBreaker
Provides circuit breaker functionality to provide stability when working with "dangerous" operations, e.g. calls to remote systems
Transitions through three states:
- In Closed state, calls pass through until the maxFailures count is reached. This causes the circuit breaker to open. Both exceptions and calls exceeding callTimeout are considered failures.
- In Open state, calls fail-fast with an exception. After resetTimeout, circuit breaker transitions to half-open state.
- In Half-Open state, the first call will be allowed through, if it succeeds the circuit breaker will reset to closed state. If it fails, the circuit breaker will re-open to open state. All calls beyond the first that execute while the first is running will fail-fast with an exception.
Inherited Members
Namespace: Akka.Pattern
Assembly: Akka.dll
Syntax
public class CircuitBreaker
Constructors
| Improve this Doc View SourceCircuitBreaker(IScheduler, Int32, TimeSpan, TimeSpan)
Create a new CircuitBreaker
Declaration
public CircuitBreaker(IScheduler scheduler, int maxFailures, TimeSpan callTimeout, TimeSpan resetTimeout)
Parameters
Type | Name | Description |
---|---|---|
IScheduler | scheduler | Reference to Akka scheduler |
Int32 | maxFailures | Maximum number of failures before opening the circuit |
TimeSpan | callTimeout | TimeSpan of time after which to consider a call a failure |
TimeSpan | resetTimeout | TimeSpan of time after which to attempt to close the circuit |
CircuitBreaker(IScheduler, Int32, TimeSpan, TimeSpan, TimeSpan, Double)
Create a new CircuitBreaker
Declaration
public CircuitBreaker(IScheduler scheduler, int maxFailures, TimeSpan callTimeout, TimeSpan resetTimeout, TimeSpan maxResetTimeout, double exponentialBackoffFactor)
Parameters
Type | Name | Description |
---|---|---|
IScheduler | scheduler | Reference to Akka scheduler |
Int32 | maxFailures | Maximum number of failures before opening the circuit |
TimeSpan | callTimeout | TimeSpan of time after which to consider a call a failure |
TimeSpan | resetTimeout | TimeSpan of time after which to attempt to close the circuit |
TimeSpan | maxResetTimeout | |
Double | exponentialBackoffFactor |
CircuitBreaker(IScheduler, Int32, TimeSpan, TimeSpan, TimeSpan, Double, Double)
Create a new CircuitBreaker
Declaration
public CircuitBreaker(IScheduler scheduler, int maxFailures, TimeSpan callTimeout, TimeSpan resetTimeout, TimeSpan maxResetTimeout, double exponentialBackoffFactor, double randomFactor)
Parameters
Type | Name | Description |
---|---|---|
IScheduler | scheduler | Reference to Akka scheduler |
Int32 | maxFailures | Maximum number of failures before opening the circuit |
TimeSpan | callTimeout | TimeSpan of time after which to consider a call a failure |
TimeSpan | resetTimeout | TimeSpan of time after which to attempt to close the circuit |
TimeSpan | maxResetTimeout | |
Double | exponentialBackoffFactor | |
Double | randomFactor | After calculation of the exponential back-off an additional random delay based on this factor is added, e.g. |
Properties
| Improve this Doc View SourceCallTimeout
TBD
Declaration
public TimeSpan CallTimeout { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
CurrentFailureCount
Retrieves current failure count.
Declaration
public long CurrentFailureCount { get; }
Property Value
Type | Description |
---|---|
Int64 |
ExponentialBackoffFactor
TBD
Declaration
public double ExponentialBackoffFactor { get; }
Property Value
Type | Description |
---|---|
Double |
IsClosed
Return true if the internal state is Closed. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in WithCircuitBreaker(Func<Task>). So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.
Declaration
public bool IsClosed { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsHalfOpen
Return true if the internal state is HalfOpen. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.
Declaration
public bool IsHalfOpen { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsOpen
Return true if the internal state is Open. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in WithCircuitBreaker(Func<Task>). So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.
Declaration
public bool IsOpen { get; }
Property Value
Type | Description |
---|---|
Boolean |
LastCaughtException
Declaration
public Exception LastCaughtException { get; }
Property Value
Type | Description |
---|---|
Exception |
MaxFailures
TBD
Declaration
public int MaxFailures { get; }
Property Value
Type | Description |
---|---|
Int32 |
MaxResetTimeout
TBD
Declaration
public TimeSpan MaxResetTimeout { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
RandomFactor
TBD
Declaration
public double RandomFactor { get; }
Property Value
Type | Description |
---|---|
Double |
ResetTimeout
TBD
Declaration
public TimeSpan ResetTimeout { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
Scheduler
TBD
Declaration
public IScheduler Scheduler { get; }
Property Value
Type | Description |
---|---|
IScheduler |
Methods
| Improve this Doc View SourceCreate(IScheduler, Int32, TimeSpan, TimeSpan)
Create a new CircuitBreaker
Declaration
public static CircuitBreaker Create(IScheduler scheduler, int maxFailures, TimeSpan callTimeout, TimeSpan resetTimeout)
Parameters
Type | Name | Description |
---|---|---|
IScheduler | scheduler | Reference to Akka scheduler |
Int32 | maxFailures | Maximum number of failures before opening the circuit |
TimeSpan | callTimeout | TimeSpan of time after which to consider a call a failure |
TimeSpan | resetTimeout | TimeSpan of time after which to attempt to close the circuit |
Returns
Type | Description |
---|---|
CircuitBreaker | TBD |
Fail()
Mark a failed call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a failed call instead of using Future via WithCircuitBreaker(Func<Task>)
Declaration
public void Fail()
OnClose(Action)
Adds a callback to execute when circuit breaker state closes
Declaration
public CircuitBreaker OnClose(Action callback)
Parameters
Type | Name | Description |
---|---|---|
Action | callback | Action Handler to be invoked on state change |
Returns
Type | Description |
---|---|
CircuitBreaker | CircuitBreaker for fluent usage |
OnHalfOpen(Action)
Adds a callback to execute when circuit breaker transitions to half-open
Declaration
public CircuitBreaker OnHalfOpen(Action callback)
Parameters
Type | Name | Description |
---|---|---|
Action | callback | Action Handler to be invoked on state change |
Returns
Type | Description |
---|---|
CircuitBreaker | CircuitBreaker for fluent usage |
OnOpen(Action)
Adds a callback to execute when circuit breaker opens
Declaration
public CircuitBreaker OnOpen(Action callback)
Parameters
Type | Name | Description |
---|---|---|
Action | callback | Action Handler to be invoked on state change |
Returns
Type | Description |
---|---|
CircuitBreaker | CircuitBreaker for fluent usage |
Succeed()
Mark a successful call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a successful call instead of using Future via WithCircuitBreaker(Func<Task>)
Declaration
public void Succeed()
WithCircuitBreaker(Func<Task>)
Wraps invocation of asynchronous calls that need to be protected
Declaration
public Task WithCircuitBreaker(Func<Task> body)
Parameters
Type | Name | Description |
---|---|---|
Func<Task> | body | Call needing protected |
Returns
Type | Description |
---|---|
Task | Task |
WithCircuitBreaker<TState>(TState, Func<TState, Task>)
Declaration
public Task WithCircuitBreaker<TState>(TState state, Func<TState, Task> body)
Parameters
Type | Name | Description |
---|---|---|
TState | state | |
Func<TState, Task> | body |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
TState |
WithCircuitBreaker<T>(Func<Task<T>>)
Wraps invocation of asynchronous calls that need to be protected
Declaration
public Task<T> WithCircuitBreaker<T>(Func<Task<T>> body)
Parameters
Type | Name | Description |
---|---|---|
Func<Task<T>> | body | Call needing protected |
Returns
Type | Description |
---|---|
Task<T> | Task containing the call result |
Type Parameters
Name | Description |
---|---|
T | TBD |
WithCircuitBreaker<T, TState>(TState, Func<TState, Task<T>>)
Declaration
public Task<T> WithCircuitBreaker<T, TState>(TState state, Func<TState, Task<T>> body)
Parameters
Type | Name | Description |
---|---|---|
TState | state | |
Func<TState, Task<T>> | body |
Returns
Type | Description |
---|---|
Task<T> |
Type Parameters
Name | Description |
---|---|
T | |
TState |
WithExponentialBackoff(TimeSpan)
The ResetTimeout will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.
Declaration
public CircuitBreaker WithExponentialBackoff(TimeSpan maxResetTimeout)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | maxResetTimeout | The upper bound of ResetTimeout |
Returns
Type | Description |
---|---|
CircuitBreaker |
WithRandomFactor(Double)
Adds jitter to the delay.
Declaration
public CircuitBreaker WithRandomFactor(double randomFactor)
Parameters
Type | Name | Description |
---|---|---|
Double | randomFactor | after calculation of the back-off an additional random delay based on this factor is added, e.g. 0.2 adds up to 20% delay. In order to skip this additional delay pass in 0. |
Returns
Type | Description |
---|---|
CircuitBreaker |
WithSyncCircuitBreaker(Action)
Wraps invocations of asynchronous calls that need to be protected.
Declaration
public void WithSyncCircuitBreaker(Action body)
Parameters
Type | Name | Description |
---|---|---|
Action | body | Call needing protected |
WithSyncCircuitBreaker<T>(Func<T>)
Wraps invocations of asynchronous calls that need to be protected.
Declaration
public T WithSyncCircuitBreaker<T>(Func<T> body)
Parameters
Type | Name | Description |
---|---|---|
Func<T> | body | Call needing protected |
Returns
Type | Description |
---|---|
T | The result of the call |
Type Parameters
Name | Description |
---|---|
T |