-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Advanced Circuit Breaker (#1153)
- Loading branch information
Showing
20 changed files
with
622 additions
and
42 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
24 changes: 24 additions & 0 deletions
24
src/Polly.Core.Benchmarks/Benchmarks/CircuitBreakerBenchmark.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,24 @@ | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Polly.Core.Benchmarks; | ||
|
||
namespace Polly.Benchmarks; | ||
|
||
public class CircuitBreakerBenchmark | ||
{ | ||
private object? _strategyV7; | ||
private object? _strategyV8; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_strategyV7 = Helper.CreateCircuitBreaker(PollyVersion.V7); | ||
_strategyV8 = Helper.CreateCircuitBreaker(PollyVersion.V8); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public ValueTask ExecuteCircuitBreaker_V7() => _strategyV7!.ExecuteAsync(PollyVersion.V7); | ||
|
||
[Benchmark] | ||
public ValueTask ExecuteCircuitBreaker_V8() => _strategyV8!.ExecuteAsync(PollyVersion.V8); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Polly.Core.Benchmarks/Internals/Helper.CircuitBreaker.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,36 @@ | ||
using System; | ||
using Polly.CircuitBreaker; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
internal static partial class Helper | ||
{ | ||
public static object CreateCircuitBreaker(PollyVersion technology) | ||
{ | ||
var delay = TimeSpan.FromSeconds(10); | ||
|
||
return technology switch | ||
{ | ||
PollyVersion.V7 => | ||
Policy | ||
.HandleResult(10) | ||
.Or<InvalidOperationException>() | ||
.AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(30), 10, TimeSpan.FromSeconds(5)), | ||
|
||
PollyVersion.V8 => CreateStrategy(builder => | ||
{ | ||
var options = new AdvancedCircuitBreakerStrategyOptions | ||
{ | ||
FailureThreshold = 0.5, | ||
SamplingDuration = TimeSpan.FromSeconds(30), | ||
MinimumThroughput = 10, | ||
BreakDuration = TimeSpan.FromSeconds(5), | ||
}; | ||
|
||
options.ShouldHandle.HandleOutcome<int>((outcome, _) => outcome.Result == 10 || outcome.Exception is InvalidOperationException); | ||
builder.AddAdvancedCircuitBreaker(options); | ||
}), | ||
_ => throw new NotSupportedException() | ||
}; | ||
} | ||
} |
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
76 changes: 63 additions & 13 deletions
76
src/Polly.Core.Tests/CircuitBreaker/Controller/AdvancedCircuitBehaviorTests.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 |
---|---|---|
@@ -1,22 +1,72 @@ | ||
using Moq; | ||
using Polly.CircuitBreaker; | ||
using Polly.CircuitBreaker.Health; | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.CircuitBreaker.Controller; | ||
|
||
public class AdvancedCircuitBehaviorTests | ||
{ | ||
private Mock<HealthMetrics> _metrics = new(MockBehavior.Strict, TimeProvider.System); | ||
|
||
[InlineData(10, 10, 0.0, 0.1, false)] | ||
[InlineData(10, 10, 0.1, 0.1, true)] | ||
[InlineData(10, 10, 0.2, 0.1, true)] | ||
[InlineData(11, 10, 0.2, 0.1, true)] | ||
[InlineData(9, 10, 0.1, 0.1, false)] | ||
[Theory] | ||
public void OnActionFailure_WhenClosed_EnsureCorrectBehavior( | ||
int throughput, | ||
int minimumThruput, | ||
double failureRate, | ||
double failureThreshold, | ||
bool expectedShouldBreak) | ||
{ | ||
_metrics.Setup(m => m.IncrementFailure()); | ||
_metrics.Setup(m => m.GetHealthInfo()).Returns(new HealthInfo(throughput, failureRate)); | ||
|
||
var behavior = new AdvancedCircuitBehavior(new AdvancedCircuitBreakerStrategyOptions { MinimumThroughput = minimumThruput, FailureThreshold = failureThreshold }, _metrics.Object); | ||
|
||
behavior.OnActionFailure(CircuitState.Closed, out var shouldBreak); | ||
|
||
shouldBreak.Should().Be(expectedShouldBreak); | ||
_metrics.VerifyAll(); | ||
} | ||
|
||
[InlineData(CircuitState.Closed, true)] | ||
[InlineData(CircuitState.Open, true)] | ||
[InlineData(CircuitState.Isolated, true)] | ||
[InlineData(CircuitState.HalfOpen, false)] | ||
[Theory] | ||
public void OnActionFailure_State_EnsureCorrectCalls(CircuitState state, bool shouldIncrementFailure) | ||
{ | ||
_metrics = new(MockBehavior.Loose, TimeProvider.System); | ||
|
||
var sut = Create(); | ||
|
||
sut.OnActionFailure(state, out var shouldBreak); | ||
|
||
shouldBreak.Should().BeFalse(); | ||
if (shouldIncrementFailure) | ||
{ | ||
_metrics.Verify(v => v.IncrementFailure(), Times.Once()); | ||
} | ||
else | ||
{ | ||
_metrics.Verify(v => v.IncrementFailure(), Times.Never()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void HappyPath() | ||
public void OnCircuitClosed_Ok() | ||
{ | ||
var behavior = new AdvancedCircuitBehavior(); | ||
|
||
behavior | ||
.Invoking(b => | ||
{ | ||
behavior.OnActionFailure(CircuitState.Closed, out var shouldBreak); | ||
shouldBreak.Should().BeFalse(); | ||
behavior.OnCircuitClosed(); | ||
behavior.OnActionSuccess(CircuitState.Closed); | ||
}) | ||
.Should() | ||
.NotThrow(); | ||
_metrics = new(MockBehavior.Loose, TimeProvider.System); | ||
var sut = Create(); | ||
|
||
sut.OnCircuitClosed(); | ||
|
||
_metrics.Verify(v => v.Reset(), Times.Once()); | ||
} | ||
|
||
private AdvancedCircuitBehavior Create() => new(new AdvancedCircuitBreakerStrategyOptions(), _metrics.Object); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Polly.Core.Tests/CircuitBreaker/Health/HealthMetricsTests.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,26 @@ | ||
using System; | ||
using Polly.CircuitBreaker; | ||
using Polly.CircuitBreaker.Health; | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.CircuitBreaker.Health; | ||
|
||
public class HealthMetricsTests | ||
{ | ||
[InlineData(100, typeof(SingleHealthMetrics))] | ||
[InlineData(199, typeof(SingleHealthMetrics))] | ||
[InlineData(200, typeof(RollingHealthMetrics))] | ||
[InlineData(201, typeof(RollingHealthMetrics))] | ||
[Theory] | ||
public void Create_Ok(int samplingDurationMs, Type expectedType) | ||
{ | ||
HealthMetrics.Create( | ||
new AdvancedCircuitBreakerStrategyOptions | ||
{ | ||
SamplingDuration = TimeSpan.FromMilliseconds(samplingDurationMs) | ||
}, | ||
TimeProvider.System) | ||
.Should() | ||
.BeOfType(expectedType); | ||
} | ||
} |
Oops, something went wrong.