Im new to Polly and is trying to create a circuit breaker with fallback and timeout policy. My setup looks like below where all policies are "global" so they keep state between calls:
_timeoutPolicy =
Policy.Timeout(TimeSpan.FromMilliseconds(1500),TimeoutStrategy.Pessimistic);
_circuitBreaker = Policy.Handle<Exception>()
.AdvancedCircuitBreaker(
failureThreshold:0.5,
samplingDuration: TimeSpan.FromSeconds(20),
minimumThroughput: 5,
durationOfBreak: TimeSpan.FromSeconds(30)
);
_policy = Policy<ServiceResponse<T>>
.Handle<Exception>()
.Fallback(() => new ServiceResponse<T>()
{
IsValid = false,
Message = "Tjänsten fungerar inte"
}).Wrap(_circuitBreaker).Wrap(_timeoutPolicy);
Later I use _policy
for calling external webapi as:
_policy.Execute(() => SomeWebApiCallMethod<T>());
What I want to achieve is to activate fallback response if circuit breaker policy in combination with timeout policy occurs... With current setup it works first round i.e 5 error occurs during 20 sec after that fallback kicks in... I wait 30 sek and now after only 1 try that is over 1500 ms fallback kicks in again(?) but this is to early because circuit breaker policy should make 5 tries in 20 sec span before fallback kicks in... I would be happy if anyone could point me in right direction how to solve this.
INITIAL ANSWER (may be updated after question clarified)
Re:
I wait 30 sek and now after only 1 try that is over 1500 ms fallback kicks in again(?) but this is too early because circuit breaker policy should make 5 tries in 20 sec span before fallback kicks in
The circuit-breaker breaking again due to a single further failure after the break period, is the designed, expected behaviour. Rather than transitioning straight back to closed state (and being governed by the metrics defined for closed state), the circuit-breaker, after the break period, first enters a 'half-open' state, where the next single call is treated as a trial call to determine the health of the called system: if that single trial call fails, the circuit breaks again. Detailed documentation here.
Re:
circuit breaker policy should make 5 tries in 20 sec span
Circuit-breakers do not make tries; they measure-and-allow a set number/ratio of failures before breaking. If you wish your Polly policy also to orchestrate retries for you, combine a RetryPolicy
with circuit-breaker, as described here.