Table of Contents

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

options CircuitBreakerOptions

Circuit breaker configuration options.

circuitName string

Optional name for the circuit breaker (used in logging and events).

logger ILogger

Optional logger instance.

Properties

ConsecutiveFailures

Gets the number of consecutive failures since the last success.

public int ConsecutiveFailures { get; }

Property Value

int

LastOpenedAt

Gets the timestamp when the circuit was last opened.

public DateTimeOffset? LastOpenedAt { get; }

Property Value

DateTimeOffset?

State

Gets the current state of the circuit breaker.

public CircuitState State { get; }

Property Value

CircuitState

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

action Func<CancellationToken, Task>
cancellationToken CancellationToken

Returns

Task

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

action Func<CancellationToken, Task<TResult>>

The action to execute.

isFailure Func<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.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<TResult>

The result of the action.

Type Parameters

TResult

The 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

action Func<CancellationToken, Task<TResult>>

The action to execute.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<TResult>

The result of the action if successful.

Type Parameters

TResult

The 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

cancellationToken CancellationToken

Cancellation 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

Event Type

EventHandler<CircuitStateChangedEventArgs>