-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature suggestion: In-box DecorrelatedJitter #526
Comments
Thanks @grant-d for taking the time to package this a step further than our quick sample. Pending this while we have discussion on #530. Note for later: If we do this, it would be make sense to (trivially) do some of the other IEnumerable patterns (like straight ExponentialBackoff) as convenience helpers too. |
That's a good idea, I will wrap some of the other strategies too |
I had a thought. If we add more 'sleep duration providers', then we could formalize the pattern as follows. The benefit is that we can then provide a host of duration providers, and the pattern protects the user from calling '.Create' at the wrong time. public interface ISleepDurationStrategy // naming TBD
{
int RetryCount { get; }
// For when IEnumerable<TimeSpan> is needed
IEnumerable<TimeSpan> Create(Context context = null);
// For when Func<int, Context, TimeSpan> is needed
TimeSpan Next(int i, Context context = null);
// etc
}
public sealed class DecorrelatedJitter : ISleepDurationStrategy
{
// Implementations per initial post above
}
public sealed class ExponentialBackoffProvider : ISleepDurationStrategy { }
public sealed class ConstantBackoffProvider : ISleepDurationStrategy { }
public sealed class MyCustomProvider : ISleepDurationStrategy { }
public sealed class NoOpProvider : ISleepDurationStrategy { }
// Additional overloads/extensions on Policy/Builder (only showing two)
public static RetryPolicy<TResult> WaitAndRetryAsync<TResult>(
this PolicyBuilder<TResult> policyBuilder,
ISleepDurationStrategy sleepDurationStrategy) // Note param
{
var sleepDurations = sleepDurationStrategy.Generate(); // Note call to .Generate
return policyBuilder.WaitAndRetryAsync(sleepDurations); // Existing IEnumerable<TimeSpan> overload
}
public static RetryPolicy<TResult> WaitAndRetry<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, ISleepDurationStrategy sleepDurationProvider)
{
Action<DelegateResult<TResult>, TimeSpan, int, Context> doNothing = (_, __, ___, ____) => { };
return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider.Next, doNothing); // Existing Func<int, Context, TimeSpan> overload
} |
I have created a PR #536, please see latest code there |
If DecorrelatedJitter is a common-enough requirement, could we turn it into a first class Polly citizen.
I am happy to submit a PR. Otherwise, proposed solution is below.
This is a little different to what's shown in the wiki; it's an encapsulated and testable class.
[Edited to fix bug]
Here's an example of how to call it. Note that the class is instantiated as a singleton (directly or via DI) and each execution uses the
.Generate()
.This is a nice-to-have, certainly doesn't have to be in-box but it would remove the duplication in custom code.
The text was updated successfully, but these errors were encountered: