Skip to content

Commit

Permalink
Remove MaxAttempts for CalculateBackoffTime (#507)
Browse files Browse the repository at this point in the history
* replace << with Pow

* revert to use <<

* rename MaxAttempts to MaxSafeShiftBit

* rename to SafeShiftLimit
  • Loading branch information
zhiyuanliang-ms authored Jan 11, 2024
1 parent e9a3aa5 commit 5c9155b
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions
{
internal static class TimeSpanExtensions
{
private const int MaxAttempts = 63;
private const int SafeShiftLimit = 63;
private const double JitterRatio = 0.25;

private static readonly IList<KeyValuePair<TimeSpan, TimeSpan>> StartupMaxBackoffDurationIntervals = new List<KeyValuePair<TimeSpan, TimeSpan>>
Expand All @@ -30,6 +30,21 @@ internal static class TimeSpanExtensions
/// <returns>The calculated exponential backoff time, or <paramref name="interval"/> if it is less than <paramref name="min"/>.</returns>
public static TimeSpan CalculateBackoffTime(this TimeSpan interval, TimeSpan min, TimeSpan max, int attempts)
{
if (interval <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(interval), interval, "The time interval should not be equal to or less than 0.");
}

if (min <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(min), min, "The minimum backoff time should not be equal to or less than 0.");
}

if (max < min)
{
throw new ArgumentOutOfRangeException(nameof(max), max, "The maximum backoff time should not be less than the minimum backoff time.");
}

if (attempts < 1)
{
throw new ArgumentOutOfRangeException(nameof(attempts), attempts, "The number of attempts should not be less than 1.");
Expand Down Expand Up @@ -58,6 +73,16 @@ public static TimeSpan CalculateBackoffTime(this TimeSpan interval, TimeSpan min
/// </exception>
public static TimeSpan CalculateBackoffDuration(this TimeSpan minDuration, TimeSpan maxDuration, int attempts)
{
if (minDuration <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(minDuration), minDuration, "The minimum backoff time should not be equal to or less than 0.");
}

if (maxDuration < minDuration)
{
throw new ArgumentOutOfRangeException(nameof(maxDuration), maxDuration, "The maximum backoff time should not be less than the minimum backoff time.");
}

if (attempts < 1)
{
throw new ArgumentOutOfRangeException(nameof(attempts), attempts, "The number of attempts should not be less than 1.");
Expand All @@ -70,7 +95,7 @@ public static TimeSpan CalculateBackoffDuration(this TimeSpan minDuration, TimeS

//
// IMPORTANT: This can overflow
double calculatedMilliseconds = Math.Max(1, minDuration.TotalMilliseconds) * ((long)1 << Math.Min(attempts, MaxAttempts));
double calculatedMilliseconds = Math.Max(1, minDuration.TotalMilliseconds) * (1L << Math.Min(attempts, SafeShiftLimit));

if (calculatedMilliseconds > maxDuration.TotalMilliseconds ||
calculatedMilliseconds <= 0 /*overflow*/)
Expand Down

0 comments on commit 5c9155b

Please sign in to comment.