forked from ThreeMammals/Ocelot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ThreeMammals#1712 Bump to Polly 8.0 (ThreeMammals#1714)
* ThreeMammals#1712 Migrate to Polly 8.0 * code review post merge * post PR * ThreeMammals#1712 Migrate to Polly 8.0 * code review post merge * Update src/Ocelot.Provider.Polly/PollyQoSProvider.cs Co-authored-by: Raman Maksimchuk <[email protected]> * namespaces * Refactor QoS provider * Refactor AddPolly extension * Remove single quote because semicolon ends sentence --------- Co-authored-by: Ray <[email protected]> Co-authored-by: Raman Maksimchuk <[email protected]>
- Loading branch information
1 parent
b4265a4
commit 3b776a7
Showing
8 changed files
with
51 additions
and
68 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,19 +1,12 @@ | ||
using Polly; | ||
|
||
namespace Ocelot.Provider.Polly | ||
{ | ||
public class CircuitBreaker | ||
{ | ||
private readonly List<IAsyncPolicy> _policies = new(); | ||
|
||
public CircuitBreaker(params IAsyncPolicy[] policies) | ||
{ | ||
foreach (var policy in policies.Where(p => p != null)) | ||
{ | ||
_policies.Add(policy); | ||
} | ||
Policies = policies.Where(p => p != null).ToArray(); | ||
} | ||
|
||
public IAsyncPolicy[] Policies => _policies.ToArray(); | ||
public IAsyncPolicy[] Policies { get; } | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,65 +1,54 @@ | ||
using Ocelot.Configuration; | ||
using Ocelot.Logging; | ||
using Ocelot.Provider.Polly.Interfaces; | ||
using Polly; | ||
using Polly.CircuitBreaker; | ||
using Polly.Timeout; | ||
using Ocelot.Configuration; | ||
using Ocelot.Logging; | ||
using Ocelot.Provider.Polly.Interfaces; | ||
using Polly.CircuitBreaker; | ||
using Polly.Timeout; | ||
|
||
namespace Ocelot.Provider.Polly | ||
{ | ||
public class PollyQoSProvider : IPollyQoSProvider | ||
{ | ||
private readonly AsyncCircuitBreakerPolicy _circuitBreakerPolicy; | ||
private readonly AsyncTimeoutPolicy _timeoutPolicy; | ||
private readonly IOcelotLogger _logger; | ||
|
||
public PollyQoSProvider(AsyncCircuitBreakerPolicy circuitBreakerPolicy, AsyncTimeoutPolicy timeoutPolicy, IOcelotLogger logger) | ||
{ | ||
_circuitBreakerPolicy = circuitBreakerPolicy; | ||
_timeoutPolicy = timeoutPolicy; | ||
_logger = logger; | ||
} | ||
|
||
public PollyQoSProvider(DownstreamRoute route, IOcelotLoggerFactory loggerFactory) | ||
{ | ||
_logger = loggerFactory.CreateLogger<PollyQoSProvider>(); | ||
|
||
_ = Enum.TryParse(route.QosOptions.TimeoutStrategy, out TimeoutStrategy strategy); | ||
|
||
_timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(route.QosOptions.TimeoutValue), strategy); | ||
|
||
AsyncCircuitBreakerPolicy circuitBreakerPolicy = null; | ||
if (route.QosOptions.ExceptionsAllowedBeforeBreaking > 0) | ||
{ | ||
_circuitBreakerPolicy = Policy | ||
{ | ||
var info = $"Route: {GetRouteName(route)}; Breaker logging in {nameof(PollyQoSProvider)}: "; | ||
var logger = loggerFactory.CreateLogger<PollyQoSProvider>(); | ||
circuitBreakerPolicy = Policy | ||
.Handle<HttpRequestException>() | ||
.Or<TimeoutRejectedException>() | ||
.Or<TimeoutException>() | ||
.CircuitBreakerAsync( | ||
exceptionsAllowedBeforeBreaking: route.QosOptions.ExceptionsAllowedBeforeBreaking, | ||
durationOfBreak: TimeSpan.FromMilliseconds(route.QosOptions.DurationOfBreak), | ||
onBreak: (ex, breakDelay) => | ||
{ | ||
_logger.LogError( | ||
".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!", ex); | ||
}, | ||
onBreak: (ex, breakDelay) => | ||
logger.LogError(info + $"Breaking the circuit for {breakDelay.TotalMilliseconds} ms!", ex), | ||
onReset: () => | ||
{ | ||
_logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again."); | ||
}, | ||
logger.LogDebug(info + "Call OK! Closed the circuit again."), | ||
onHalfOpen: () => | ||
{ | ||
_logger.LogDebug(".Breaker logging: Half-open; next call is a trial."); | ||
} | ||
logger.LogDebug(info + "Half-open; Next call is a trial.") | ||
); | ||
} | ||
else | ||
{ | ||
_circuitBreakerPolicy = null; | ||
} | ||
|
||
CircuitBreaker = new CircuitBreaker(_circuitBreakerPolicy, _timeoutPolicy); | ||
_ = Enum.TryParse(route.QosOptions.TimeoutStrategy, out TimeoutStrategy strategy); | ||
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(route.QosOptions.TimeoutValue), strategy); | ||
CircuitBreaker = new CircuitBreaker(circuitBreakerPolicy, timeoutPolicy); | ||
} | ||
|
||
private const string ObsoleteConstructorMessage = $"Use the constructor {nameof(PollyQoSProvider)}({nameof(DownstreamRoute)} route, {nameof(IOcelotLoggerFactory)} loggerFactory)!"; | ||
|
||
[Obsolete(ObsoleteConstructorMessage)] | ||
public PollyQoSProvider(AsyncCircuitBreakerPolicy circuitBreakerPolicy, AsyncTimeoutPolicy timeoutPolicy, IOcelotLogger logger) | ||
{ | ||
throw new NotSupportedException(ObsoleteConstructorMessage); | ||
} | ||
|
||
public CircuitBreaker CircuitBreaker { get; } | ||
public CircuitBreaker CircuitBreaker { get; } | ||
|
||
private static string GetRouteName(DownstreamRoute route) | ||
=> string.IsNullOrWhiteSpace(route.ServiceName) | ||
? route.UpstreamPathTemplate?.Template ?? route.DownstreamPathTemplate?.Value ?? string.Empty | ||
: route.ServiceName; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
// Default Microsoft.NET.Sdk namespaces | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.IO; | ||
global using System.Linq; | ||
global using System.Net.Http; | ||
global using System.Threading; | ||
global using System.Threading.Tasks; | ||
|
||
// Project extra global namespaces | ||
global using Ocelot; | ||
global using Polly; |
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
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