using System.Collections.Generic;
using Polly.Contrib.WaitAndRetry;
static void Main(string[] args)
TimeSpan maxDelay = TimeSpan.FromSeconds(30);
TimeSpan result = DefaultRetryDuration(retryAttempt, maxDelay);
Console.WriteLine($"Retry Attempt: {retryAttempt}");
Console.WriteLine($"Max Delay: {maxDelay}");
Console.WriteLine($"Retry Duration: {result}");
private static TimeSpan DefaultRetryDuration(int retryAttempt, TimeSpan maxDelay)
var medianFirstRetryDelay = TimeSpan.FromSeconds(Math.Pow(2, retryAttempt));
var backoffDurations = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: medianFirstRetryDelay, retryCount: retryAttempt);
var limitedDurations = backoffDurations.Select(s => TimeSpan.FromTicks(Math.Min(s.Ticks, maxDelay.Ticks))).OrderBy(s => s.Ticks).Take(retryAttempt + 1).ToList();
if (limitedDurations.Count() <= 0)
return medianFirstRetryDelay;
return limitedDurations.Last();
private static TimeSpan OldDefaultRetryDuration(int retryAttempt, TimeSpan maxDelay)
var medianFirstRetryDelay = TimeSpan.FromSeconds(Math.Pow(2, retryAttempt));
return Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: medianFirstRetryDelay, retryCount: retryAttempt).Select(s => TimeSpan.FromTicks(Math.Min(s.Ticks, maxDelay.Ticks))).OrderBy(s => s.Ticks).ElementAt(retryAttempt);