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

Comments

Projects
None yet
2 participants
@vany0114

vany0114 commented Jul 19, 2018

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 from Issue with CircuitBreaker policy to Circuit-breakers count all faults they handle as an aggregate, not separately Jul 20, 2018

@reisenberger

This comment has been minimized.

Show comment
Hide comment
@reisenberger

reisenberger Jul 20, 2018

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.

Member

reisenberger commented Jul 20, 2018

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