| | 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 | | |
| | 5 | | namespace Microsoft.Azure.Batch |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using Common; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// A RequestInterceptor that sets the RetryPolicy. |
| | 12 | | /// </summary> |
| | 13 | | public class RetryPolicyProvider : Protocol.RequestInterceptor |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets or sets the retry policy to use. |
| | 17 | | /// </summary> |
| 689 | 18 | | public IRetryPolicy Policy { get; set; } |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Creates a new <see cref="RetryPolicyProvider"/> using the <see cref="NoRetry"/> policy. |
| | 22 | | /// </summary> |
| | 23 | | /// <returns>A provider configured to perform no retries.</returns> |
| | 24 | | public static RetryPolicyProvider NoRetryProvider() |
| | 25 | | { |
| 0 | 26 | | return new RetryPolicyProvider(new NoRetry()); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Creates a new <see cref="RetryPolicyProvider"/> using the <see cref="LinearRetry"/> policy. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="deltaBackoff">The backoff interval between retries.</param> |
| | 33 | | /// <param name="maxRetries">The maximum number of retry attempts.</param> |
| | 34 | | /// <returns>A provider configured to perform linear retries using the specified backoff and max retries.</retur |
| | 35 | | public static RetryPolicyProvider LinearRetryProvider(TimeSpan deltaBackoff, int maxRetries) |
| | 36 | | { |
| 0 | 37 | | return new RetryPolicyProvider(new LinearRetry(deltaBackoff, maxRetries)); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Creates a new <see cref="RetryPolicyProvider"/> using the <see cref="ExponentialRetry"/> policy. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="deltaBackoff">The backoff interval between retries, where the resulting backoff is 2^n * deltaB |
| | 44 | | /// <param name="maxRetries">The maximum number of retry attempts.</param> |
| | 45 | | /// <param name="maxBackoff">The maximum amount of time to back off between attempts.</param> |
| | 46 | | /// <returns>A provider configured to perform exponential retries using the specified backoff and max retries.</ |
| | 47 | | public static RetryPolicyProvider ExponentialRetryProvider(TimeSpan deltaBackoff, int maxRetries, TimeSpan? maxB |
| | 48 | | { |
| 653 | 49 | | return new RetryPolicyProvider(new ExponentialRetry(deltaBackoff, maxRetries, maxBackoff)); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Initializes a new behavior to set the retry policy. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="retryPolicy">The retry policy to set.</param> |
| 659 | 56 | | public RetryPolicyProvider(IRetryPolicy retryPolicy) |
| | 57 | | { |
| 659 | 58 | | this.Policy = retryPolicy; |
| | 59 | |
|
| 659 | 60 | | base.ModificationInterceptHandler = SetRetryPolicyInterceptor; |
| 659 | 61 | | } |
| | 62 | |
|
| | 63 | | private void SetRetryPolicyInterceptor(Protocol.IBatchRequest request) |
| | 64 | | { |
| 19 | 65 | | if (null != this.Policy) |
| | 66 | | { |
| 19 | 67 | | request.RetryPolicy = this.Policy; |
| | 68 | | } |
| 19 | 69 | | } |
| | 70 | | } |
| | 71 | | } |