Skip to content

Commit a5cc056

Browse files
authored
Drop custom validation attributes (#1351)
1 parent 761d969 commit a5cc056

17 files changed

+21
-164
lines changed

src/Polly.Core/CircuitBreaker/AdvancedCircuitBreakerStrategyOptions.TResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public class AdvancedCircuitBreakerStrategyOptions<TResult> : CircuitBreakerStra
5252
/// <remarks>
5353
/// Value must be greater than 0.5 seconds. Defaults to 30 seconds.
5454
/// </remarks>
55-
[TimeSpan("00:00:00.500")]
55+
[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
5656
public TimeSpan SamplingDuration { get; set; } = CircuitBreakerConstants.DefaultSamplingDuration;
5757
}

src/Polly.Core/CircuitBreaker/CircuitBreakerStrategyOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class CircuitBreakerStrategyOptions<TResult> : ResilienceStrateg
3030
/// Value must be greater than 0.5 seconds.
3131
/// Defaults to 5 seconds.
3232
/// </remarks>
33-
[TimeSpan("00:00:00.500")]
33+
[Range(typeof(TimeSpan), "00:00:00.500", "1.00:00:00")]
3434
public TimeSpan BreakDuration { get; set; } = CircuitBreakerConstants.DefaultBreakDuration;
3535

3636
/// <summary>

src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class RetryStrategyOptions<TResult> : ResilienceStrategyOptions
6060
/// Defaults to 2 seconds.
6161
/// </para>
6262
/// </remarks>
63-
[TimeSpan("00:00:00", "1.00:00:00")]
63+
[Range(typeof(TimeSpan), "00:00:00", "1.00:00:00")]
6464
public TimeSpan BaseDelay { get; set; } = RetryConstants.DefaultBaseDelay;
6565

6666
/// <summary>

src/Polly.Core/Timeout/TimeoutAttribute.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Polly.Core/Timeout/TimeoutResilienceStrategy.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<T
3232
var timeout = DefaultTimeout;
3333
if (TimeoutGenerator is not null)
3434
{
35-
timeout = await GenerateTimeoutAsync(context).ConfigureAwait(context.ContinueOnCapturedContext);
35+
timeout = await TimeoutGenerator!(new TimeoutGeneratorArguments(context)).ConfigureAwait(context.ContinueOnCapturedContext);
3636
}
3737

3838
if (!TimeoutUtil.ShouldApplyTimeout(timeout))
@@ -80,17 +80,6 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<T
8080
return outcome;
8181
}
8282

83-
internal async ValueTask<TimeSpan> GenerateTimeoutAsync(ResilienceContext context)
84-
{
85-
var timeout = await TimeoutGenerator!(new TimeoutGeneratorArguments(context)).ConfigureAwait(context.ContinueOnCapturedContext);
86-
if (!TimeoutUtil.IsTimeoutValid(timeout))
87-
{
88-
return DefaultTimeout;
89-
}
90-
91-
return timeout;
92-
}
93-
9483
private static CancellationTokenRegistration CreateRegistration(CancellationTokenSource cancellationSource, CancellationToken previousToken)
9584
{
9685
if (previousToken.CanBeCanceled)

src/Polly.Core/Timeout/TimeoutResilienceStrategyBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class TimeoutResilienceStrategyBuilderExtensions
1313
/// </summary>
1414
/// <typeparam name="TBuilder">The builder type.</typeparam>
1515
/// <param name="builder">The builder instance.</param>
16-
/// <param name="timeout">The timeout value. This value should be greater than <see cref="TimeSpan.Zero"/> or <see cref="System.Threading.Timeout.InfiniteTimeSpan"/>.</param>
16+
/// <param name="timeout">The timeout value. This value should be greater than <see cref="TimeSpan.Zero"/>.</param>
1717
/// <returns>The same builder instance.</returns>
1818
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> is <see langword="null"/>.</exception>
1919
/// <exception cref="ValidationException">Thrown when the options produced from the arguments are invalid.</exception>

src/Polly.Core/Timeout/TimeoutStrategyOptions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.ComponentModel.DataAnnotations;
2+
13
namespace Polly.Timeout;
24

35
/// <summary>
@@ -15,10 +17,10 @@ public class TimeoutStrategyOptions : ResilienceStrategyOptions
1517
/// Gets or sets the default timeout.
1618
/// </summary>
1719
/// <remarks>
18-
/// By default, the value is set to <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> thus making the timeout strategy disabled.
20+
/// Defaults to 30 seconds. This value must be greater than 1 second and less than 24 hours.
1921
/// </remarks>
20-
[Timeout]
21-
public TimeSpan Timeout { get; set; } = System.Threading.Timeout.InfiniteTimeSpan;
22+
[Range(typeof(TimeSpan), "00:00:01", "1.00:00:00")]
23+
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30);
2224

2325
/// <summary>
2426
/// Gets or sets the timeout generator that generates the timeout for a given execution.

src/Polly.Core/Timeout/TimeoutUtil.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,4 @@ public static bool ShouldApplyTimeout(TimeSpan timeout)
88
{
99
return timeout > TimeSpan.Zero;
1010
}
11-
12-
public static bool IsTimeoutValid(TimeSpan timeout)
13-
{
14-
if (timeout > TimeSpan.Zero)
15-
{
16-
return true;
17-
}
18-
19-
return timeout == System.Threading.Timeout.InfiniteTimeSpan || timeout > TimeSpan.Zero;
20-
}
2111
}

src/Polly.Core/Utils/TimeSpanAttribute.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

test/Polly.Core.Tests/CircuitBreaker/AdvancedCircuitBreakerOptionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void InvalidOptions_Validate()
9191
9292
Validation Errors:
9393
The field MinimumThroughput must be between 2 and 2147483647.
94-
The field SamplingDuration must be >= to 00:00:00.5000000.
95-
The field BreakDuration must be >= to 00:00:00.5000000.
94+
The field SamplingDuration must be between 00:00:00.5000000 and 1.00:00:00.
95+
The field BreakDuration must be between 00:00:00.5000000 and 1.00:00:00.
9696
The ShouldHandle field is required.
9797
""");
9898
}

0 commit comments

Comments
 (0)