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

DecorrelatedJitterBackoffV2 without exponential side

Open andremantaswow opened this issue 1 year ago • 0 comments

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.

andremantaswow avatar Jul 03 '24 19:07 andremantaswow