| | | 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 | | |
| | | 8 | | internal static class RetryPolicyCommon |
| | | 9 | | { |
| | | 10 | | internal static bool ShouldRetry(Exception exception, OperationContext operationContext, int maxRetries) |
| | | 11 | | { |
| | | 12 | | bool shouldRetry; |
| | 56 | 13 | | int currentRetryCount = operationContext.RequestResults.Count - 1; |
| | | 14 | | |
| | 56 | 15 | | if (currentRetryCount < maxRetries) |
| | | 16 | | { |
| | 47 | 17 | | BatchException batchException = exception as BatchException; |
| | 47 | 18 | | if (batchException != null) |
| | | 19 | | { |
| | 4 | 20 | | int statusCode = (int)batchException.RequestInformation.HttpStatusCode; |
| | | 21 | | |
| | 4 | 22 | | if ((statusCode >= 400 && statusCode < 500 |
| | 4 | 23 | | && statusCode != 408 // Timeout |
| | 4 | 24 | | && statusCode != 429 // Too many reqeuests |
| | 4 | 25 | | ) |
| | 4 | 26 | | || statusCode == 501 // Not Implemented |
| | 4 | 27 | | || statusCode == 505 // Version Not Supported |
| | 4 | 28 | | ) |
| | | 29 | | { |
| | 0 | 30 | | shouldRetry = false; |
| | | 31 | | } |
| | | 32 | | else |
| | | 33 | | { |
| | 4 | 34 | | shouldRetry = true; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | else |
| | | 38 | | { |
| | | 39 | | //Note that we use a disallow list here in order to have the most robust retry policy -- |
| | | 40 | | //attempting to guess all the possible exception types thrown by the network stack to craft a |
| | | 41 | | //white list is error prone and so we avoid it. |
| | 43 | 42 | | if (exception is Microsoft.Rest.ValidationException) |
| | | 43 | | { |
| | | 44 | | //TODO: Consider adding SystemException to disallow list, but SystemException includes TimeoutEx |
| | 2 | 45 | | shouldRetry = false; |
| | | 46 | | } |
| | | 47 | | else |
| | | 48 | | { |
| | | 49 | | //Retry on all other exceptions which aren't a BatchException since those exceptions |
| | | 50 | | //mean we haven't heard back from the server |
| | 41 | 51 | | shouldRetry = true; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | else |
| | | 56 | | { |
| | 9 | 57 | | shouldRetry = false; //We are out of retries to perform so we will not retry |
| | | 58 | | } |
| | | 59 | | |
| | 56 | 60 | | return shouldRetry; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal static void ValidateArguments(TimeSpan deltaBackoff, int maxRetries) |
| | | 64 | | { |
| | 678 | 65 | | if (deltaBackoff < TimeSpan.Zero) |
| | | 66 | | { |
| | 2 | 67 | | throw new ArgumentOutOfRangeException(nameof(deltaBackoff)); |
| | | 68 | | } |
| | | 69 | | |
| | 676 | 70 | | if (maxRetries < 0) |
| | | 71 | | { |
| | 2 | 72 | | throw new ArgumentOutOfRangeException(nameof(maxRetries)); |
| | | 73 | | } |
| | 674 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |