|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace NLU.DevOps.Luis |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Globalization; |
| 8 | + using System.Linq; |
| 9 | + using System.Net; |
| 10 | + using System.Text.RegularExpressions; |
| 11 | + using System.Threading; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Microsoft.Azure.CognitiveServices.Language.LUIS.Authoring.Models; |
| 14 | +#if LUIS_V2 |
| 15 | + using ErrorException = Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime.Models.APIErrorException; |
| 16 | +#else |
| 17 | + using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime.Models; |
| 18 | +#endif |
| 19 | + |
| 20 | + internal static class Retry |
| 21 | + { |
| 22 | + public const string RetryAfterHeader = "Retry-After"; |
| 23 | + |
| 24 | + private static readonly Regex RetryAfterSecondsRegex = new Regex(@"^\d+$"); |
| 25 | + |
| 26 | + private static TimeSpan DefaultTransientDelay { get; } = TimeSpan.FromMilliseconds(100); |
| 27 | + |
| 28 | + public static TimeSpan GetRetryAfterDelay(string retryAfter, TimeSpan? defaultDelay = default) |
| 29 | + { |
| 30 | + if (retryAfter == null) |
| 31 | + { |
| 32 | + return defaultDelay ?? DefaultTransientDelay; |
| 33 | + } |
| 34 | + |
| 35 | + if (RetryAfterSecondsRegex.IsMatch(retryAfter)) |
| 36 | + { |
| 37 | + return TimeSpan.FromSeconds(int.Parse(retryAfter, CultureInfo.InvariantCulture)); |
| 38 | + } |
| 39 | + |
| 40 | + return DateTimeOffset.Parse(retryAfter, CultureInfo.InvariantCulture) - DateTimeOffset.Now; |
| 41 | + } |
| 42 | + |
| 43 | + public static CancellationTokenHolder With(CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + return new CancellationTokenHolder(cancellationToken); |
| 46 | + } |
| 47 | + |
| 48 | + private static async Task<TResult> OnTransientExceptionAsync<TResult, TException>( |
| 49 | + Func<Task<TResult>> func, |
| 50 | + Func<TException, HttpStatusCode> statusCodeSelector, |
| 51 | + Func<TException, string> retryAfterDelaySelector = default, |
| 52 | + int retryCount = int.MaxValue, |
| 53 | + CancellationToken cancellationToken = default) |
| 54 | + where TException : Exception |
| 55 | + { |
| 56 | + var count = 0; |
| 57 | + while (count++ < retryCount) |
| 58 | + { |
| 59 | + cancellationToken.ThrowIfCancellationRequested(); |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + return await func().ConfigureAwait(false); |
| 64 | + } |
| 65 | + catch (TException ex) |
| 66 | + when (count < retryCount && IsTransientStatusCode(statusCodeSelector(ex))) |
| 67 | + { |
| 68 | + var delay = GetRetryAfterDelay(retryAfterDelaySelector?.Invoke(ex)); |
| 69 | + await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + throw new InvalidOperationException("Exception will be rethrown before reaching this point."); |
| 74 | + } |
| 75 | + |
| 76 | + private static bool IsTransientStatusCode(HttpStatusCode statusCode) |
| 77 | + { |
| 78 | + return statusCode == HttpStatusCode.TooManyRequests |
| 79 | + || (statusCode >= HttpStatusCode.InternalServerError |
| 80 | + && statusCode != HttpStatusCode.HttpVersionNotSupported |
| 81 | + && statusCode != HttpStatusCode.NotImplemented); |
| 82 | + } |
| 83 | + |
| 84 | + public class CancellationTokenHolder |
| 85 | + { |
| 86 | + public CancellationTokenHolder(CancellationToken cancellationToken) |
| 87 | + { |
| 88 | + this.CancellationToken = cancellationToken; |
| 89 | + } |
| 90 | + |
| 91 | + private CancellationToken CancellationToken { get; } |
| 92 | + |
| 93 | + public Task<T> OnTransientErrorAsync<T>(Func<Task<T>> func) |
| 94 | + { |
| 95 | + return OnTransientExceptionAsync( |
| 96 | + func, |
| 97 | + (ErrorException ex) => ex.Response.StatusCode, |
| 98 | + (ErrorException ex) => ex.Response.Headers?[RetryAfterHeader]?.FirstOrDefault(), |
| 99 | + cancellationToken: this.CancellationToken); |
| 100 | + } |
| 101 | + |
| 102 | + public Task<T> OnTransientErrorResponseAsync<T>(Func<Task<T>> func) |
| 103 | + { |
| 104 | + return OnTransientExceptionAsync( |
| 105 | + func, |
| 106 | + (ErrorResponseException ex) => ex.Response.StatusCode, |
| 107 | + (ErrorResponseException ex) => ex.Response.Headers?[RetryAfterHeader]?.FirstOrDefault(), |
| 108 | + cancellationToken: this.CancellationToken); |
| 109 | + } |
| 110 | + |
| 111 | + public Task<T> OnTransientWebExceptionAsync<T>(Func<Task<T>> func) |
| 112 | + { |
| 113 | + return OnTransientExceptionAsync( |
| 114 | + func, |
| 115 | + (WebException ex) => (ex.Response as HttpWebResponse)?.StatusCode ?? default, |
| 116 | + (WebException ex) => (ex.Response as HttpWebResponse)?.Headers?[RetryAfterHeader], |
| 117 | + cancellationToken: this.CancellationToken); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments