This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring resilience implementation in order to use Polly directly …
…when creating the resilience policies
- Loading branch information
Showing
7 changed files
with
77 additions
and
141 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
src/BuildingBlocks/Resilience/Resilience.Http/Policies/CircuitBreakerPolicy.cs
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/BuildingBlocks/Resilience/Resilience.Http/Policies/RetryPolicy.cs
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/BuildingBlocks/Resilience/Resilience.Http/ResiliencePolicyFactory.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Web/WebMVC/Infrastructure/IResilientHttpClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; | ||
using System; | ||
|
||
namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure | ||
{ | ||
public interface IResilientHttpClientFactory | ||
{ | ||
ResilientHttpClient CreateResilientHttpClient(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Web/WebMVC/Infrastructure/ResilientHttpClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Polly; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure | ||
{ | ||
public class ResilientHttpClientFactory : IResilientHttpClientFactory | ||
{ | ||
private readonly ILogger<ResilientHttpClient> _logger; | ||
|
||
public ResilientHttpClientFactory(ILogger<ResilientHttpClient> logger) | ||
=>_logger = logger; | ||
|
||
public ResilientHttpClient CreateResilientHttpClient() | ||
=> new ResilientHttpClient(CreatePolicies(), _logger); | ||
|
||
|
||
private Policy[] CreatePolicies() | ||
=> new Policy[] | ||
{ | ||
Policy.Handle<HttpRequestException>() | ||
.WaitAndRetryAsync( | ||
// number of retries | ||
6, | ||
// exponential backofff | ||
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, 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); | ||
}), | ||
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"); | ||
})}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters