Skip to content

Commit 20490e9

Browse files
committed
Improve performance
1 parent 9557630 commit 20490e9

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/Middleware/ConcurrencyLimiter/src/QueuePolicies/QueuePolicy.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,49 @@ namespace Microsoft.AspNetCore.ConcurrencyLimiter
1010
{
1111
internal class QueuePolicy : IQueuePolicy, IDisposable
1212
{
13-
private readonly int _maxConcurrentRequests;
14-
private readonly int _requestQueueLimit;
13+
private readonly int _maxTotalRequest;
1514
private readonly SemaphoreSlim _serverSemaphore;
1615

1716
private object _totalRequestsLock = new object();
17+
18+
private readonly static ValueTask<bool> _falseValueTask = new ValueTask<bool>(false);
19+
1820
public int TotalRequests { get; private set; }
1921

2022
public QueuePolicy(IOptions<QueuePolicyOptions> options)
2123
{
22-
_maxConcurrentRequests = options.Value.MaxConcurrentRequests;
23-
if (_maxConcurrentRequests <= 0)
24+
if (options.Value.MaxConcurrentRequests <= 0)
2425
{
25-
throw new ArgumentException(nameof(_maxConcurrentRequests), "MaxConcurrentRequests must be a positive integer.");
26+
throw new ArgumentException(nameof(options.Value.MaxConcurrentRequests), "MaxConcurrentRequests must be a positive integer.");
2627
}
2728

28-
_requestQueueLimit = options.Value.RequestQueueLimit;
29-
if (_requestQueueLimit < 0)
29+
if (options.Value.RequestQueueLimit < 0)
3030
{
31-
throw new ArgumentException(nameof(_requestQueueLimit), "The RequestQueueLimit cannot be a negative number.");
31+
throw new ArgumentException(nameof(options.Value.RequestQueueLimit), "The RequestQueueLimit cannot be a negative number.");
3232
}
3333

34-
_serverSemaphore = new SemaphoreSlim(_maxConcurrentRequests);
34+
_serverSemaphore = new SemaphoreSlim(options.Value.MaxConcurrentRequests);
35+
36+
_maxTotalRequest = options.Value.MaxConcurrentRequests + options.Value.RequestQueueLimit;
3537
}
3638

37-
public async ValueTask<bool> TryEnterAsync()
39+
public ValueTask<bool> TryEnterAsync()
3840
{
3941
// a return value of 'false' indicates that the request is rejected
4042
// a return value of 'true' indicates that the request may proceed
4143
// _serverSemaphore.Release is *not* called in this method, it is called externally when requests leave the server
4244

4345
lock (_totalRequestsLock)
4446
{
45-
if (TotalRequests >= _requestQueueLimit + _maxConcurrentRequests)
47+
if (TotalRequests >= _maxTotalRequest)
4648
{
47-
return false;
49+
return _falseValueTask;
4850
}
4951

5052
TotalRequests++;
5153
}
5254

53-
await _serverSemaphore.WaitAsync();
54-
55-
return true;
55+
return AwaitSemaphore();
5656
}
5757

5858
public void OnExit()
@@ -69,5 +69,12 @@ public void Dispose()
6969
{
7070
_serverSemaphore.Dispose();
7171
}
72+
73+
private async ValueTask<bool> AwaitSemaphore()
74+
{
75+
await _serverSemaphore.WaitAsync();
76+
77+
return true;
78+
}
7279
}
7380
}

0 commit comments

Comments
 (0)