Polly icon indicating copy to clipboard operation
Polly copied to clipboard

How do I pass context to a circuit breaker to be used onbreak or onreset?

Open bunceg opened this issue 10 years ago • 2 comments

Hi,

This example in the docs is confusing me a little...

// Break the circuit after the specified number of exceptions // and keep circuit broken for the specified duration, // calling an action on change of circuit state, // passing a context provided to Execute(). Action<Exception, TimeSpan, Context> onBreak = (exception, timespan, context) => { ... }; Action onReset = context => { ... }; CircuitBreakerPolicy breaker = Policy .Handle() .CircuitBreaker(2, TimeSpan.FromMinutes(1), onBreak, onReset);

I can see how to get context when onBreak or onReset is called, but not how to generate this context on Execution so that it is made available when onBreak fires.

I have created a static instance of the circuit breaker policy so that the breaker can correctly keep track of the counts, but then invoke the circuit breaker wrapper with the ExecuteAndCaptureAsync method. I can't see any overload to pass context at this point... (nor can I see this on any of the other policy method such as ExecuteAsync)

When I look at the retry policy WaitAndRetryAsync it's execute method has an overload that takes a Function and a dictionary object, which is then presumably made available as context.

Am I missing something?

Thanks

bunceg avatar Mar 17 '16 17:03 bunceg

Never mind. Worked out that I need to store a CircuitBreakerPolicy not a Policy object as my static pointer to the circuit breaker policy...

bunceg avatar Mar 17 '16 18:03 bunceg

Hi @bunceg . Re:

Worked out that I need to store a CircuitBreakerPolicy not a Policy object as my static pointer to the circuit breaker policy

Yes, that would be needed. We had to retain the ability for people to code Policy myPolicy = ....CircuitBreaker(...) for backwards compatibility. But, to gain access to the V4 circuit-breaker's full features (including passing context), you have to switch to var myPolicy = ....CircuitBreaker(...) or CircuitBreakerPolicy myPolicy = ....CircuitBreaker(...)

Hope this helps!

reisenberger avatar Mar 17 '16 18:03 reisenberger