Class PollyCircuitBreakerPolicyAdapter
- Namespace
- Excalibur.Dispatch.Resilience.Polly
- Assembly
- Excalibur.Dispatch.Resilience.Polly.dll
Polly-based circuit breaker policy adapter that wraps Polly's resilience pipeline and implements the core ICircuitBreakerPolicy interface.
public sealed class PollyCircuitBreakerPolicyAdapter : ICircuitBreakerPolicy, ICircuitBreakerEvents, ICircuitBreakerDiagnostics, IDisposable
- Inheritance
-
PollyCircuitBreakerPolicyAdapter
- Implements
- Inherited Members
- Extension Methods
Remarks
This adapter bridges Polly's circuit breaker capabilities with Dispatch's zero-dependency circuit breaker abstraction. Use this when you want Polly's advanced features like health metrics, telemetry integration, or advanced circuit breaker strategies.
For consumers who don't need Polly's advanced features, the core package provides
DefaultCircuitBreakerPolicy which has no external dependencies.
Constructors
PollyCircuitBreakerPolicyAdapter(CircuitBreakerOptions, string?, ILogger?)
Initializes a new instance of the PollyCircuitBreakerPolicyAdapter class.
public PollyCircuitBreakerPolicyAdapter(CircuitBreakerOptions options, string? circuitName = null, ILogger? logger = null)
Parameters
optionsCircuitBreakerOptionsCircuit breaker configuration options.
circuitNamestringOptional name for the circuit breaker (used in logging and events).
loggerILoggerOptional logger instance.
Properties
ConsecutiveFailures
Gets the number of consecutive failures since the last success.
public int ConsecutiveFailures { get; }
Property Value
LastOpenedAt
Gets the timestamp when the circuit was last opened.
public DateTimeOffset? LastOpenedAt { get; }
Property Value
State
Gets the current state of the circuit breaker.
public CircuitState State { get; }
Property Value
Methods
Dispose()
Disposes the circuit breaker policy adapter.
public void Dispose()
ExecuteAsync(Func<CancellationToken, Task>, CancellationToken)
public Task ExecuteAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken)
Parameters
actionFunc<CancellationToken, Task>cancellationTokenCancellationToken
Returns
Remarks
Executes the action through Polly's resilience pipeline with circuit breaker protection. If the circuit is open, throws CircuitBreakerOpenException.
ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>>, Func<TResult, bool>, CancellationToken)
Executes an action through the circuit, treating a returned value as a failure when
isFailure says so.
public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, Func<TResult, bool> isFailure, CancellationToken cancellationToken)
Parameters
actionFunc<CancellationToken, Task<TResult>>The action to execute.
isFailureFunc<TResult, bool>Decides whether a returned result counts as a failure. Not every failure is an exception: a pipeline that returns a failed result would otherwise look healthy to the circuit forever.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<TResult>
The result of the action.
Type Parameters
TResultThe result type.
Exceptions
- CircuitBreakerOpenException
Thrown when the circuit is open.
ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>>, CancellationToken)
Executes an asynchronous operation through the circuit breaker.
public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken)
Parameters
actionFunc<CancellationToken, Task<TResult>>The action to execute.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<TResult>
The result of the action if successful.
Type Parameters
TResultThe type of the result.
Remarks
This method records the outcome itself. An implementation registers the success, or registers the failure and rethrows, before control returns to the caller. Recording an outcome is not something a caller can do -- or need to do -- separately.
Exceptions
- CircuitBreakerOpenException
Thrown when the circuit is open.
ResetAsync(CancellationToken)
Manually returns the circuit breaker to the closed state.
public Task ResetAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task
A task that completes once the circuit is closed.
Remarks
When the returned task completes, State is Closed. That is the postcondition an operator depends on: the point of a manual reset is to put a circuit back into service, and a caller that cannot tell when that has happened has to poll or guess. An implementation whose close is asynchronous must therefore await it and publish the closed state before completing, and a close that fails must surface as a faulted task rather than a log line.
Events
StateChanged
Event raised when the circuit state changes.
public event EventHandler<CircuitStateChangedEventArgs>? StateChanged