Minor refactoring related to Circuit Breaker, Exponential Backoff and the Order controller
This commit is contained in:
parent
61df92872a
commit
2e5dca4467
@ -54,7 +54,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
@ -12,9 +12,7 @@ namespace WebMVC.Services.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// HttpClient wrapper that integrates Retry and Circuit
|
||||
/// breaker policies when calling to Api services.
|
||||
/// Currently is ONLY implemented for the ASP MVC
|
||||
/// and Xamarin App
|
||||
/// breaker policies when invoking HTTP services.
|
||||
/// </summary>
|
||||
public class ResilientHttpClient : IHttpClient
|
||||
{
|
||||
@ -34,6 +32,25 @@ namespace WebMVC.Services.Utilities
|
||||
);
|
||||
}
|
||||
|
||||
private Policy CreateRetryPolicy() =>
|
||||
Policy.Handle<HttpRequestException>()
|
||||
.WaitAndRetryAsync(
|
||||
// number of retries
|
||||
6,
|
||||
// exponential backofff
|
||||
retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)),
|
||||
// on retry
|
||||
(exception, timeSpan, retryCount, context) =>
|
||||
{
|
||||
var msg = $"Retry {retryCount} implemented with Polly's RetryPolicy " +
|
||||
$"of {context.PolicyKey} " +
|
||||
$"at {context.ExecutionKey}, " +
|
||||
$"due to: {exception}.";
|
||||
_logger.LogWarning(msg);
|
||||
_logger.LogDebug(msg);
|
||||
}
|
||||
);
|
||||
|
||||
private Policy CreateCircuitBreakerPolicy() =>
|
||||
Policy.Handle<HttpRequestException>()
|
||||
.CircuitBreakerAsync(
|
||||
@ -53,23 +70,6 @@ namespace WebMVC.Services.Utilities
|
||||
}
|
||||
);
|
||||
|
||||
private Policy CreateRetryPolicy() =>
|
||||
Policy.Handle<HttpRequestException>()
|
||||
.WaitAndRetryAsync(
|
||||
// number of retries
|
||||
5,
|
||||
// exponential backofff
|
||||
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
|
||||
// on retry
|
||||
(exception, timeSpan, retryCount, context) =>
|
||||
{
|
||||
_logger.LogTrace($"Retry {retryCount} " +
|
||||
$"of {context.PolicyKey} " +
|
||||
$"at {context.ExecutionKey}, " +
|
||||
$"due to: {exception}.");
|
||||
}
|
||||
);
|
||||
|
||||
public Task<string> GetStringAsync(string uri) =>
|
||||
HttpInvoker(() =>
|
||||
_client.GetStringAsync(uri));
|
||||
|
Loading…
x
Reference in New Issue
Block a user