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
133 stars 12 forks source link

DecorrelatedJitterBackoffV2 without exponential side #35

Open andremantaswow opened 4 months ago

andremantaswow commented 4 months ago

Is your feature request related to a specific problem? Or an existing feature? Please describe.

Similar to existing feature.

Describe your proposed or preferred solution:

Expose a new backoff strategy, in this package, similar to Backoff.DecorrelatedJitterV2.cs but that does not include the exponential aspect in the returned sleep durations.

Describe any alternative options you've considered:

A naive implementation:

public static IEnumerable<TimeSpan> ConstantJitterBackoff(TimeSpan averageDelay, int retryCount, double jitterFactor = 0.5)
{
    for (var i = 0; i < retryCount; i++)
    {
        // Calculate random jitter
        var jitter = ((Random.Shared.NextDouble() * 2) - 1) * jitterFactor;

        // Apply jitter factor to the averageDelay
        var delayWithJitter = TimeSpan.FromTicks((long)(averageDelay.Ticks * (1 + jitter)));

        yield return delayWithJitter;
    }
}

Any additional info?

This would be useful for scenarios where we want to retry with some randomness but always around a specific interval.