Polly icon indicating copy to clipboard operation
Polly copied to clipboard

Pattern for CircuitBreaker? - can't get it to work

Open bunceg opened this issue 10 years ago • 2 comments

Hi, I am trying to use the circuitbreaker but it never fires as I expect even though I believe I have followed the pattern correctly. Although it's a little unclear from the documentation, I think the use case is that Polly will throw the original exception raised until it hits the limit count for that exception, at which point it will throw a BrokenCircuitException until the timespan has expired.

I can't find any tests in the sourcecode to verify this - have these been migrated across from Michael??

Simple circuit breaker code:

try { var pol = Policy .Handle<ArgumentNullException>() .CircuitBreaker(3, TimeSpan.FromSeconds(60));

pol.Execute(() => { throw new ArgumentNullException(""); }); } catch (BrokenCircuitException ex) { throw new ServiceUnavailableException(ex); }

My test:

Assert.That(() => handler.CauseBreakerException(), Throws.InstanceOf<ArgumentNullException>()); Assert.That(() => handler.CauseBreakerException(), Throws.InstanceOf<ArgumentNullException>()); Assert.That(() => handler.CauseBreakerException(), Throws.InstanceOf<ArgumentNullException>()); Assert.That(() => handler.CauseBreakerException(), Throws.InstanceOf<ServiceUnavailableException>());

The final step does not get a ServiceUnavailableException which means the BrokenCircuitException exception is never caught. The code will continually raise an ArgumentNullException exception each time.

bunceg avatar Dec 16 '15 10:12 bunceg

Ok... worked this out. The Policy was being overwritten each time so wasn't keeping state between calls. I needed to store this in a static variable so it could remember previous actions

bunceg avatar Dec 16 '15 10:12 bunceg

@bunceg: Glad you worked it out :)

This is a great example of why I created the Polly-Samples project. If you'd like to contribute to it in order to add an example for using the Circuitbreaker, that would be great. If not, I'll do it once I have a spare moment.

Thanks!

joelhulen avatar Dec 16 '15 16:12 joelhulen