Populate RetryAfter on SlidingWindowRateLimiter#131225
Open
nwoolls wants to merge 7 commits into
Open
Conversation
SlidingWindowLease.FailedLease was a static singleton constructed once with retryAfter: null, so TryGetMetadata(RetryAfter) always returned false on a rejected lease regardless of limiter state. This mirrors the fix already in place for FixedWindowRateLimiter. Unlike the fixed-window case, a sliding window frees permits one segment at a time rather than resetting all at once, so the estimate walks forward through _requestsPerSegment from the next segment to free until enough permits are accounted for, rather than assuming the very next replenishment tick is sufficient. CreateFailedSlidingWindowLease reads _requestsPerSegment and _currentSegmentIndex, both of which are otherwise only mutated under the limiter's lock (in ReplenishInternal). Callers on the permitCount == 0 fast path previously read state outside the lock; that path now acquires the lock before computing the failed lease to avoid a torn read. Fix dotnet#131175
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes SlidingWindowRateLimiter failed leases always reporting no RetryAfter metadata by computing an estimated retry delay based on the sliding window’s per-segment state, aligning behavior with other rate limiter implementations.
Changes:
- Replace the static singleton failed lease path with per-failure
SlidingWindowLeaseinstances that include an estimatedRetryAfter. - Add
CreateFailedSlidingWindowLease(lock-required) to compute retry estimates by walking_requestsPerSegmentacross segments. - Expand tests to validate
RetryAftermetadata behavior across common and edge-case scenarios (queued items, partial segment elapsed, window expired).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/SlidingWindowRateLimiter.cs | Compute and return failed leases with populated RetryAfter metadata, with locking to avoid torn reads. |
| src/libraries/System.Threading.RateLimiting/tests/SlidingWindowRateLimiterTests.cs | Add/adjust tests to validate RetryAfter metadata for failed leases and related timing scenarios. |
… estimation logic
…mits This update ensures the correct lease type is returned when permits become available while waiting for a lock. Added a new test to validate the fix and prevent regression.
Author
|
Not sure on the failing builds — I tried running them locally (if I read the YAML correctly) and they succeeded for me on macOS. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SlidingWindowLease.FailedLease was a static singleton constructed once with retryAfter: null, so TryGetMetadata(RetryAfter) always returned false on a rejected lease regardless of limiter state. This mirrors the fix already in place for FixedWindowRateLimiter.
Unlike the fixed-window case, a sliding window frees permits one segment at a time rather than resetting all at once, so the estimate walks forward through _requestsPerSegment from the next segment to free until enough permits are accounted for, rather than assuming the very next replenishment tick is sufficient.
CreateFailedSlidingWindowLease reads _requestsPerSegment and _currentSegmentIndex, both of which are otherwise only mutated under the limiter's lock (in ReplenishInternal). Callers on the permitCount == 0 fast path previously read state outside the lock; that path now acquires the lock before computing the failed lease to avoid a torn read.
Fix #131175