Forum Discussion

coder1232045's avatar
coder1232045
Copper Contributor
Jul 27, 2022

How to have dynamic Jittered Back-off for wait and retry in .Net core using polly

 Hi,

I am implementing wait and https://github.com/Polly-Contrib/Polly.Contrib.WaitAndRetry#new-jitter-recommendation see below. In the example below the delay is same.

How can I make delay dynamic?
var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay:TimeSpan.FromSeconds(1), retryCount: 3);
var retryPolicy = Policy.Handle<FooException>().WaitAndRetryAsync(delay);

 

In my Project, I have Azure function https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection


public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var configuration = builder.GetContext().Configuration;
        builder.Services.RegisterService(configuration);
    }
}
public static IHttpClientBuilder RegisterService(this IServiceCollection serviceCollection,IConfiguration configuration)
{
    return serviceCollection.AddHttpClient<IService, Service()
               .AddPolicyHandler(RetryPolicyHandler.GetRetryPolicy())
}

public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(IConfiguration configuration)
{
    var delay =Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 3);
    return HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync(delay);
}
 
If you see the update code. Startup class override configure method that call register service>RetryPolicy.
 
Startup-> Configure-> Register Service -> RetryPolicy
 
My understanding is RetryPolicy will be called once (function startup) that will set the delay duration. Correct me if I am wrong?
 
Sorry I am new to polly and github wiki does not have this information.How can I make delay dynamic?Please advice. Thanks
No RepliesBe the first to reply

Resources