1

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
user3154653
  • 203
  • 1
  • 11
  • Could you state more precisely the behaviour you are seeking to achieve, when you state: activate fallback response "if circuit breaker policy in combination with timeout policy occurs"? – mountain traveller Jul 10 '17 at 22:21

1 Answers1

0

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.

mountain traveller
  • 7,591
  • 33
  • 38
  • Thank you for a good explanation. I understand that CB evaluates its state based on historical/current data but lets say you have a flaky "legacy" system that of some reason throws exception for a certain user. Is there a way to use CB's settings/evaluation regarding % of failed req during timespan x even for "half open state" so this decision is not made based on 1 request but rather the evaluated state? – user3154653 Jul 11 '17 at 09:01
  • 1
    Like Hystrix, Polly's only metric (at time of this question) for determining how to exit HalfOpen state is a single trial call. A feature request however is open on Polly for exiting HalfOpen state with a more refined metric, with my comments on implementation here: https://github.com/App-vNext/Polly/issues/239#issuecomment-292710948 . You can if you wish +1 / add a comment on that github issue, to indicate your interest in that feature. – mountain traveller Jul 11 '17 at 17:03