Minor refactoring related to Circuit Breaker, Exponential Backoff and the Order controller

This commit is contained in:
Cesar De la Torre 2017-03-28 19:48:04 -07:00
parent 61df92872a
commit 2e5dca4467
2 changed files with 27 additions and 27 deletions

View File

@ -54,7 +54,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
} }
catch(BrokenCircuitException ex) catch(BrokenCircuitException ex)
{ {
ModelState.AddModelError("Error", "Not possible to create a new order, please try later on"); ModelState.AddModelError("Error", "It was not possible to create a new order, please try later on");
} }
return View(model); return View(model);
} }

View File

@ -12,9 +12,7 @@ namespace WebMVC.Services.Utilities
{ {
/// <summary> /// <summary>
/// HttpClient wrapper that integrates Retry and Circuit /// HttpClient wrapper that integrates Retry and Circuit
/// breaker policies when calling to Api services. /// breaker policies when invoking HTTP services.
/// Currently is ONLY implemented for the ASP MVC
/// and Xamarin App
/// </summary> /// </summary>
public class ResilientHttpClient : IHttpClient public class ResilientHttpClient : IHttpClient
{ {
@ -34,42 +32,44 @@ namespace WebMVC.Services.Utilities
); );
} }
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");
}
);
private Policy CreateRetryPolicy() => private Policy CreateRetryPolicy() =>
Policy.Handle<HttpRequestException>() Policy.Handle<HttpRequestException>()
.WaitAndRetryAsync( .WaitAndRetryAsync(
// number of retries // number of retries
5, 6,
// exponential backofff // exponential backofff
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)),
// on retry // on retry
(exception, timeSpan, retryCount, context) => (exception, timeSpan, retryCount, context) =>
{ {
_logger.LogTrace($"Retry {retryCount} " + var msg = $"Retry {retryCount} implemented with Polly's RetryPolicy " +
$"of {context.PolicyKey} " + $"of {context.PolicyKey} " +
$"at {context.ExecutionKey}, " + $"at {context.ExecutionKey}, " +
$"due to: {exception}."); $"due to: {exception}.";
_logger.LogWarning(msg);
_logger.LogDebug(msg);
} }
); );
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");
}
);
public Task<string> GetStringAsync(string uri) => public Task<string> GetStringAsync(string uri) =>
HttpInvoker(() => HttpInvoker(() =>
_client.GetStringAsync(uri)); _client.GetStringAsync(uri));