| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | |
| | | 7 | | namespace Azure.Core |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// A type that analyzes HTTP responses and exceptions and determines if they should be retried. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ResponseClassifier |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Specifies if the request contained in the <paramref name="message"/> should be retried. |
| | | 16 | | /// </summary> |
| | | 17 | | public virtual bool IsRetriableResponse(HttpMessage message) |
| | | 18 | | { |
| | 414 | 19 | | switch (message.Response.Status) |
| | | 20 | | { |
| | | 21 | | case 408: // Request Timeout |
| | | 22 | | case 429: // Too Many Requests |
| | | 23 | | case 500: // Internal Server Error |
| | | 24 | | case 502: // Bad Gateway |
| | | 25 | | case 503: // Service Unavailable |
| | | 26 | | case 504: // Gateway Timeout |
| | 414 | 27 | | return true; |
| | | 28 | | default: |
| | 0 | 29 | | return false; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Specifies if the operation that caused the exception should be retried. |
| | | 35 | | /// </summary> |
| | | 36 | | public virtual bool IsRetriableException(Exception exception) |
| | | 37 | | { |
| | 66 | 38 | | return (exception is IOException) || |
| | 66 | 39 | | (exception is RequestFailedException requestFailed && requestFailed.Status == 0); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Specifies if the operation that caused the exception should be retried taking the <see cref="HttpMessage"/> |
| | | 44 | | /// </summary> |
| | | 45 | | public virtual bool IsRetriable(HttpMessage message, Exception exception) |
| | | 46 | | { |
| | 74 | 47 | | return IsRetriableException(exception) || |
| | 74 | 48 | | // Retry non-user initiated cancellations |
| | 74 | 49 | | (exception is OperationCanceledException && !message.CancellationToken.IsCancellationRequested); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Specifies if the response contained in the <paramref name="message"/> is not successful. |
| | | 54 | | /// </summary> |
| | | 55 | | public virtual bool IsErrorResponse(HttpMessage message) |
| | | 56 | | { |
| | 4856 | 57 | | var statusKind = message.Response.Status / 100; |
| | 4856 | 58 | | return statusKind == 4 || statusKind == 5; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | } |