| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | | 3 | | |
| | | 4 | | namespace Microsoft.Azure.Batch.Common |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a retry policy that performs a specified number of retries, using a specified fixed time interval bet |
| | | 11 | | /// </summary> |
| | | 12 | | public class LinearRetry : IRetryPolicy |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the backoff interval between retries. |
| | | 16 | | /// </summary> |
| | 36 | 17 | | public TimeSpan DeltaBackoff { get; private set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the maximum number of retry attempts. |
| | | 21 | | /// </summary> |
| | 42 | 22 | | public int MaximumRetries { get; private set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="LinearRetry"/> class using the specified delta and maximum numb |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="deltaBackoff">The backoff interval between retries.</param> |
| | | 28 | | /// <param name="maxRetries">The maximum number of retry attempts.</param> |
| | 13 | 29 | | public LinearRetry(TimeSpan deltaBackoff, int maxRetries) |
| | | 30 | | { |
| | 13 | 31 | | RetryPolicyCommon.ValidateArguments(deltaBackoff, maxRetries); |
| | | 32 | | |
| | 11 | 33 | | this.DeltaBackoff = deltaBackoff; |
| | 11 | 34 | | this.MaximumRetries = maxRetries; |
| | 11 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Determines if the operation should be retried and how long to wait until the next retry. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="exception">An <see cref="Exception"/> object that represents the last exception encountered.</p |
| | | 41 | | /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.< |
| | | 42 | | /// <returns>A <see cref="RetryDecision"/> object which specifies if a retry should be performed.</returns> |
| | | 43 | | public Task<RetryDecision> ShouldRetryAsync(Exception exception, OperationContext operationContext) |
| | | 44 | | { |
| | 30 | 45 | | if (operationContext == null) |
| | | 46 | | { |
| | 0 | 47 | | throw new ArgumentNullException("operationContext"); |
| | | 48 | | } |
| | | 49 | | |
| | 30 | 50 | | bool shouldRetry = RetryPolicyCommon.ShouldRetry(exception, operationContext, this.MaximumRetries); |
| | | 51 | | |
| | | 52 | | RetryDecision decision; |
| | 30 | 53 | | if (shouldRetry) |
| | | 54 | | { |
| | 24 | 55 | | TimeSpan? retryAfter = (exception as BatchException)?.RequestInformation?.RetryAfter; |
| | 24 | 56 | | var delay = retryAfter.HasValue && this.DeltaBackoff > retryAfter ? retryAfter.Value : this.DeltaBackoff |
| | | 57 | | |
| | 24 | 58 | | decision = RetryDecision.RetryWithDelay(delay); |
| | | 59 | | } |
| | | 60 | | else |
| | | 61 | | { |
| | 6 | 62 | | decision = RetryDecision.NoRetry; |
| | | 63 | | } |
| | | 64 | | |
| | 30 | 65 | | return Task.FromResult(decision); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |