2017-04-07 12:48:22 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Polly;
|
|
|
|
|
using Polly.Wrap;
|
|
|
|
|
using System;
|
2017-03-31 14:20:03 +02:00
|
|
|
|
using System.Collections.Generic;
|
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;
|
|
|
|
|
|
2017-04-06 16:59:17 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
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-29 13:16:00 -07:00
|
|
|
|
/// Based on Polly library: https://github.com/App-vNext/Polly
|
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-31 14:20:03 +02:00
|
|
|
|
|
2017-04-07 12:48:22 +02:00
|
|
|
|
public ResilientHttpClient(Policy[] policies, 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
|
2017-04-07 12:48:22 +02:00
|
|
|
|
_policyWrapper = Policy.WrapAsync(policies);
|
|
|
|
|
}
|
2017-03-28 19:48:04 -07:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|