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

Disallow TimeSpan.Zero in rate limiters #75496

Merged
merged 2 commits into from
Sep 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@
<data name="ReplenishmentLimitTooHigh" xml:space="preserve">
<value>Over 49 days is not supported.</value>
</data>
<data name="ShouldBeGreaterThan0" xml:space="preserve">
<value>{0} must be set to a value greater than 0.</value>
</data>
<data name="ShouldBeGreaterThanOrEqual0" xml:space="preserve">
<value>{0} must be set to a value greater than or equal to 0.</value>
</data>
<data name="ShouldBeGreaterThanTimeSpan0" xml:space="preserve">
<value>{0} must be set to a value greater than TimeSpan.Zero.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public ConcurrencyLimiter(ConcurrencyLimiterOptions options)
}
if (options.PermitLimit <= 0)
{
throw new ArgumentException($"{nameof(options.PermitLimit)} must be set to a value greater than 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.PermitLimit)), nameof(options));
}
if (options.QueueLimit < 0)
{
throw new ArgumentException($"{nameof(options.QueueLimit)} must be set to a value greater than or equal to 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanOrEqual0, nameof(options.QueueLimit)), nameof(options));
}

_options = new ConcurrencyLimiterOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public FixedWindowRateLimiter(FixedWindowRateLimiterOptions options)
}
if (options.PermitLimit <= 0)
{
throw new ArgumentException($"{nameof(options.PermitLimit)} must be set to a value greater than 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.PermitLimit)), nameof(options));
}
if (options.QueueLimit < 0)
{
throw new ArgumentException($"{nameof(options.QueueLimit)} must be set to a value greater than or equal to 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanOrEqual0, nameof(options.QueueLimit)), nameof(options));
}
if (options.Window < TimeSpan.Zero)
if (options.Window <= TimeSpan.Zero)
{
throw new ArgumentException($"{nameof(options.Window)} must be set to a value greater than or equal to TimeSpan.Zero.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanTimeSpan0, nameof(options.Window)), nameof(options));
}

_options = new FixedWindowRateLimiterOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public sealed class FixedWindowRateLimiterOptions
{
/// <summary>
/// Specifies the time window that takes in the requests.
/// Must be set to a value >= <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="FixedWindowRateLimiter"/>.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="FixedWindowRateLimiter"/>.
/// </summary>
/// <remarks><see cref="TimeSpan.Zero"/> means the limiter will never replenish.</remarks>
public TimeSpan Window { get; set; } = TimeSpan.Zero;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ public SlidingWindowRateLimiter(SlidingWindowRateLimiterOptions options)
{
throw new ArgumentNullException(nameof(options));
}
if (options.PermitLimit <= 0 || options.SegmentsPerWindow <= 0)
if (options.PermitLimit <= 0)
{
throw new ArgumentException($"Both {nameof(options.PermitLimit)} and {nameof(options.SegmentsPerWindow)} must be set to values greater than 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.PermitLimit)), nameof(options));
}
if (options.SegmentsPerWindow <= 0)
{
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.SegmentsPerWindow)), nameof(options));
}
if (options.QueueLimit < 0)
{
throw new ArgumentException($"{nameof(options.QueueLimit)} must be set to a value greater than or equal to 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanOrEqual0, nameof(options.QueueLimit)), nameof(options));
}
if (options.Window < TimeSpan.Zero)
if (options.Window <= TimeSpan.Zero)
{
throw new ArgumentException($"{nameof(options.Window)} must be set to a value greater than or equal to TimeSpan.Zero.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanTimeSpan0, nameof(options.Window)), nameof(options));
}

_options = new SlidingWindowRateLimiterOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public sealed class SlidingWindowRateLimiterOptions
{
/// <summary>
/// Specifies the minimum period between replenishments.
/// Must be set to a value >= <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="SlidingWindowRateLimiter"/>.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="SlidingWindowRateLimiter"/>.
/// </summary>
/// <remarks><see cref="TimeSpan.Zero"/> means the limiter will never replenish.</remarks>
public TimeSpan Window { get; set; } = TimeSpan.Zero;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ public TokenBucketRateLimiter(TokenBucketRateLimiterOptions options)
{
throw new ArgumentNullException(nameof(options));
}
if (options.TokenLimit <= 0 || options.TokensPerPeriod <= 0)
if (options.TokenLimit <= 0)
{
throw new ArgumentException($"Both {nameof(options.TokenLimit)} and {nameof(options.TokensPerPeriod)} must be set to values greater than 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.TokenLimit)), nameof(options));
}
if (options.TokensPerPeriod <= 0)
{
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThan0, nameof(options.TokensPerPeriod)), nameof(options));
}
if (options.QueueLimit < 0)
{
throw new ArgumentException($"{nameof(options.QueueLimit)} must be set to a value greater than or equal to 0.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanOrEqual0, nameof(options.QueueLimit)), nameof(options));
}
if (options.ReplenishmentPeriod < TimeSpan.Zero)
if (options.ReplenishmentPeriod <= TimeSpan.Zero)
{
throw new ArgumentException($"{nameof(options.ReplenishmentPeriod)} must be set to a value greater than or equal to TimeSpan.Zero.", nameof(options));
throw new ArgumentException(SR.Format(SR.ShouldBeGreaterThanTimeSpan0, nameof(options.ReplenishmentPeriod)), nameof(options));
}

_options = new TokenBucketRateLimiterOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public sealed class TokenBucketRateLimiterOptions
{
/// <summary>
/// Specifies the minimum period between replenishments.
/// Must be set to a value >= <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="TokenBucketRateLimiter"/>.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="TokenBucketRateLimiter"/>.
/// </summary>
/// <remarks><see cref="TimeSpan.Zero"/> means the limiter will never replenish.</remarks>
public TimeSpan ReplenishmentPeriod { get; set; } = TimeSpan.Zero;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class ConcurrencyLimiterTests : BaseRateLimiterTests
[Fact]
public override void InvalidOptionsThrows()
{
Assert.Throws<ArgumentException>(() => new ConcurrencyLimiter(new ConcurrencyLimiterOptions
AssertExtensions.Throws<ArgumentException>("options", () => new ConcurrencyLimiter(new ConcurrencyLimiterOptions
{
PermitLimit = -1,
QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
QueueLimit = 1
}));
Assert.Throws<ArgumentException>(() => new ConcurrencyLimiter(new ConcurrencyLimiterOptions
AssertExtensions.Throws<ArgumentException>("options", () => new ConcurrencyLimiter(new ConcurrencyLimiterOptions
{
PermitLimit = 1,
QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void CanAcquireResource()
[Fact]
public override void InvalidOptionsThrows()
{
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
{
PermitLimit = -1,
Expand All @@ -44,7 +44,7 @@ public override void InvalidOptionsThrows()
Window = TimeSpan.FromMinutes(2),
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -53,7 +53,7 @@ public override void InvalidOptionsThrows()
Window = TimeSpan.FromMinutes(2),
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -62,7 +62,7 @@ public override void InvalidOptionsThrows()
Window = TimeSpan.MinValue,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -71,6 +71,15 @@ public override void InvalidOptionsThrows()
Window = TimeSpan.FromMinutes(-2),
AutoReplenishment = false,
}));
AssertExtensions.Throws<ArgumentException>("options",
() => new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
{
PermitLimit = 1,
QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
QueueLimit = 1,
Window = TimeSpan.Zero,
AutoReplenishment = false,
}));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void CanAcquireResource()
[Fact]
public override void InvalidOptionsThrows()
{
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = -1,
Expand All @@ -47,7 +47,7 @@ public override void InvalidOptionsThrows()
SegmentsPerWindow = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -57,7 +57,7 @@ public override void InvalidOptionsThrows()
SegmentsPerWindow = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -67,7 +67,7 @@ public override void InvalidOptionsThrows()
SegmentsPerWindow = -1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -77,7 +77,7 @@ public override void InvalidOptionsThrows()
SegmentsPerWindow = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -87,6 +87,16 @@ public override void InvalidOptionsThrows()
SegmentsPerWindow = 1,
AutoReplenishment = false
}));
AssertExtensions.Throws<ArgumentException>("options",
() => new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 1,
QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
QueueLimit = 1,
Window = TimeSpan.Zero,
SegmentsPerWindow = 1,
AutoReplenishment = false
}));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void CanAcquireResource()
[Fact]
public override void InvalidOptionsThrows()
{
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = -1,
Expand All @@ -47,7 +47,7 @@ public override void InvalidOptionsThrows()
TokensPerPeriod = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand All @@ -57,7 +57,7 @@ public override void InvalidOptionsThrows()
TokensPerPeriod = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand All @@ -67,7 +67,7 @@ public override void InvalidOptionsThrows()
TokensPerPeriod = -1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand All @@ -77,7 +77,7 @@ public override void InvalidOptionsThrows()
TokensPerPeriod = 1,
AutoReplenishment = false
}));
Assert.Throws<ArgumentException>(
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand All @@ -87,6 +87,16 @@ public override void InvalidOptionsThrows()
TokensPerPeriod = 1,
AutoReplenishment = false
}));
AssertExtensions.Throws<ArgumentException>("options",
() => new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions
{
TokenLimit = 1,
QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
QueueLimit = 1,
ReplenishmentPeriod = TimeSpan.Zero,
TokensPerPeriod = 1,
AutoReplenishment = false
}));
}

[Fact]
Expand Down