| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using Primitives; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// RetryPolicy implementation where the delay between retries will grow in a staggered exponential manner. |
| | 11 | | /// RetryIntervals will be computed using a retryFactor which is a function of deltaBackOff (MaximumBackoff - Minimu |
| | 12 | | /// <remarks>RetryPolicy will not be applied when an ambient transaction is found.</remarks> |
| | 13 | | /// </summary> |
| | 14 | | public sealed class RetryExponential : RetryPolicy |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Returns a new RetryExponential retry policy object. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="minimumBackoff">Minimum backoff interval.</param> |
| | 20 | | /// <param name="maximumBackoff">Maximum backoff interval.</param> |
| | 21 | | public RetryExponential(TimeSpan minimumBackoff, TimeSpan maximumBackoff, int maximumRetryCount) |
| 84 | 22 | | : this(minimumBackoff, maximumBackoff, Constants.DefaultRetryDeltaBackoff, maximumRetryCount) |
| | 23 | | { |
| 84 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Returns a new RetryExponential retry policy object. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="minimumBackoff">Minimum backoff interval.</param> |
| | 30 | | /// <param name="maximumBackoff">Maximum backoff interval.</param> |
| | 31 | | /// <param name="deltaBackoff">Delta backoff interval.</param> |
| 84 | 32 | | public RetryExponential(TimeSpan minimumBackoff, TimeSpan maximumBackoff, TimeSpan deltaBackoff, int maxRetryCou |
| | 33 | | { |
| 84 | 34 | | TimeoutHelper.ThrowIfNonPositiveArgument(deltaBackoff, nameof(deltaBackoff)); |
| 84 | 35 | | TimeoutHelper.ThrowIfNegativeArgument(minimumBackoff, nameof(minimumBackoff)); |
| 84 | 36 | | TimeoutHelper.ThrowIfNonPositiveArgument(maximumBackoff, nameof(maximumBackoff)); |
| | 37 | |
|
| 84 | 38 | | if (maxRetryCount <= 0) |
| | 39 | | { |
| 0 | 40 | | throw new ArgumentOutOfRangeException( |
| 0 | 41 | | nameof(maxRetryCount), |
| 0 | 42 | | Resources.ArgumentMustBePositive.FormatForUser(nameof(maxRetryCount))); |
| | 43 | | } |
| | 44 | |
|
| 84 | 45 | | if (minimumBackoff >= maximumBackoff) |
| | 46 | | { |
| 0 | 47 | | throw new ArgumentException(Resources.ExponentialRetryBackoffRange.FormatForUser(minimumBackoff, maximum |
| | 48 | | } |
| | 49 | |
|
| 84 | 50 | | this.MinimalBackoff = minimumBackoff; |
| 84 | 51 | | this.MaximumBackoff = maximumBackoff; |
| 84 | 52 | | this.DeltaBackoff = deltaBackoff; |
| 84 | 53 | | this.MaxRetryCount = maxRetryCount; |
| 84 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Minimum backoff interval. |
| | 58 | | /// </summary> |
| | 59 | | /// <value>The minimum backoff interval.</value> |
| 68 | 60 | | public TimeSpan MinimalBackoff { get; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets or sets the maximum backoff interval. |
| | 64 | | /// </summary> |
| | 65 | | /// <value>The maximum backoff interval.</value> |
| 68 | 66 | | public TimeSpan MaximumBackoff { get; } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Gets or sets the backoff interval associated with the retry. |
| | 70 | | /// </summary> |
| | 71 | | /// <value>The backoff interval associated with the retry.</value> |
| 136 | 72 | | public TimeSpan DeltaBackoff { get; } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Gets or sets the maximum number of allowed retries. |
| | 76 | | /// </summary> |
| | 77 | | /// <value>The maximum number of allowed retries.</value> |
| 72 | 78 | | public int MaxRetryCount { get; } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Called to see if a retry should be performed. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="remainingTime">The remaining time before the timeout expires.</param> |
| | 84 | | /// <param name="currentRetryCount">The number of attempts that have been processed.</param> |
| | 85 | | /// <param name="retryInterval">The amount of time to delay before retry.</param> |
| | 86 | | protected override bool OnShouldRetry(TimeSpan remainingTime, int currentRetryCount, out TimeSpan retryInterval) |
| | 87 | | { |
| 72 | 88 | | if (currentRetryCount > this.MaxRetryCount) |
| | 89 | | { |
| 4 | 90 | | retryInterval = TimeSpan.Zero; |
| 4 | 91 | | return false; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | // Logic: - first use currentRetryCount to calculate the size of the interval. |
| | 95 | | // - then get the interval in terms of sleep time (between min and max sleep time) |
| | 96 | | // - if interval to large to fit inside remainingTime, we quit. |
| 68 | 97 | | var randomizedInterval = ConcurrentRandom.Next((int)(this.DeltaBackoff.TotalMilliseconds * 0.8), (int)(this. |
| 68 | 98 | | double increment = (Math.Pow(2, currentRetryCount) - 1) * randomizedInterval; |
| 68 | 99 | | double timeToSleepMsec = Math.Min(this.MinimalBackoff.TotalMilliseconds + increment, this.MaximumBackoff.Tot |
| 68 | 100 | | retryInterval = TimeSpan.FromMilliseconds(timeToSleepMsec); |
| | 101 | |
|
| 68 | 102 | | return true; |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |