/// <summary> /// Send a request to the specified endpoint. /// Make a request to get an access token if required. /// </summary> /// <typeparam name="TR"></typeparam> /// <typeparam name="TD"></typeparam> /// <param name="url"></param> /// <param name="method"></param> /// <param name="data"></param> /// <returns></returns> private async Task <OrderWrapper <OrderParent <TR> > > SendOrderAsync <TR, TD>(string url, HttpMethod method, TD data) where TR : IFieldedData where TD : class { var orderProcessingPolicy = Policy .HandleResult <OrderWrapper <OrderParent <TR> > >(result => IsResponseMissingJsonAndProcessing(result)) .WaitAndRetryAsync(Options.MaxRetries, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); try { var response = await _authPolicy.ExecuteAsync(async() => await this.Client.SendJsonAsync <OrderWrapper <OrderParent <TR> >, TD>(url, method, data)); if (IsResponseMissingJsonAndProcessing(response)) { response = await _authPolicy.WrapAsync(orderProcessingPolicy).ExecuteAsync(async() => await GetOrderById <TR>(response?.Order?.OrderId)); if (IsResponseMissingJsonAndProcessing(response)) { throw new LtsaException("Request timed out waiting for ltsa response", HttpStatusCode.RequestTimeout); } } return(response); } catch (HttpClientRequestException ex) { Error error = await GetLtsaError(ex, url); throw new LtsaException(ex, this.Client, error); } }
public async Task <ProducerResult> ProduceCommandWithRetryAsync <T>(T message) { try { return(await retryPolicy .WrapAsync(circuitBreakerPolicy) .ExecuteAsync(async() => { EventData eventData = CreateEventData(message); await MessagesCollector.AddAsync(eventData); return new ProducerResult { Message = eventData }; })); } catch (Exception ex) { Logger.LogError(string.Format(ConstantStrings.ProducerErrorPosting, ex.Message)); return(new ProducerResult { Valid = false }); } }
public RetryingHandler(TimeSpan timeout, TimeSpan uploadTimeout, int maxRetries, Func <int, DelegateResult <HttpResponseMessage>, Context, TimeSpan> sleepDurationProvider) { AsyncRetryPolicy <HttpResponseMessage> retryAfterPolicy = Policy .HandleResult <HttpResponseMessage>(ContainsRetryAfterHeader) .WaitAndRetryAsync(maxRetries, sleepDurationProvider, (outcome, timespan, retryCount, context) => Task.CompletedTask); AsyncRetryPolicy <HttpResponseMessage> httpRetryPolicy = Policy .Handle <HttpRequestException>() .Or <TaskCanceledException>() .Or <TimeoutRejectedException>() .OrResult <HttpResponseMessage>(RetryRequired) .WaitAndRetryAsync(GetBackOff(maxRetries)); AsyncPolicyWrap <HttpResponseMessage> baseRetryPolicy = retryAfterPolicy.WrapAsync(httpRetryPolicy); _policy = baseRetryPolicy.WrapAsync(Policy.TimeoutAsync(timeout)); _fileUploadPolicy = baseRetryPolicy.WrapAsync(Policy.TimeoutAsync(uploadTimeout)); }
public CatalogController(IHttpClientFactory clientFactory) { _httpClientFactory = clientFactory; _httpRetryPolicy = Policy.HandleResult <HttpResponseMessage>(result => result.StatusCode == System.Net.HttpStatusCode.InternalServerError) .Or <TimeoutRejectedException>() .RetryAsync(3); _httpWaitAndRetryPolicy = Policy.HandleResult <HttpResponseMessage>(result => !result.IsSuccessStatusCode) .WaitAndRetryAsync(3, sleepDurationProvider: retryCount => TimeSpan.FromSeconds(Math.Pow(2, retryCount) / 2), onRetry: (httpResponseMessage, time, context) => { if (httpResponseMessage.Result.StatusCode == System.Net.HttpStatusCode.Unauthorized) { Debug.WriteLine("There was issue with authentication"); } else { if (context.Contains("Logging") && context["Logging"].ToString() == "Enabled") { Debug.WriteLine(httpResponseMessage.Result.Content.ReadAsStringAsync().Result); } } }); _fallbackPolicy = Policy.HandleResult <HttpResponseMessage>(result => !result.IsSuccessStatusCode) .FallbackAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent("Cached Default Result") }); _timeoutPolicy = Policy.TimeoutAsync(seconds: 1); _policyWrap = _fallbackPolicy.WrapAsync(_httpWaitAndRetryPolicy.WrapAsync(_timeoutPolicy)); }
public PolicyProvider() { IdPSearchServiceRetryPolicy = _idPSearchServicePolicyBuilder.RetryAsync(3); IdPSearchServiceErrorPolicy = _idPSearchServicePolicyBuilder.CircuitBreakerAsync(3, TimeSpan.FromMinutes(5)); IdPSearchServicePolicy = IdPSearchServiceRetryPolicy.WrapAsync(IdPSearchServiceErrorPolicy); }
/// <summary> /// Policy Resiliance Strategy /// </summary> /// <returns></returns> public AsyncPolicy GetResillianceStrategy() { return(_circuitBreakerPolicy.WrapAsync(_retryPolicy.WrapAsync(_bulkheadPolicy))); }
/// <summary> /// Do an api request with resiliance policy thats will retry unitl max attemps /// </summary> /// <typeparam name="T">Result type</typeparam> /// <param name="func">Api request function</param> /// <returns>Request result or ApiException</returns> protected virtual async Task <T> RequestWithPolicy <T>(Func <Task <T> > func) => await _retryPolicy.WrapAsync(_refreshTokenPolicy).ExecuteAsync(func).ConfigureAwait(false);