< Summary

Class:Microsoft.Azure.ServiceBus.RetryExponential
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\RetryExponential.cs
Covered lines:25
Uncovered lines:4
Coverable lines:29
Total lines:105
Line coverage:86.2% (25 of 29)
Covered branches:4
Total branches:6
Branch coverage:66.6% (4 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
.ctor(...)-73.33%50%
get_MinimalBackoff()-100%100%
get_MaximumBackoff()-100%100%
get_DeltaBackoff()-100%100%
get_MaxRetryCount()-100%100%
OnShouldRetry(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\RetryExponential.cs

#LineLine coverage
 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
 4namespace 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)
 8422            : this(minimumBackoff, maximumBackoff, Constants.DefaultRetryDeltaBackoff, maximumRetryCount)
 23        {
 8424        }
 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>
 8432        public RetryExponential(TimeSpan minimumBackoff, TimeSpan maximumBackoff, TimeSpan deltaBackoff, int maxRetryCou
 33        {
 8434            TimeoutHelper.ThrowIfNonPositiveArgument(deltaBackoff, nameof(deltaBackoff));
 8435            TimeoutHelper.ThrowIfNegativeArgument(minimumBackoff, nameof(minimumBackoff));
 8436            TimeoutHelper.ThrowIfNonPositiveArgument(maximumBackoff, nameof(maximumBackoff));
 37
 8438            if (maxRetryCount <= 0)
 39            {
 040                throw new ArgumentOutOfRangeException(
 041                    nameof(maxRetryCount),
 042                    Resources.ArgumentMustBePositive.FormatForUser(nameof(maxRetryCount)));
 43            }
 44
 8445            if (minimumBackoff >= maximumBackoff)
 46            {
 047                throw new ArgumentException(Resources.ExponentialRetryBackoffRange.FormatForUser(minimumBackoff, maximum
 48            }
 49
 8450            this.MinimalBackoff = minimumBackoff;
 8451            this.MaximumBackoff = maximumBackoff;
 8452            this.DeltaBackoff = deltaBackoff;
 8453            this.MaxRetryCount = maxRetryCount;
 8454        }
 55
 56        /// <summary>
 57        /// Minimum backoff interval.
 58        /// </summary>
 59        /// <value>The minimum backoff interval.</value>
 6860        public TimeSpan MinimalBackoff { get; }
 61
 62        /// <summary>
 63        /// Gets or sets the maximum backoff interval.
 64        /// </summary>
 65        /// <value>The maximum backoff interval.</value>
 6866        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>
 13672        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>
 7278        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        {
 7288            if (currentRetryCount > this.MaxRetryCount)
 89            {
 490                retryInterval = TimeSpan.Zero;
 491                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.
 6897            var randomizedInterval = ConcurrentRandom.Next((int)(this.DeltaBackoff.TotalMilliseconds * 0.8), (int)(this.
 6898            double increment = (Math.Pow(2, currentRetryCount) - 1) * randomizedInterval;
 6899            double timeToSleepMsec = Math.Min(this.MinimalBackoff.TotalMilliseconds + increment, this.MaximumBackoff.Tot
 68100            retryInterval = TimeSpan.FromMilliseconds(timeToSleepMsec);
 101
 68102            return true;
 103        }
 104    }
 105}