-
-
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.
Calculated Circuit breaker durations (#1776)
Add support for calculating how long a circuit should break for dynamically.
- Loading branch information
Showing
12 changed files
with
250 additions
and
17 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/Polly.Core/CircuitBreaker/BreakDurationGeneratorArguments.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,43 @@ | ||
namespace Polly.CircuitBreaker; | ||
|
||
#pragma warning disable CA1815 // Override equals and operator equals on value types | ||
|
||
/// <summary> | ||
/// Represents arguments used to generate a dynamic break duration for a circuit breaker. | ||
/// </summary> | ||
public readonly struct BreakDurationGeneratorArguments | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BreakDurationGeneratorArguments"/> struct. | ||
/// </summary> | ||
/// <param name="failureRate">The failure rate at which the circuit breaker should trip. | ||
/// It represents the ratio of failed actions to the total executed actions.</param> | ||
/// <param name="failureCount">The number of failures that have occurred. | ||
/// This count is used to determine if the failure threshold has been reached.</param> | ||
/// <param name="context">The resilience context providing additional information | ||
/// about the execution state and failures.</param> | ||
public BreakDurationGeneratorArguments( | ||
double failureRate, | ||
int failureCount, | ||
ResilienceContext context) | ||
{ | ||
FailureRate = failureRate; | ||
FailureCount = failureCount; | ||
Context = context; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the failure rate that represents the ratio of failures to total actions. | ||
/// </summary> | ||
public double FailureRate { get; } | ||
|
||
/// <summary> | ||
/// Gets the count of failures that have occurred. | ||
/// </summary> | ||
public int FailureCount { get; } | ||
|
||
/// <summary> | ||
/// Gets the context that provides additional information about the resilience operation. | ||
/// </summary> | ||
public ResilienceContext Context { get; } | ||
} |
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
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,15 +1,15 @@ | ||
namespace Polly.CircuitBreaker.Health; | ||
|
||
internal readonly record struct HealthInfo(int Throughput, double FailureRate) | ||
internal readonly record struct HealthInfo(int Throughput, double FailureRate, int FailureCount) | ||
{ | ||
public static HealthInfo Create(int successes, int failures) | ||
{ | ||
var total = successes + failures; | ||
if (total == 0) | ||
{ | ||
return new HealthInfo(0, 0); | ||
return new HealthInfo(0, 0, failures); | ||
} | ||
|
||
return new(total, failures / (double)total); | ||
return new(total, failures / (double)total, failures); | ||
} | ||
} |
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 +1,9 @@ | ||
#nullable enable | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments.BreakDurationGeneratorArguments() -> void | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments.BreakDurationGeneratorArguments(double failureRate, int failureCount, Polly.ResilienceContext! context) -> void | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments.Context.get -> Polly.ResilienceContext! | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments.FailureCount.get -> int | ||
Polly.CircuitBreaker.BreakDurationGeneratorArguments.FailureRate.get -> double | ||
Polly.CircuitBreaker.CircuitBreakerStrategyOptions<TResult>.BreakDurationGenerator.get -> System.Func<Polly.CircuitBreaker.BreakDurationGeneratorArguments, System.Threading.Tasks.ValueTask<System.TimeSpan>>? | ||
Polly.CircuitBreaker.CircuitBreakerStrategyOptions<TResult>.BreakDurationGenerator.set -> void |
43 changes: 43 additions & 0 deletions
43
test/Polly.Core.Tests/CircuitBreaker/BreakDurationGeneratorArgumentsTests.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,43 @@ | ||
using Polly; | ||
using Polly.CircuitBreaker; | ||
|
||
namespace Polly.Core.Tests.CircuitBreaker; | ||
|
||
public class BreakDurationGeneratorArgumentsTests | ||
{ | ||
[Fact] | ||
public void Constructor_ShouldSetFailureRate() | ||
{ | ||
double expectedFailureRate = 0.5; | ||
int failureCount = 10; | ||
var context = new ResilienceContext(); | ||
|
||
var args = new BreakDurationGeneratorArguments(expectedFailureRate, failureCount, context); | ||
|
||
args.FailureRate.Should().Be(expectedFailureRate); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_ShouldSetFailureCount() | ||
{ | ||
double failureRate = 0.5; | ||
int expectedFailureCount = 10; | ||
var context = new ResilienceContext(); | ||
|
||
var args = new BreakDurationGeneratorArguments(failureRate, expectedFailureCount, context); | ||
|
||
args.FailureCount.Should().Be(expectedFailureCount); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_ShouldSetContext() | ||
{ | ||
double failureRate = 0.5; | ||
int failureCount = 10; | ||
var expectedContext = new ResilienceContext(); | ||
|
||
var args = new BreakDurationGeneratorArguments(failureRate, failureCount, expectedContext); | ||
|
||
args.Context.Should().Be(expectedContext); | ||
} | ||
} |
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