mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
fix: Value must be greater than zero exception in Polly CircuitBreaker (#836)
when Only TimeoutValue was set in QoSOptions
This commit is contained in:
parent
c5433fcf66
commit
bd058c22ca
@ -1,3 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Polly;
|
||||
using Polly.CircuitBreaker;
|
||||
using Polly.Timeout;
|
||||
|
||||
@ -5,13 +8,16 @@ namespace Ocelot.Provider.Polly
|
||||
{
|
||||
public class CircuitBreaker
|
||||
{
|
||||
public CircuitBreaker(CircuitBreakerPolicy circuitBreakerPolicy, TimeoutPolicy timeoutPolicy)
|
||||
private readonly List<IAsyncPolicy> _policies = new List<IAsyncPolicy>();
|
||||
|
||||
public CircuitBreaker(params IAsyncPolicy[] policies)
|
||||
{
|
||||
CircuitBreakerPolicy = circuitBreakerPolicy;
|
||||
TimeoutPolicy = timeoutPolicy;
|
||||
foreach (var policy in policies.Where(p => p != null))
|
||||
{
|
||||
this._policies.Add(policy);
|
||||
}
|
||||
}
|
||||
|
||||
public CircuitBreakerPolicy CircuitBreakerPolicy { get; private set; }
|
||||
public TimeoutPolicy TimeoutPolicy { get; private set; }
|
||||
public IAsyncPolicy[] Policies => this._policies.ToArray();
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ namespace Ocelot.Provider.Polly
|
||||
try
|
||||
{
|
||||
return await Policy
|
||||
.WrapAsync(_qoSProvider.CircuitBreaker.CircuitBreakerPolicy, _qoSProvider.CircuitBreaker.TimeoutPolicy)
|
||||
.WrapAsync(_qoSProvider.CircuitBreaker.Policies)
|
||||
.ExecuteAsync(() => base.SendAsync(request,cancellationToken));
|
||||
}
|
||||
catch (BrokenCircuitException ex)
|
||||
|
@ -22,6 +22,8 @@ namespace Ocelot.Provider.Polly
|
||||
|
||||
_timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(reRoute.QosOptions.TimeoutValue), strategy);
|
||||
|
||||
if (reRoute.QosOptions.ExceptionsAllowedBeforeBreaking > 0)
|
||||
{
|
||||
_circuitBreakerPolicy = Policy
|
||||
.Handle<HttpRequestException>()
|
||||
.Or<TimeoutRejectedException>()
|
||||
@ -43,6 +45,11 @@ namespace Ocelot.Provider.Polly
|
||||
_logger.LogDebug(".Breaker logging: Half-open; next call is a trial.");
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
_circuitBreakerPolicy = null;
|
||||
}
|
||||
|
||||
CircuitBreaker = new CircuitBreaker(_circuitBreakerPolicy, _timeoutPolicy);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user