Skip to content

Commit 5a10be2

Browse files
Add the failed queued requests back to available permits
1 parent 7ac2e15 commit 5a10be2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/SlidingWindowRateLimiter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ private void ReplenishInternal(long nowTicks)
241241

242242
_lastReplenishmentTick = nowTicks;
243243

244-
// Increament the current segment index while move the window
245-
// We need to know the no. of requests that were acquired in a segment perviously to ensure that we don't acquire more than the permit limit.
244+
// Increment the current segment index while move the window
245+
// We need to know the no. of requests that were acquired in a segment previously to ensure that we don't acquire more than the permit limit.
246246
_currentSegmentIndex = (_currentSegmentIndex + 1) % _options.SegmentsPerWindow;
247247
int oldSegmentRequestCount = _requestsPerSegment[_currentSegmentIndex];
248248
_requestsPerSegment[_currentSegmentIndex] = 0;
@@ -280,7 +280,7 @@ private void ReplenishInternal(long nowTicks)
280280
if (!nextPendingRequest.Tcs.TrySetResult(SuccessfulLease))
281281
{
282282
// Queued item was canceled so add count back
283-
_requestCount -= nextPendingRequest.Count;
283+
_requestCount += nextPendingRequest.Count;
284284
_requestsPerSegment[_currentSegmentIndex] -= nextPendingRequest.Count;
285285
// Updating queue count is handled by the cancellation code
286286
_queueCount += nextPendingRequest.Count;

src/libraries/System.Threading.RateLimiting/tests/SlidingWindowRateLimiterTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ public override async Task CanAcquireResourceAsync()
6363
}
6464

6565
[Fact]
66-
public async Task CanAcquireMulitpleRequestsAsync()
66+
public async Task CanAcquireMultipleRequestsAsync()
6767
{
68+
// This test verifies the following behavior
69+
// 1. when we have available permits after replenish to serve the queued requests
70+
// 2. when the oldest item from queue is remove to accomodate new requests (QueueProcessingOrder: NewestFirst)
6871
var limiter = new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions(4, QueueProcessingOrder.NewestFirst, 4,
6972
TimeSpan.Zero, 3, autoReplenishment: false));
7073

@@ -456,14 +459,15 @@ public override async Task CancelUpdatesQueueLimit()
456459
var ex = await Assert.ThrowsAsync<TaskCanceledException>(() => wait.AsTask());
457460
Assert.Equal(cts.Token, ex.CancellationToken);
458461

459-
wait = limiter.WaitAsync();
462+
wait = limiter.WaitAsync(1);
460463
Assert.False(wait.IsCompleted);
461464

462465
limiter.TryReplenish();
463466
limiter.TryReplenish();
464467

465468
lease = await wait;
466469
Assert.True(lease.IsAcquired);
470+
Assert.Equal(1, limiter.GetAvailablePermits());
467471
}
468472

469473
[Fact]

0 commit comments

Comments
 (0)