Table of Contents

Class CircuitBreakerStrategyOptions<TResult>

Namespace
Polly.CircuitBreaker
Assembly
Polly.Core.dll

The options for circuit breaker resilience strategy.

public class CircuitBreakerStrategyOptions<TResult> : ResilienceStrategyOptions

Type Parameters

TResult

The type of result the circuit breaker strategy handles.

Inheritance
CircuitBreakerStrategyOptions<TResult>
Derived
Inherited Members

Remarks

The circuit will break if, within any time-slice of duration SamplingDuration, the proportion of actions resulting in a handled exception exceeds FailureRatio, provided also that the number of actions through the circuit in the time-slice is at least MinimumThroughput.

The circuit will stay broken for the BreakDuration. Any attempt to execute this while the circuit is broken, will immediately throw a BrokenCircuitException containing the exception that broke the circuit.

If the first action after the break duration period results in a handled exception, the circuit will break again for another BreakDuration; if no exception is thrown, the circuit will reset.

Constructors

CircuitBreakerStrategyOptions()

Initializes a new instance of the CircuitBreakerStrategyOptions<TResult> class.

public CircuitBreakerStrategyOptions()

Properties

BreakDuration

Gets or sets the duration of break the circuit will stay open before resetting.

[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
public TimeSpan BreakDuration { get; set; }

Property Value

TimeSpan

The default value is 5 seconds. Value must be greater than 0.5 seconds.

BreakDurationGenerator

Gets or sets an optional delegate to use to dynamically generate the break duration.

public Func<BreakDurationGeneratorArguments, ValueTask<TimeSpan>>? BreakDurationGenerator { get; set; }

Property Value

Func<BreakDurationGeneratorArguments, ValueTask<TimeSpan>>

The default value is null.

FailureRatio

Gets or sets the failure-to-success ratio at which the circuit will break.

[Range(0, 1)]
public double FailureRatio { get; set; }

Property Value

double

A ratio number higher than 0, up to 1. The default value is 0.1 (i.e. 10%).

Remarks

A number between zero and one (inclusive) e.g. 0.5 represents breaking if 50% or more of actions result in a handled failure.

ManualControl

Gets or sets the manual control for the circuit breaker.

public CircuitBreakerManualControl? ManualControl { get; set; }

Property Value

CircuitBreakerManualControl

The default value is null.

MinimumThroughput

Gets or sets the minimum throughput: this many actions or more must pass through the circuit in the time-slice, for statistics to be considered significant and the circuit-breaker to come into action.

[Range(2, 2147483647)]
public int MinimumThroughput { get; set; }

Property Value

int

The default value is 100. The value must be 2 or greater.

OnClosed

Gets or sets the event that is raised when the circuit resets to a Closed state.

public Func<OnCircuitClosedArguments<TResult>, ValueTask>? OnClosed { get; set; }

Property Value

Func<OnCircuitClosedArguments<TResult>, ValueTask>

The default value is null.

Remarks

The callbacks registered to this event are invoked with eventual consistency. There is no guarantee that the circuit breaker doesn't change the state before the callbacks finish. If you need to know the up-to-date state of the circuit breaker use the CircuitState property.

Note that these events might be executed asynchronously at a later time when the circuit state is no longer the same as at the point of invocation of the event. However, the invocation order of the OnOpened, OnClosed, and OnHalfOpened events is always maintained to ensure the correct sequence of state transitions.

OnHalfOpened

Gets or sets the event that is raised when when the circuit transitions to an HalfOpen state.

public Func<OnCircuitHalfOpenedArguments, ValueTask>? OnHalfOpened { get; set; }

Property Value

Func<OnCircuitHalfOpenedArguments, ValueTask>

The default value is null.

Remarks

The callbacks registered to this event are invoked with eventual consistency. There is no guarantee that the circuit breaker doesn't change the state before the callbacks finish. If you need to know the up-to-date state of the circuit breaker use the CircuitState property.

Note that these events might be executed asynchronously at a later time when the circuit state is no longer the same as at the point of invocation of the event. However, the invocation order of the OnOpened, OnClosed, and OnHalfOpened events is always maintained to ensure the correct sequence of state transitions.

OnOpened

Gets or sets the event that is raised when the circuit transitions to an Open state.

public Func<OnCircuitOpenedArguments<TResult>, ValueTask>? OnOpened { get; set; }

Property Value

Func<OnCircuitOpenedArguments<TResult>, ValueTask>

The default value is null.

Remarks

The callbacks registered to this event are invoked with eventual consistency. There is no guarantee that the circuit breaker doesn't change the state before the callbacks finish. If you need to know the up-to-date state of the circuit breaker use the CircuitState property.

Note that these events might be executed asynchronously at a later time when the circuit state is no longer the same as at the point of invocation of the event. However, the invocation order of the OnOpened, OnClosed, and OnHalfOpened events is always maintained to ensure the correct sequence of state transitions.

SamplingDuration

Gets or sets the duration of the sampling over which failure ratios are assessed.

[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
public TimeSpan SamplingDuration { get; set; }

Property Value

TimeSpan

The default value is 30 seconds. Value must be greater than 0.5 seconds.

ShouldHandle

Gets or sets a predicate that determines whether the outcome should be handled by the circuit breaker.

[Required]
public Func<CircuitBreakerPredicateArguments<TResult>, ValueTask<bool>> ShouldHandle { get; set; }

Property Value

Func<CircuitBreakerPredicateArguments<TResult>, ValueTask<bool>>

The default value is a predicate that handles circuit breaker on any exception except OperationCanceledException. This property is required.

StateProvider

Gets or sets the state provider for the circuit breaker.

public CircuitBreakerStateProvider? StateProvider { get; set; }

Property Value

CircuitBreakerStateProvider

The default value is null.