Skip to content
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

Drop simple circuit breaker #1444

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bench/Polly.Core.Benchmarks/Utils/Helper.CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal static partial class Helper
public static object CreateOpenedCircuitBreaker(PollyVersion version, bool handleOutcome)
{
var manualControl = new CircuitBreakerManualControl();
var options = new AdvancedCircuitBreakerStrategyOptions
var options = new CircuitBreakerStrategyOptions
{
ShouldHandle = _ => PredicateResult.True,
ManualControl = manualControl,
Expand All @@ -20,13 +20,13 @@ public static object CreateOpenedCircuitBreaker(PollyVersion version, bool handl
builder.AddStrategy(new OutcomeHandlingStrategy());
}

var strategy = builder.AddAdvancedCircuitBreaker(options).Build();
var strategy = builder.AddCircuitBreaker(options).Build();
manualControl.IsolateAsync().GetAwaiter().GetResult();
return strategy;
}
else
{
var policy = Policy.HandleResult<string>(r => true).AdvancedCircuitBreakerAsync(options.FailureThreshold, options.SamplingDuration, options.MinimumThroughput, options.BreakDuration);
var policy = Policy.HandleResult<string>(r => true).AdvancedCircuitBreakerAsync(options.FailureRatio, options.SamplingDuration, options.MinimumThroughput, options.BreakDuration);
policy.Isolate();
return policy;
}
Expand All @@ -46,9 +46,9 @@ public static object CreateCircuitBreaker(PollyVersion technology)

PollyVersion.V8 => CreateStrategy(builder =>
{
builder.AddAdvancedCircuitBreaker(new AdvancedCircuitBreakerStrategyOptions<string>
builder.AddCircuitBreaker(new CircuitBreakerStrategyOptions<string>
{
FailureThreshold = 0.5,
FailureRatio = 0.5,
SamplingDuration = TimeSpan.FromSeconds(30),
MinimumThroughput = 10,
BreakDuration = TimeSpan.FromSeconds(5),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ internal static partial class Helper
}
})
.AddTimeout(TimeSpan.FromSeconds(1))
.AddAdvancedCircuitBreaker(new()
.AddCircuitBreaker(new()
{
FailureThreshold = 0.5,
FailureRatio = 0.5,
SamplingDuration = TimeSpan.FromSeconds(30),
MinimumThroughput = 10,
BreakDuration = TimeSpan.FromSeconds(5),
Expand Down Expand Up @@ -79,9 +79,9 @@ public static ResilienceStrategy CreateNonGenericStrategyPipeline()
}
})
.AddTimeout(TimeSpan.FromSeconds(1))
.AddAdvancedCircuitBreaker(new()
.AddCircuitBreaker(new()
{
FailureThreshold = 0.5,
FailureRatio = 0.5,
SamplingDuration = TimeSpan.FromSeconds(30),
MinimumThroughput = 10,
BreakDuration = TimeSpan.FromSeconds(5),
Expand Down

This file was deleted.

This file was deleted.

4 changes: 1 addition & 3 deletions src/Polly.Core/CircuitBreaker/CircuitBreakerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ internal static class CircuitBreakerConstants

public const string OnCircuitOpened = "OnCircuitOpened";

public const double DefaultAdvancedFailureThreshold = 0.1;
public const double DefaultFailureRatio = 0.1;

public const int DefaultMinimumThroughput = 100;

public const int MinimumValidThroughput = 2;

public const int DefaultFailureThreshold = 100;

public static readonly TimeSpan DefaultBreakDuration = TimeSpan.FromSeconds(5);

public static readonly TimeSpan DefaultSamplingDuration = TimeSpan.FromSeconds(30);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,129 +11,66 @@ namespace Polly;
public static class CircuitBreakerResilienceStrategyBuilderExtensions
{
/// <summary>
/// Add advanced circuit breaker strategy to the builder.
/// Add circuit breaker strategy to the builder.
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="options">The options instance.</param>
/// <returns>A builder with the circuit breaker strategy added.</returns>
/// <remarks>
/// See <see cref="AdvancedCircuitBreakerStrategyOptions{TResult}"/> for more details about the advanced circuit breaker strategy.
/// See <see cref="CircuitBreakerStrategyOptions{TResult}"/> for more details about the circuit breaker strategy.
/// <para>
/// If you are discarding the strategy created by this call make sure to use <see cref="CircuitBreakerManualControl"/> and dispose the manual control instance when the strategy is no longer used.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="options"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when <paramref name="options"/> are invalid.</exception>
public static ResilienceStrategyBuilder AddAdvancedCircuitBreaker(this ResilienceStrategyBuilder builder, AdvancedCircuitBreakerStrategyOptions options)
public static ResilienceStrategyBuilder AddCircuitBreaker(this ResilienceStrategyBuilder builder, CircuitBreakerStrategyOptions options)
{
Guard.NotNull(builder);
Guard.NotNull(options);

return builder.AddAdvancedCircuitBreakerCore(options);
return builder.AddCircuitBreakerCore(options);
}

/// <summary>
/// Add advanced circuit breaker strategy to the builder.
/// Add circuit breaker strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the circuit breaker strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="options">The options instance.</param>
/// <returns>A builder with the circuit breaker strategy added.</returns>
/// <remarks>
/// See <see cref="AdvancedCircuitBreakerStrategyOptions{TResult}"/> for more details about the advanced circuit breaker strategy.
/// See <see cref="CircuitBreakerStrategyOptions{TResult}"/> for more details about the circuit breaker strategy.
/// <para>
/// If you are discarding the strategy created by this call make sure to use <see cref="CircuitBreakerManualControl"/> and dispose the manual control instance when the strategy is no longer used.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="options"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when <paramref name="options"/> are invalid.</exception>
public static ResilienceStrategyBuilder<TResult> AddAdvancedCircuitBreaker<TResult>(this ResilienceStrategyBuilder<TResult> builder, AdvancedCircuitBreakerStrategyOptions<TResult> options)
public static ResilienceStrategyBuilder<TResult> AddCircuitBreaker<TResult>(this ResilienceStrategyBuilder<TResult> builder, CircuitBreakerStrategyOptions<TResult> options)
{
Guard.NotNull(builder);
Guard.NotNull(options);

return builder.AddAdvancedCircuitBreakerCore(options);
}

/// <summary>
/// Add simple circuit breaker strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the circuit breaker strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="options">The options instance.</param>
/// <returns>A builder with the circuit breaker strategy added.</returns>
/// <remarks>
/// See <see cref="SimpleCircuitBreakerStrategyOptions{TResult}"/> for more details about the advanced circuit breaker strategy.
/// <para>
/// If you are discarding the strategy created by this call make sure to use <see cref="CircuitBreakerManualControl"/> and dispose the manual control instance when the strategy is no longer used.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="options"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when <paramref name="options"/> are invalid.</exception>
public static ResilienceStrategyBuilder<TResult> AddSimpleCircuitBreaker<TResult>(this ResilienceStrategyBuilder<TResult> builder, SimpleCircuitBreakerStrategyOptions<TResult> options)
{
Guard.NotNull(builder);
Guard.NotNull(options);

return builder.AddSimpleCircuitBreakerCore(options);
}

/// <summary>
/// Add simple circuit breaker strategy to the builder.
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="options">The options instance.</param>
/// <returns>A builder with the circuit breaker strategy added.</returns>
/// <remarks>
/// See <see cref="SimpleCircuitBreakerStrategyOptions{TResult}"/> for more details about the advanced circuit breaker strategy.
/// <para>
/// If you are discarding the strategy created by this call make sure to use <see cref="CircuitBreakerManualControl"/> and dispose the manual control instance when the strategy is no longer used.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="options"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when <paramref name="options"/> are invalid.</exception>
public static ResilienceStrategyBuilder AddSimpleCircuitBreaker(this ResilienceStrategyBuilder builder, SimpleCircuitBreakerStrategyOptions options)
{
Guard.NotNull(builder);
Guard.NotNull(options);

return builder.AddSimpleCircuitBreakerCore(options);
return builder.AddCircuitBreakerCore(options);
}

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "All options members preserved.")]
private static TBuilder AddAdvancedCircuitBreakerCore<TBuilder, TResult>(this TBuilder builder, AdvancedCircuitBreakerStrategyOptions<TResult> options)
private static TBuilder AddCircuitBreakerCore<TBuilder, TResult>(this TBuilder builder, CircuitBreakerStrategyOptions<TResult> options)
where TBuilder : ResilienceStrategyBuilderBase
{
return builder.AddStrategy(
context =>
{
var behavior = new AdvancedCircuitBehavior(
options.FailureThreshold,
options.FailureRatio,
options.MinimumThroughput,
HealthMetrics.Create(options.SamplingDuration, context.TimeProvider));

return CreateStrategy<TResult, AdvancedCircuitBreakerStrategyOptions<TResult>>(context, options, behavior);
},
options);
}

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "All options members preserved.")]
private static TBuilder AddSimpleCircuitBreakerCore<TBuilder, TResult>(this TBuilder builder, SimpleCircuitBreakerStrategyOptions<TResult> options)
where TBuilder : ResilienceStrategyBuilderBase
{
return builder.AddStrategy(
context =>
{
return CreateStrategy<TResult, SimpleCircuitBreakerStrategyOptions<TResult>>(
context,
options,
new ConsecutiveFailuresCircuitBehavior(options.FailureThreshold));
return CreateStrategy<TResult, CircuitBreakerStrategyOptions<TResult>>(context, options, behavior);
},
options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ internal void Initialize(Func<CircuitState> circuitStateProvider, Func<Outcome<o
/// <remarks>
/// This will be null if no exceptions or results have been handled by the circuit-breaker since the circuit last closed.</remarks>
/// </summary>
public Outcome<object>? LastHandledOutcome => _lastHandledOutcomeProvider?.Invoke();
internal Outcome<object>? LastHandledOutcome => _lastHandledOutcomeProvider?.Invoke();
}
Loading