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

Time:07-28

I am implementing wait and 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.

  • Related