2017-03-27 14:05:28 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Polly;
|
|
|
|
|
using Polly.Wrap;
|
|
|
|
|
using System;
|
2017-03-27 14:05:28 +02:00
|
|
|
|
using System.Net;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WebMVC.Services.Utilities
|
|
|
|
|
{
|
2017-03-22 12:24:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// HttpClient wrapper that integrates Retry and Circuit
|
2017-03-28 19:48:04 -07:00
|
|
|
|
/// breaker policies when invoking HTTP services.
|
2017-03-22 12:24:55 +01:00
|
|
|
|
/// </summary>
|
2017-03-28 14:30:30 -07:00
|
|
|
|
public class ResilientHttpClient : IHttpClient
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
|
|
|
|
private HttpClient _client;
|
|
|
|
|
private PolicyWrap _policyWrapper;
|
2017-03-29 11:20:33 +02:00
|
|
|
|
private ILogger<ResilientHttpClient> _logger;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
public HttpClient Inst => _client;
|
2017-03-29 11:20:33 +02:00
|
|
|
|
public ResilientHttpClient(ILogger<ResilientHttpClient> logger)
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
|
|
|
|
_client = new HttpClient();
|
2017-03-29 11:20:33 +02:00
|
|
|
|
_logger = logger;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
// Add Policies to be applied
|
|
|
|
|
_policyWrapper = Policy.WrapAsync(
|
|
|
|
|
CreateRetryPolicy(),
|
|
|
|
|
CreateCircuitBreakerPolicy()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
private Policy CreateRetryPolicy() =>
|
|
|
|
|
Policy.Handle<HttpRequestException>()
|
|
|
|
|
.WaitAndRetryAsync(
|
|
|
|
|
// number of retries
|
2017-03-28 19:48:04 -07:00
|
|
|
|
6,
|
2017-03-20 14:18:20 -04:00
|
|
|
|
// exponential backofff
|
2017-03-28 23:58:04 -07:00
|
|
|
|
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
|
2017-03-20 14:18:20 -04:00
|
|
|
|
// on retry
|
2017-03-22 12:24:55 +01:00
|
|
|
|
(exception, timeSpan, retryCount, context) =>
|
2017-03-20 14:18:20 -04:00
|
|
|
|
{
|
2017-03-28 19:48:04 -07:00
|
|
|
|
var msg = $"Retry {retryCount} implemented with Polly's RetryPolicy " +
|
2017-03-20 14:18:20 -04:00
|
|
|
|
$"of {context.PolicyKey} " +
|
|
|
|
|
$"at {context.ExecutionKey}, " +
|
2017-03-28 19:48:04 -07:00
|
|
|
|
$"due to: {exception}.";
|
|
|
|
|
_logger.LogWarning(msg);
|
|
|
|
|
_logger.LogDebug(msg);
|
2017-03-20 14:18:20 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
2017-03-28 19:48:04 -07:00
|
|
|
|
private Policy CreateCircuitBreakerPolicy() =>
|
|
|
|
|
Policy.Handle<HttpRequestException>()
|
|
|
|
|
.CircuitBreakerAsync(
|
|
|
|
|
// number of exceptions before breaking circuit
|
|
|
|
|
5,
|
|
|
|
|
// time circuit opened before retry
|
|
|
|
|
TimeSpan.FromMinutes(1),
|
|
|
|
|
(exception, duration) =>
|
|
|
|
|
{
|
|
|
|
|
// on circuit opened
|
|
|
|
|
_logger.LogTrace("Circuit breaker opened");
|
|
|
|
|
},
|
|
|
|
|
() =>
|
|
|
|
|
{
|
|
|
|
|
// on circuit closed
|
|
|
|
|
_logger.LogTrace("Circuit breaker reset");
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
public Task<string> GetStringAsync(string uri) =>
|
2017-03-22 12:24:55 +01:00
|
|
|
|
HttpInvoker(() =>
|
|
|
|
|
_client.GetStringAsync(uri));
|
2017-03-20 14:18:20 -04:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseMessage> PostAsync<T>(string uri, T item) =>
|
2017-03-17 10:00:18 +01:00
|
|
|
|
// a new StringContent must be created for each retry
|
|
|
|
|
// as it is disposed after each call
|
2017-03-22 12:24:55 +01:00
|
|
|
|
HttpInvoker(() =>
|
2017-03-27 14:05:28 +02:00
|
|
|
|
{
|
|
|
|
|
var response = _client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json"));
|
|
|
|
|
// raise exception if HttpResponseCode 500
|
|
|
|
|
// needed for circuit breaker to track fails
|
|
|
|
|
if (response.Result.StatusCode == HttpStatusCode.InternalServerError)
|
2017-03-22 12:24:55 +01:00
|
|
|
|
{
|
2017-03-27 14:05:28 +02:00
|
|
|
|
throw new HttpRequestException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
});
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
public Task<HttpResponseMessage> DeleteAsync(string uri) =>
|
|
|
|
|
HttpInvoker(() => _client.DeleteAsync(uri));
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
|
|
|
|
|
private Task<T> HttpInvoker<T>(Func<Task<T>> action) =>
|
2017-03-17 10:00:18 +01:00
|
|
|
|
// Executes the action applying all
|
|
|
|
|
// the policies defined in the wrapper
|
2017-03-20 14:18:20 -04:00
|
|
|
|
_policyWrapper.ExecuteAsync(() => action());
|
2017-03-17 10:00:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|