Polly-Contrib / Polly.Contrib.WaitAndRetry

Polly.Contrib.WaitAndRetry is an extension library for Polly containing helper methods for a variety of wait-and-retry strategies.
Other
132 stars 12 forks source link

How can i use the DecorrelatedJitterBackoffV2 method for infinite retries? #29

Open itachiCrash opened 3 years ago

itachiCrash commented 3 years ago

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().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.

devigo commented 3 years ago

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);
        });
}
swimmesberger commented 2 years ago

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

cyungmann commented 9 months ago

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.