Search code examples
c#asp.net-core.net-corepollyretry-logic

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


I am implementing wait and retry using jitter 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://learn.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);
}

Solution

  • No, the delays are not the same.

    The DecorrelatedJitterBackoffV2 will return an IEnumerable<TimeSpan> so, please issue this command:

    var sleepDurations = delay.ToArray();
    

    and use a debug visualizer to see they are different. If you run this experiment several times the delays will be always different.

    first run second run


    UPDATE #1

    My understanding is RetryPolicy will be called once (function startup) that will set the delay duration. Correct me if I am wrong

    That's a wrong assumption. A new retry policy will be created for every HttpClient call. In order to demonstrate that let's have these two subsequent method calls:

    await client.GetAsync("http://httpstat.us/408");
    await client.GetAsync("http://httpstat.us/408");
    

    and add some logging inside the onRetry delegate

    .WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 3),
        onRetry: (dr, ts) => {
            Console.WriteLine($"Delay: {ts}"); //Replace this with ILogger                     
        })
    

    then you will see something similar inside your logs:

    Delay: 00:00:00.4752054
    ...
    Delay: 00:00:01.2825508
    ...
    Delay: 00:00:03.1409815
    ...
    ...
    Delay: 00:00:01.2526426
    ...
    Delay: 00:00:01.2919173
    ...
    Delay: 00:00:00.3157069
    

    As you can see not the same sequence of sleep durations are used for both GetAsync calls.