RetryAfter metadata in FixedWindowRateLimiter returns time to next window - #124478
Conversation
|
@dotnet-policy-service agree |
|
Tagging subscribers to this area: @agocke, @VSadov |
There was a problem hiding this comment.
Pull request overview
This PR attempts to fix issue #92557 where the FixedWindowRateLimiter incorrectly returns the full window duration as the Retry-After value instead of the remaining time until the next window. The implementation changes the CreateFailedWindowLease method to calculate the remaining time by subtracting the elapsed time from the window duration.
Changes:
- Modified
CreateFailedWindowLeaseto calculate remaining time instead of returning static window multiples - Added a test-injectable function field to control elapsed time behavior for testing
- Updated test assertions to reflect the new expected behavior
- Added a test helper method to manipulate elapsed time in tests
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.
| File | Description |
|---|---|
| src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/FixedWindowRateLimiter.cs | Adds injectable getElapsedTime function field; reimplements CreateFailedWindowLease to calculate remaining window time; comments out original queue-aware implementation |
| src/libraries/System.Threading.RateLimiting/tests/FixedWindowRateLimiterTests.cs | Adds SetElapsedTime helper for testing; updates test expectations; adds calls to set elapsed time to 0 in multiple tests |
a64de24 to
4a1af36
Compare
af257cb to
eced7f5
Compare
eced7f5 to
361f6a6
Compare
|
@VSadov, please review this community PR. |
|
I am looking. Not very familiar with the area, so may need some time to understand the change. It does not look too big though. |
|
The change overall looks good to me, I just had a question about use of |
I agree with this. |
361f6a6 to
da83766
Compare
…xt window. This commit solves the issue of wrongly returned RetryAfter value in the FixedWindowRateLimiter. It now returns the time to window refresh. It includes tests to verify that the RetryAfter value is correct when the window has not yet elapsed, and when it has elapsed but has not been refreshed. Changed Interlocked.Read to Volatile.Read, since the objective of the read was to avoid torn longs on 32 bit. Added Volatile.Write when updating _lastReplenishmentTick. The queue is not taken into consideration in this implementation and does not cover other RateLimiters that may have RetryAfter metadata.
da83766 to
4d76805
Compare
|
Seems like the Build Analysis is stalled due to failed builds. |
|
/ba-g build analysis stalled, all failures are known |
This solves the issue of wrongly returned RetryAfter value, however only in the FixedWindowRateLimiter.
The queue is not taken into consideration in this implementation.
#92557