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.