2

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);
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
itaustralia
  • 127
  • 1
  • 12
  • @itasutralia Currently you have 4 open polly related questions. Please consider to close the other three if you have received answers which are suitable for your needs. – Peter Csala Jul 27 '22 at 05:17
  • 1
    @PeterCsala I have closed others polly questions. Thanks for help – itaustralia Jul 27 '22 at 07:22
  • Thank you. Does my post answer this question? – Peter Csala Jul 27 '22 at 08:27
  • I have updated the code above. Question: 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. Please advice. Thanks – itaustralia Jul 27 '22 at 11:43
  • I'm currently reading this on phone. Later today when I will sit at my computer I will reflect to the update. – Peter Csala Jul 27 '22 at 12:11
  • Thanks PeterCsala. You are amazing. – itaustralia Jul 27 '22 at 12:36

1 Answers1

3

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Hi @Peter, Thank you for response. I have updated the code above. Question: 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. Please advice. Thanks – itaustralia Jul 27 '22 at 06:57
  • @itaustralia I've updated my post, please check it! – Peter Csala Jul 27 '22 at 13:06
  • thanks a lot. let me try. Again Thanks – itaustralia Jul 27 '22 at 13:13