Search code examples
circuit-breakerpolly

Check if a Polly circuit breaker in a wrap is closed?


I need to see is a circuit break is closed but my polices are in a policy wrap. How can find the CircuitBreakerPolicy (one or more) in the wrap? Is there a better way to know is any breaker is open for a given policy?


Solution

  • As per the Polly documentation:

    For example:

    var breaker = wrap.GetPolicy<CircuitBreakerPolicy>();
    var state = breaker.CircuitState;
    

    or:

    var breaker = wrap.GetPolicy<CircuitBreakerPolicy>(p => p.PolicyKey == "SomeKey");
    

    or:

    IEnumerable<CircuitBreakerPolicy> breakers = wrap.GetPolicies<CircuitBreakerPolicy>();
    

    CircuitBreakerPolicy (as the type to obtain) is only an example in the above. Substitute with the specific type you need:

    • CircuitBreakerPolicy
    • AsyncCircuitBreakerPolicy
    • AsyncCircuitBreakerPolicy<HttpResponseMessage>

    (or whatever)