|
|
@ -9,11 +9,18 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure |
|
|
|
public class ResilientHttpClientFactory : IResilientHttpClientFactory |
|
|
|
{ |
|
|
|
private readonly ILogger<ResilientHttpClient> _logger; |
|
|
|
private readonly int _retryCount; |
|
|
|
private readonly int _exceptionsAllowedBeforeBreaking; |
|
|
|
|
|
|
|
public ResilientHttpClientFactory(ILogger<ResilientHttpClient> logger) |
|
|
|
=>_logger = logger; |
|
|
|
public ResilientHttpClientFactory(ILogger<ResilientHttpClient> logger, int exceptionsAllowedBeforeBreaking = 5, int retryCount = 6) |
|
|
|
{ |
|
|
|
_logger = logger; |
|
|
|
_exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking; |
|
|
|
_retryCount = retryCount; |
|
|
|
} |
|
|
|
|
|
|
|
public ResilientHttpClient CreateResilientHttpClient() |
|
|
|
|
|
|
|
public ResilientHttpClient CreateResilientHttpClient() |
|
|
|
=> new ResilientHttpClient((origin) => CreatePolicies(), _logger); |
|
|
|
|
|
|
|
private Policy[] CreatePolicies() |
|
|
@ -22,7 +29,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure |
|
|
|
Policy.Handle<HttpRequestException>() |
|
|
|
.WaitAndRetryAsync( |
|
|
|
// number of retries
|
|
|
|
6, |
|
|
|
_retryCount, |
|
|
|
// exponential backofff
|
|
|
|
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), |
|
|
|
// on retry
|
|
|
@ -36,9 +43,9 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure |
|
|
|
_logger.LogDebug(msg); |
|
|
|
}), |
|
|
|
Policy.Handle<HttpRequestException>() |
|
|
|
.CircuitBreakerAsync( |
|
|
|
.CircuitBreakerAsync( |
|
|
|
// number of exceptions before breaking circuit
|
|
|
|
5, |
|
|
|
_exceptionsAllowedBeforeBreaking, |
|
|
|
// time circuit opened before retry
|
|
|
|
TimeSpan.FromMinutes(1), |
|
|
|
(exception, duration) => |
|
|
@ -51,6 +58,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure |
|
|
|
// on circuit closed
|
|
|
|
_logger.LogTrace("Circuit breaker reset"); |
|
|
|
}) |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |