Clamp replenishment timer period to 1ms in rate limiters#130027
Open
su-senka wants to merge 1 commit into
Open
Clamp replenishment timer period to 1ms in rate limiters#130027su-senka wants to merge 1 commit into
su-senka wants to merge 1 commit into
Conversation
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
Author
|
@dotnet-policy-service agree |
System.Threading.Timer truncates its period to whole milliseconds, so a sub-millisecond ReplenishmentPeriod/Window produced a timer period of 0, which fires once and never again, silently stopping auto-replenishment. Clamp the timer period to a 1ms floor in TokenBucket, FixedWindow, and SlidingWindow limiters. The observable ReplenishmentPeriod is unchanged. Fixes dotnet#109027
0bb2409 to
fe9a2ea
Compare
This was referenced Jun 30, 2026
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.
Fixes #109027
Problem
System.Threading.Timertruncates its period to whole milliseconds. So a sub-millisecondReplenishmentPeriod(TokenBucket) orWindow(FixedWindow / SlidingWindow) ends up as a timer period of 0. A period of 0 fires once and then never again, so auto-replenishment silently stops. No exception, the limiter just stalls. The repro in the issue usedReplenishmentPeriod = 500us.Fix
Clamp the timer period to a 1ms floor at timer creation, in all three replenishing limiters (
TokenBucketRateLimiter,FixedWindowRateLimiter,SlidingWindowRateLimiter).This is the clamp-not-throw direction settled on in the issue. Existing limiters keep working, and a config that would have produced a sub-1ms interval ends up slightly more restrictive than asked for, which is safer than slightly less restrictive.
Notes
The clamp is on the timer only, not on the options. The public
ReplenishmentPeriodproperty still returns whatever the user configured. The tests check this.SlidingWindow is the easy one to miss here. Its effective period is
Window / SegmentsPerWindow, so it can be sub-millisecond even whenWindowitself is >= 1ms (e.g.Window = 5ms, SegmentsPerWindow = 10gives 500us). There's a test case for that.No new exception, so no new resource string. The doc comments on the three options types now mention the 1ms floor.
One thing I left alone:
FixedWindowRateLimiter.CreateFailedWindowLeaseandTokenBucketRateLimiter.CreateFailedTokenLeasestill computeRetryAfterfrom the original un-clamped value, so with a sub-1ms config the reportedRetryAftercan be a touch under the real timer cadence. Didn't want to change user-visible lease metadata as part of this.Testing
One regression test per limiter: a sub-millisecond period constructs fine with
AutoReplenishment = true, and the observableReplenishmentPeriodstill reflects the configured value. The SlidingWindow test also covers the derived-period case above.Built and ran the RateLimiting test suite locally on macOS/arm64. Relying on CI for the .NET Framework leg.