Search code examples
c#pollycircuit-breakerretry-logicbulkhead

Access to wrapped Polly policy


If I wrap some polices is it possible to acces them from the wrapped policy?

example:

var handle = Policy.Handle<Exception>();//.OrResult<HttpResponseMessage>(r => r.IsSuccessStatusCode == false);

var timeout = Policy.TimeoutAsync(() => TimeSpan.FromMinutes(5)  /*loginConnectorOptions.Timeout*/);
var retry = handle.RetryAsync(retryCount: 3);
var cb = handle.CircuitBreakerAsync(exceptionsAllowedBeforeBreaking: 3, durationOfBreak: TimeSpan.FromMinutes(3));
var bulkhead = Policy.BulkheadAsync(maxParallelization: 4, maxQueuingActions: 20);
_lcPolicy = Policy.WrapAsync(bulkhead, retry, cb, timeout);
_lcPolicy.WithPolicyKey("LoginConnector");

I would like to access the circuit breaker to get access to the state.


Solution

  • There is not currently a way within Polly to interrogate a configured PolicyWrap to obtain the policies it consists of. This could be added: please raise a feature request as an Issue on Polly's Github, if desired.

    From the code example presented in the question, of course you have the circuit-breaker in the variable cb. So for now you could pass the value of cb to where you need it, and access cb.CircuitState.

    EDIT: Polly v5.6.0 now provides the ability to obtain the policies within a PolicyWrap natively, using the .GetPolices<> (and related) methods.