-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix AttemptAcquire(0) when token isn't available #123841
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -464,6 +464,96 @@ public override void AcquireZero_WithoutAvailability() | |||||||||
| lease2.Dispose(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| [Fact] | ||||||||||
| public void AcquireZero_WithFractionalTokens_Fails() | ||||||||||
| { | ||||||||||
| var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions | ||||||||||
| { | ||||||||||
| TokenLimit = 3, | ||||||||||
| QueueProcessingOrder = QueueProcessingOrder.OldestFirst, | ||||||||||
| QueueLimit = 1, | ||||||||||
| ReplenishmentPeriod = TimeSpan.FromMilliseconds(2), | ||||||||||
| TokensPerPeriod = 1, | ||||||||||
| AutoReplenishment = false | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| using var lease1 = limiter.AttemptAcquire(3); | ||||||||||
| Assert.True(lease1.IsAcquired); | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
|
||||||||||
| // Replenish a fractional amount (less than 1 token) | ||||||||||
| // With ReplenishmentPeriod=2ms and TokensPerPeriod=1, replenishing 1ms adds 0.5 tokens | ||||||||||
| Replenish(limiter, 1); | ||||||||||
|
|
||||||||||
| // Statistics should still report 0 available permits (fractional tokens truncated) | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
|
||||||||||
| // AttemptAcquire(0) should fail when there are only fractional tokens | ||||||||||
| var lease2 = limiter.AttemptAcquire(0); | ||||||||||
| Assert.False(lease2.IsAcquired); | ||||||||||
| lease2.Dispose(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| [Fact] | ||||||||||
| public async Task AcquireAsyncZero_WithFractionalTokens_Queues() | ||||||||||
| { | ||||||||||
| var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions | ||||||||||
| { | ||||||||||
| TokenLimit = 3, | ||||||||||
| QueueProcessingOrder = QueueProcessingOrder.OldestFirst, | ||||||||||
| QueueLimit = 1, | ||||||||||
| ReplenishmentPeriod = TimeSpan.FromMilliseconds(2), | ||||||||||
| TokensPerPeriod = 1, | ||||||||||
| AutoReplenishment = false | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| using var lease1 = limiter.AttemptAcquire(3); | ||||||||||
| Assert.True(lease1.IsAcquired); | ||||||||||
|
|
||||||||||
| Replenish(limiter, 1); | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
|
||||||||||
| var acquireTask = limiter.AcquireAsync(0); | ||||||||||
| Assert.False(acquireTask.IsCompleted); | ||||||||||
|
|
||||||||||
| Replenish(limiter, 1); | ||||||||||
| using var lease2 = await acquireTask; | ||||||||||
| Assert.True(lease2.IsAcquired); | ||||||||||
| } | ||||||||||
|
Comment on lines
+516
to
+522
|
||||||||||
|
|
||||||||||
| [Fact] | ||||||||||
| public async Task AcquireAsyncZero_QueuedRequest_NotFulfilledWithFractionalTokens() | ||||||||||
| { | ||||||||||
| var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions | ||||||||||
| { | ||||||||||
| TokenLimit = 3, | ||||||||||
| QueueProcessingOrder = QueueProcessingOrder.OldestFirst, | ||||||||||
| QueueLimit = 1, | ||||||||||
| ReplenishmentPeriod = TimeSpan.FromMilliseconds(4), | ||||||||||
| TokensPerPeriod = 1, | ||||||||||
| AutoReplenishment = false | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| using var lease1 = limiter.AttemptAcquire(3); | ||||||||||
| Assert.True(lease1.IsAcquired); | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
|
||||||||||
| Replenish(limiter, 2); | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
|
||||||||||
| var acquireTask = limiter.AcquireAsync(0); | ||||||||||
| Assert.False(acquireTask.IsCompleted); | ||||||||||
|
|
||||||||||
| Replenish(limiter, 1); | ||||||||||
| Assert.Equal(0, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
| Assert.False(acquireTask.IsCompleted); | ||||||||||
|
|
||||||||||
| Replenish(limiter, 1); | ||||||||||
| Assert.Equal(1, limiter.GetStatistics()!.CurrentAvailablePermits); | ||||||||||
|
Comment on lines
+551
to
+552
|
||||||||||
| Replenish(limiter, 1); | |
| Assert.Equal(1, limiter.GetStatistics()!.CurrentAvailablePermits); | |
| Replenish(limiter, 2); | |
| Assert.True(limiter.GetStatistics()!.CurrentAvailablePermits >= 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fulfillment condition works, but it’s hard to read due to redundant comparisons (for
Count > 0,_tokenCount >= Countalready implies>= 1). Consider rewriting it in a more direct form (e.g., branch onnextPendingRequest.Count == 0) to reduce cognitive load and help avoid future mistakes around thetokenCount == 0special-case.