Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circuit-breakers count all faults they handle as an aggregate, not separately #490

Closed
vany0114 opened this issue Jul 19, 2018 · 1 comment
Labels

Comments

@vany0114
Copy link
Contributor

I have a policy wrap with WaitAndRetry and CircuitBreaker policies, so, from my understanding the circuit only breaks when the exception is the same consecutively.

var policies = new [] {
Policy
                .Handle<SqlException>(ex => ex.Number == 40613)
                .Or<SqlException>(ex => ex.Number == 40197)
                .Or<SqlException>(ex => ex.Number == 40501)
                .Or<SqlException>(ex => ex.Number == 49918)
                .WaitAndRetryAsync(...),
Policy
                .Handle<SqlException>(ex => ex.Number == 40613)
                .Or<SqlException>(ex => ex.Number == 40197)
                .Or<SqlException>(ex => ex.Number == 40501)
                .Or<SqlException>(ex => ex.Number == 49918)
                .CircuitBreakerAsync(...)
};

await Policy.Wrap(policies).ExecuteAsync(DoSomehitngAsync);

private async Task<bool> DoSomehitngAsync()
{
            await Task.Delay(TimeSpan.FromMilliseconds(500));

            if (++_retriesCounter <= _threshold - 1)
                throw GetRandomException();

            return true;
}

So, the GetRandomException method returns a SqlException randomly (40613, 40197, 40501 or 49918). So, my circuit breaker policy is configured to break the circuit after 3 exceptions, but I would expect that only happen if I throw, let's say the 40613 SqlException in a row 3 times, but currently, it's breaking the circuit despite I'm throwing random exceptions.

I don't know if I'm misunderstanding the usage or I have something wrong with the policies.

@reisenberger reisenberger changed the title Issue with CircuitBreaker policy Circuit-breakers count all faults they handle as an aggregate, not separately Jul 20, 2018
@reisenberger
Copy link
Member

The circuit-breaker will break after N consecutive actions executed through the policy have thrown 'a' handled exception - any of the exceptions handled by the policy. This is intentional behaviour.

The documentation here used to say 'a' handled exception: I've just updated it to say 'any' handled exception, to make that clearer. Thanks for the question.


If you wanted to construct a system that counts different faults (or groups of faults) separately for the purposes of circuit-breaking, you could achieve that by defining separate circuit-breaker policy instances for each fault (or group of faults) you wanted counted separately, and then nesting those circuit-breakers in a PolicyWrap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants