Polly.Contrib.WaitAndRetry icon indicating copy to clipboard operation
Polly.Contrib.WaitAndRetry copied to clipboard

How can i use the DecorrelatedJitterBackoffV2 method for infinite retries?

Open itachiCrash opened this issue 4 years ago • 3 comments

I am trying to implement the policy with DecorrelatedJitterBackoffV2 method:

private static void SetPolicies() { var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 5); _policy = Policy.Handle<Exception>().WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt)), (exception, timespan, context) => { Log.Information( exception.Message); }); }

But it has no been possible to include the delay because of the number of attempts. I mean, i need to use the delay generated in the method WaitAndRetryForeverAsync. is it possible?

Thanks in advance.

itachiCrash avatar Apr 22 '21 15:04 itachiCrash

I think you can achieve the desired result using WaitAndRetryAsync method

private static void SetPolicies()
{
    var delay = Backoff.DecorrelatedJitterBackoffV2(
        medianFirstRetryDelay: TimeSpan.FromSeconds(1),
        retryCount: int.MaxValue);
    var _policy = Policy
        .Handle<Exception>()
        .WaitAndRetryAsync(
            sleepDurations: delay,
            onRetry: (exception, timespan, context) =>
        {
            Log.Information(exception.Message);
        });
}

devigo avatar Aug 26 '21 21:08 devigo

I would also like to use int.MaxValue but it kind of feels wrong because int.MaxValue is not really handled by the Backoff strategies. The main polly code recognizes a int.MaxValue retry variant and simply keeps looping even when int.MaxValue is reached: https://github.com/App-vNext/Polly/blob/174cc53e17bf02da5e1f2c0d74dffb4f23aa99c0/src/Polly/Retry/RetryEngine.cs#L70

swimmesberger avatar Sep 14 '21 13:09 swimmesberger

Resurrecting this issue. An additional problem with using int.MaxValue as the retryCount is that Backoff.DecorrelatedJitterBackoffV2 will eventually return negative TimeSpan values. This can be easily seen by cloning the repo and changing the retryCount argument in Backoff_should_not_overflow_to_give_negative_timespan from 100 to 10000.

Re: point from @swimmesberger, I am not sure this is a problem in practice because even if each delay is an average of only 1 second, with a retryCount of int.MaxValue that still comes to 68 years.

cyungmann avatar Nov 20 '23 21:11 cyungmann