Skip to content

Commit 3c125a2

Browse files
committed
#2080 Adds parameters (in this case those of Polly V8) to fine-tune circuit-breaker behavior
1 parent cc8f5c5 commit 3c125a2

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/Ocelot.Provider.Polly/PollyQoSResiliencePipelineProvider.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ResiliencePipeline<HttpResponseMessage> GetResiliencePipeline(DownstreamR
3232
var options = route.QosOptions;
3333

3434
// Check if we need pipeline at all before calling GetOrAddPipeline
35-
if (options is null ||
35+
if (options is null || options.UseQos == false ||
3636
(options.ExceptionsAllowedBeforeBreaking == 0 && options.TimeoutValue is int.MaxValue))
3737
{
3838
return null; // shortcut > no qos
@@ -48,6 +48,8 @@ private void PollyResiliencePipelineWrapperFactory(ResiliencePipelineBuilder<Htt
4848
{
4949
var options = route.QosOptions;
5050

51+
if(!options.UseQos) return; // shortcut > no qos
52+
5153
// Add TimeoutStrategy if TimeoutValue is not int.MaxValue and greater than 0
5254
if (options.TimeoutValue != int.MaxValue && options.TimeoutValue > 0)
5355
{
@@ -64,8 +66,8 @@ private void PollyResiliencePipelineWrapperFactory(ResiliencePipelineBuilder<Htt
6466

6567
var circuitBreakerStrategyOptions = new CircuitBreakerStrategyOptions<HttpResponseMessage>
6668
{
67-
FailureRatio = 0.8,
68-
SamplingDuration = TimeSpan.FromSeconds(10),
69+
FailureRatio = options.FailureRatio,
70+
SamplingDuration = TimeSpan.FromSeconds(options.SamplingDuration),
6971
MinimumThroughput = options.ExceptionsAllowedBeforeBreaking,
7072
BreakDuration = TimeSpan.FromMilliseconds(options.DurationOfBreak),
7173
ShouldHandle = new PredicateBuilder<HttpResponseMessage>()

src/Ocelot/Configuration/QoSOptions.cs

+47-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public QoSOptions(FileQoSOptions from)
2323
public QoSOptions(
2424
int exceptionsAllowedBeforeBreaking,
2525
int durationOfBreak,
26-
int timeoutValue,
26+
int timeoutValue,
2727
string key)
2828
{
2929
DurationOfBreak = durationOfBreak;
@@ -32,6 +32,36 @@ public QoSOptions(
3232
TimeoutValue = timeoutValue;
3333
}
3434

35+
public QoSOptions(
36+
int exceptionsAllowedBeforeBreaking,
37+
int durationOfBreak,
38+
double failureRatio,
39+
int timeoutValue,
40+
string key)
41+
{
42+
DurationOfBreak = durationOfBreak;
43+
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
44+
Key = key;
45+
TimeoutValue = timeoutValue;
46+
FailureRatio = failureRatio;
47+
}
48+
49+
public QoSOptions(
50+
int exceptionsAllowedBeforeBreaking,
51+
int durationOfBreak,
52+
double failureRatio,
53+
int samplingDuration,
54+
int timeoutValue,
55+
string key)
56+
{
57+
DurationOfBreak = durationOfBreak;
58+
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
59+
Key = key;
60+
TimeoutValue = timeoutValue;
61+
FailureRatio = failureRatio;
62+
SamplingDuration = samplingDuration;
63+
}
64+
3565
/// <summary>
3666
/// How long the circuit should stay open before resetting in milliseconds.
3767
/// </summary>
@@ -54,6 +84,22 @@ public QoSOptions(
5484
/// </value>
5585
public int ExceptionsAllowedBeforeBreaking { get; }
5686

87+
/// <summary>
88+
/// The failure-success ratio that will cause the circuit to break/open.
89+
/// </summary>
90+
/// <value>
91+
/// An <see cref="double"/> 0.8 means 80% failed of all sampled executions.
92+
/// </value>
93+
public double FailureRatio { get; } = .8;
94+
95+
/// <summary>
96+
/// The time period over which the failure-success ratio is calculated (in seconds).
97+
/// </summary>
98+
/// <value>
99+
/// An <see cref="int"/> Time period in seconds, 10 means 10 seconds.
100+
/// </value>
101+
public int SamplingDuration { get; } = 10;
102+
57103
public string Key { get; }
58104

59105
/// <summary>

test/Ocelot.AcceptanceTests/PollyQoSTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override void Dispose()
4444
public void Should_not_timeout()
4545
{
4646
var port = PortFinder.GetRandomPort();
47-
var configuration = FileConfigurationFactory(port, new QoSOptions(10, 500, 1000, null), HttpMethods.Post);
47+
var configuration = FileConfigurationFactory(port, new QoSOptions(10, 500,.5,5, 1000, null), HttpMethods.Post);
4848

4949
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty, 10))
5050
.And(x => GivenThereIsAConfiguration(configuration))

0 commit comments

Comments
 (0)