Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
itachiCrash opened this issue Apr 22, 2021 · 3 comments

Comments

@itachiCrash
Copy link

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
Copy link

devigo commented Aug 26, 2021

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants