App-vNext / Polly.Extensions.Http

Polly.Extensions.Http is an extensions package containing opinionated convenience methods for configuring Polly policies to handle transient faults typical of calls through HttpClient.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AdvancedCircuitBreakerSyntaxAsync.AdvancedCircuitBreakerAsync(PolicyBuilder, double, TimeSpan, int, TimeSpan, Action<Exception, TimeSpan>, Action)' requires a receiver of type 'PolicyBuilder'

jvelezc opened this issue · comments

commented

I basically wanted to add logging of exceptions on error but no matter what I try I can't get the syntax right.

Doesn't work:

Action<Exception, TimeSpan> onBreak = (exception,duration) => { };
Action onReset = () => { };
            var circuitBreaker = HttpPolicyExtensions
            .HandleTransientHttpError().AdvancedCircuitBreakerAsync
            (
                failureThreshold: 0.5,
                samplingDuration: TimeSpan.FromSeconds(10),
                minimumThroughput: 2,
                durationOfBreak: TimeSpan.FromSeconds(30),
                onBreak: onBreak,
                onReset: onReset
            );  

Works

var circuitBreaker = HttpPolicyExtensions
            .HandleTransientHttpError().AdvancedCircuitBreakerAsync
            (
                failureThreshold: 0.5,
                samplingDuration: TimeSpan.FromSeconds(10),
                minimumThroughput: 2,
                durationOfBreak: TimeSpan.FromSeconds(30)
       
            );

Hi @jvelezc !

Policies defined by HttpPolicyExtensions.HandleTransientHttpError() handle both exceptions and Http result codes, This means the first input parameter to onBreak is of type DelegateResult<HttpResponseMessage> rather than Exception. So:

Action<DelegateResult<HttpResponseMessage>, TimeSpan> onBreak = (handledFault, duration) => { };
Action onReset = () => { };
var circuitBreaker = HttpPolicyExtensions
    .HandleTransientHttpError().AdvancedCircuitBreakerAsync
    (
        failureThreshold: 0.5,
        samplingDuration: TimeSpan.FromSeconds(10),
        minimumThroughput: 2,
        durationOfBreak: TimeSpan.FromSeconds(30),
        onBreak: onBreak,
        onReset: onReset
    );

This allows the onBreak delegate to also capture the cause of the failure if it was a handled Http status code. For instance:

Action<DelegateResult<HttpResponseMessage>, TimeSpan> onBreak = (handledFault, duration) =>
{
    logger.LogError($"Circuit breaking for {duration} due to {handledFault.Exception?.Message ?? handledFault.Result.StatusCode.ToString()}");
};
Action onReset = () => { /* etc */ };

var circuitBreaker = HttpPolicyExtensions
    .HandleTransientHttpError().AdvancedCircuitBreakerAsync
    (
        failureThreshold: 0.5,
        samplingDuration: TimeSpan.FromSeconds(10),
        minimumThroughput: 2,
        durationOfBreak: TimeSpan.FromSeconds(30),
        onBreak: onBreak,
        onReset: onReset
    );

There is more information about DelegateResult<TResult> in the Polly readme.

I hope that helps, but let us know if there is anything else we can help with!

Closing due to lack of further response.