Skip to content

Commit a9337ff

Browse files
committed
MaxConcurrentRequests range check
1 parent c9af324 commit a9337ff

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/Middleware/RequestThrottling/src/RequestThrottlingMiddleware.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,14 @@ public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFa
3535
{
3636
throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be specified.", nameof(options));
3737
}
38-
if (_requestThrottlingOptions.MaxConcurrentRequests < 0)
38+
if (_requestThrottlingOptions.MaxConcurrentRequests <= 0)
3939
{
40-
throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be a positive integer.", nameof(options));
40+
throw new ArgumentOutOfRangeException(nameof(options), "The value of `options.MaxConcurrentRequests` must be a positive integer.");
4141
}
4242
if (_requestThrottlingOptions.RequestQueueLimit < 0)
4343
{
4444
throw new ArgumentException("The value of 'options.RequestQueueLimit' must be a positive integer.", nameof(options));
4545
}
46-
if (_requestThrottlingOptions.MaxConcurrentRequests <= 0)
47-
{
48-
throw new ArgumentOutOfRangeException(nameof(options), "The value of `options.MaxConcurrentRequests` must be a positive integer.");
49-
}
5046

5147
if (_requestThrottlingOptions.OnRejected == null)
5248
{
@@ -55,9 +51,15 @@ public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFa
5551

5652
_next = next;
5753
_logger = loggerFactory.CreateLogger<RequestThrottlingMiddleware>();
58-
_requestQueue = new TailDrop(
59-
_requestThrottlingOptions.MaxConcurrentRequests.Value,
60-
_requestThrottlingOptions.RequestQueueLimit);
54+
55+
if (_requestThrottlingOptions.ServerAlwaysBlocks)
56+
{
57+
_requestQueue = new TailDrop(0, _requestThrottlingOptions.RequestQueueLimit);
58+
}
59+
else
60+
{
61+
_requestQueue = new TailDrop(_requestThrottlingOptions.MaxConcurrentRequests.Value, _requestThrottlingOptions.RequestQueueLimit);
62+
}
6163
}
6264

6365
/// <summary>

src/Middleware/RequestThrottling/src/RequestThrottlingOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ public class RequestThrottlingOptions
3333
{
3434
return Task.CompletedTask;
3535
};
36+
37+
/// <summary>
38+
/// For internal testing only. If true, no requests will enter the server.
39+
/// </summary>
40+
internal bool ServerAlwaysBlocks { get; set; } = false;
3641
}
3742
}

src/Middleware/RequestThrottling/test/TestUtils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ public static RequestThrottlingMiddleware CreateTestMiddleware(int? maxConcurren
1515
{
1616
var options = new RequestThrottlingOptions
1717
{
18-
MaxConcurrentRequests = maxConcurrentRequests,
18+
MaxConcurrentRequests = maxConcurrentRequests == 0 ? 999 : maxConcurrentRequests,
1919
RequestQueueLimit = requestQueueLimit
2020
};
2121

22+
options.ServerAlwaysBlocks = maxConcurrentRequests == 0;
23+
2224
if (onRejected != null)
2325
{
2426
options.OnRejected = onRejected;

0 commit comments

Comments
 (0)