-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Advanced Circuit Breaker #1153
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that it's slower.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will give this another shot later to see if we can bring it down. Allocations are greatly reduced though, that for us makes bigger impact.