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

Default names for individual resilience strategies #1475

Merged
merged 1 commit into from
Aug 10, 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
2 changes: 2 additions & 0 deletions src/Polly.Core/CircuitBreaker/CircuitBreakerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Polly.CircuitBreaker;

internal static class CircuitBreakerConstants
{
public const string DefaultName = "CircuitBreaker";

public const string OnCircuitClosed = "OnCircuitClosed";

public const string OnHalfOpenEvent = "OnCircuitHalfOpened";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace Polly.CircuitBreaker;
/// </remarks>
public class CircuitBreakerStrategyOptions<TResult> : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="CircuitBreakerStrategyOptions{TResult}"/> class.
/// </summary>
public CircuitBreakerStrategyOptions() => Name = CircuitBreakerConstants.DefaultName;

/// <summary>
/// Gets or sets the failure-to-success ratio at which the circuit will break.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/Fallback/FallbackConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Polly.Fallback;

internal static class FallbackConstants
{
public const string DefaultName = "Fallback";

public const string OnFallback = "OnFallback";
}

5 changes: 5 additions & 0 deletions src/Polly.Core/Fallback/FallbackStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Polly.Fallback;
/// <typeparam name="TResult">The result type.</typeparam>
public class FallbackStrategyOptions<TResult> : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="FallbackStrategyOptions{TResult}"/> class.
/// </summary>
public FallbackStrategyOptions() => Name = FallbackConstants.DefaultName;

/// <summary>
/// Gets or sets the outcome predicate for determining whether a fallback should be executed.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/Hedging/HedgingConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Polly.Hedging;

internal static class HedgingConstants
{
public const string DefaultName = "Hedging";

public const string OnHedgingEventName = "OnHedging";

public const int DefaultMaxHedgedAttempts = 2;
Expand Down
5 changes: 5 additions & 0 deletions src/Polly.Core/Hedging/HedgingStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Polly.Hedging;
/// <typeparam name="TResult">The type of result these hedging options handle.</typeparam>
public class HedgingStrategyOptions<TResult> : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="HedgingStrategyOptions{TResult}"/> class.
/// </summary>
public HedgingStrategyOptions() => Name = HedgingConstants.DefaultName;

/// <summary>
/// Gets or sets the minimal time of waiting before spawning a new hedged call.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/Retry/RetryConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Polly.Retry;

internal static class RetryConstants
{
public const string DefaultName = "Retry";

public const string OnRetryEvent = "OnRetry";

public const RetryBackoffType DefaultBackoffType = RetryBackoffType.Constant;
Expand Down
5 changes: 5 additions & 0 deletions src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Polly.Retry;
/// <typeparam name="TResult">The type of result the retry strategy handles.</typeparam>
public class RetryStrategyOptions<TResult> : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="RetryStrategyOptions{TResult}"/> class.
/// </summary>
public RetryStrategyOptions() => Name = RetryConstants.DefaultName;

/// <summary>
/// Gets or sets the maximum number of retries to use, in addition to the original call.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/Timeout/TimeoutConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ namespace Polly.Timeout;

internal static class TimeoutConstants
{
public const string DefaultName = "Timeout";

public const string OnTimeoutEvent = "OnTimeout";
}
5 changes: 5 additions & 0 deletions src/Polly.Core/Timeout/TimeoutStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Polly.Timeout;
/// </summary>
public class TimeoutStrategyOptions : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="TimeoutStrategyOptions"/> class.
/// </summary>
public TimeoutStrategyOptions() => Name = TimeoutConstants.DefaultName;

#pragma warning disable IL2026 // Addressed with DynamicDependency on ValidationHelper.Validate method
/// <summary>
/// Gets or sets the default timeout.
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.RateLimiting/RateLimiterConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Polly.RateLimiting;

internal static class RateLimiterConstants
{
public const string DefaultName = "RateLimiter";

public const int DefaultPermitLimit = 1000;

public const int DefaultQueueLimit = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/Polly.RateLimiting/RateLimiterStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Polly.RateLimiting;
/// </summary>
public class RateLimiterStrategyOptions : ResilienceStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="RateLimiterStrategyOptions"/> class.
/// </summary>
public RateLimiterStrategyOptions() => Name = RateLimiterConstants.DefaultName;

/// <summary>
/// Gets or sets the default rate limiter options.
/// </summary>
Expand Down
27 changes: 2 additions & 25 deletions test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ namespace Polly.Core.Tests.CircuitBreaker;

public class CircuitBreakerOptionsTests
{
[Fact]
public void Ctor_Defaults()
{
var options = new CircuitBreakerStrategyOptions();
options.BreakDuration.Should().Be(TimeSpan.FromSeconds(5));
options.FailureRatio.Should().Be(0.1);
options.MinimumThroughput.Should().Be(100);
options.SamplingDuration.Should().Be(TimeSpan.FromSeconds(30));
options.OnOpened.Should().BeNull();
options.OnClosed.Should().BeNull();
options.OnHalfOpened.Should().BeNull();
options.ShouldHandle.Should().NotBeNull();
options.Name.Should().BeNull();

// now set to min values
options.FailureRatio = 0.001;
options.BreakDuration = TimeSpan.FromMilliseconds(500);
options.MinimumThroughput = 2;
options.SamplingDuration = TimeSpan.FromMilliseconds(500);

ValidationHelper.ValidateObject(new(options, "Dummy."));
}

[Fact]
public async Task ShouldHandle_EnsureDefaults()
{
Expand All @@ -42,7 +19,7 @@ public async Task ShouldHandle_EnsureDefaults()
}

[Fact]
public void Ctor_Generic_Defaults()
public void Ctor_Defaults()
{
var options = new CircuitBreakerStrategyOptions<int>();

Expand All @@ -54,7 +31,7 @@ public void Ctor_Generic_Defaults()
options.OnClosed.Should().BeNull();
options.OnHalfOpened.Should().BeNull();
options.ShouldHandle.Should().NotBeNull();
options.Name.Should().BeNull();
options.Name.Should().Be("CircuitBreaker");

// now set to min values
options.FailureRatio = 0.001;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void Ctor_EnsureDefaults()
options.ShouldHandle.Should().NotBeNull();
options.OnFallback.Should().BeNull();
options.FallbackAction.Should().BeNull();
options.Name.Should().Be("Fallback");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void Ctor_EnsureDefaults()
options.HedgingDelay.Should().Be(TimeSpan.FromSeconds(2));
options.MaxHedgedAttempts.Should().Be(2);
options.OnHedging.Should().BeNull();
options.Name.Should().Be("Hedging");
}

[InlineData(true)]
Expand Down
1 change: 1 addition & 0 deletions test/Polly.Core.Tests/Retry/RetryStrategyOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void Ctor_Ok()
options.RetryCount.Should().Be(3);
options.BackoffType.Should().Be(RetryBackoffType.Constant);
options.BaseDelay.Should().Be(TimeSpan.FromSeconds(2));
options.Name.Should().Be("Retry");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void Ctor_EnsureDefaultValues()

options.TimeoutGenerator.Should().BeNull();
options.OnTimeout.Should().BeNull();
options.Name.Should().Be("Timeout");
}

[MemberData(nameof(TimeoutTestUtils.InvalidTimeouts), MemberType = typeof(TimeoutTestUtils))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public void Ctor_EnsureDefaults()
options.OnRejected.Should().BeNull();
options.DefaultRateLimiterOptions.PermitLimit.Should().Be(1000);
options.DefaultRateLimiterOptions.QueueLimit.Should().Be(0);
options.Name.Should().Be("RateLimiter");
}
}