Skip to content

Commit

Permalink
#2080 Adds parameters (in this case those of Polly V8) to fine-tune c…
Browse files Browse the repository at this point in the history
…ircuit-breaker behavior
  • Loading branch information
RaynaldM authored and raman-m committed Jun 14, 2024
1 parent 8c0180a commit 2d2bdaa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ protected virtual ResiliencePipelineBuilder<HttpResponseMessage> ConfigureCircui
var info = $"Circuit Breaker for Route: {GetRouteName(route)}: ";
var strategyOptions = new CircuitBreakerStrategyOptions<HttpResponseMessage>
{
FailureRatio = 0.8,
SamplingDuration = TimeSpan.FromSeconds(10),
FailureRatio = options.FailureRatio,
SamplingDuration = TimeSpan.FromSeconds(options.SamplingDuration),
MinimumThroughput = options.ExceptionsAllowedBeforeBreaking,
BreakDuration = options.DurationOfBreak > QoSOptions.LowBreakDuration
? TimeSpan.FromMilliseconds(options.DurationOfBreak)
Expand Down
48 changes: 47 additions & 1 deletion src/Ocelot/Configuration/QoSOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public QoSOptions(FileQoSOptions from)
public QoSOptions(
int exceptionsAllowedBeforeBreaking,
int durationOfBreak,
int timeoutValue,
int timeoutValue,
string key)
{
DurationOfBreak = durationOfBreak;
Expand All @@ -32,6 +32,36 @@ public QoSOptions(
TimeoutValue = timeoutValue;
}

public QoSOptions(
int exceptionsAllowedBeforeBreaking,
int durationOfBreak,
double failureRatio,
int timeoutValue,
string key)
{
DurationOfBreak = durationOfBreak;
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
Key = key;
TimeoutValue = timeoutValue;
FailureRatio = failureRatio;
}

public QoSOptions(
int exceptionsAllowedBeforeBreaking,
int durationOfBreak,
double failureRatio,
int samplingDuration,
int timeoutValue,
string key)
{
DurationOfBreak = durationOfBreak;
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
Key = key;
TimeoutValue = timeoutValue;
FailureRatio = failureRatio;
SamplingDuration = samplingDuration;
}

/// <summary>How long the circuit should stay open before resetting in milliseconds.</summary>
/// <remarks>If using Polly version 8 or above, this value must be 500 (0.5 sec) or greater.</remarks>
/// <value>An <see cref="int"/> value (milliseconds).</value>
Expand All @@ -50,6 +80,22 @@ public QoSOptions(
/// </value>
public int ExceptionsAllowedBeforeBreaking { get; }

/// <summary>
/// The failure-success ratio that will cause the circuit to break/open.
/// </summary>
/// <value>
/// An <see cref="double"/> 0.8 means 80% failed of all sampled executions.
/// </value>
public double FailureRatio { get; } = .8;

/// <summary>
/// The time period over which the failure-success ratio is calculated (in seconds).
/// </summary>
/// <value>
/// An <see cref="int"/> Time period in seconds, 10 means 10 seconds.
/// </value>
public int SamplingDuration { get; } = 10;

public string Key { get; }

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions test/Ocelot.AcceptanceTests/PollyQoSTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Dispose()
public void Should_not_timeout()
{
var port = PortFinder.GetRandomPort();
var route = GivenRoute(port, new QoSOptions(10, 500, 1000, null), HttpMethods.Post);
var route = GivenRoute(port, new QoSOptions(10, 500, .5, 5, 1000, null), HttpMethods.Post);
var configuration = GivenConfiguration(route);

this.Given(x => x.GivenThereIsAServiceRunningOn(port, HttpStatusCode.OK, string.Empty, 10))
Expand Down Expand Up @@ -186,7 +186,7 @@ public void Should_timeout_per_default_after_90_seconds()
var port = PortFinder.GetRandomPort();
var route = GivenRoute(port, new QoSOptions(new FileQoSOptions()), HttpMethods.Get);
var configuration = GivenConfiguration(route);


this.Given(x => x.GivenThereIsAServiceRunningOn(port, HttpStatusCode.Created, string.Empty, 3500)) // 3.5s > 3s -> ServiceUnavailable
.And(x => GivenThereIsAConfiguration(configuration))
.And(x => GivenOcelotIsRunningWithPolly())
Expand Down

0 comments on commit 2d2bdaa

Please sign in to comment.