Repository navigation
Description
Description
We were using TokenBucketRateLimiter with a ReplenishmentPeriod in the microseconds (so less than 1ms).
This value is just passed into the Timer constructor by the limiter.
This meant that any value < 1ms was being casted to 0. We ran into an issue where the rate limiter was never replenishing, because the timer would not run, so our application would just pause and no errors were present.
In other applications our replenishment periods were > 1ms, so this wasn't caught in those instances.
I don't know if this qualifies as a bug, but at least the documentation should warn you about this.
TokenBucketRateLimiterOptions only warns about > TimeSpan.Zero..
The TokenBucketRateLimiter could maybe do something to prevent the timer from being initialized with a low enough TimeSpan, but then that would couple the limiter implementation much closer to the Timer implementation.
I am not sure how or if this is something the .NET team would look to fix, but for us at least a documentation update or summary comment update would go a long way to make others aware of the limitation.
Reproduction Steps
Create a TokenBucketRateLimiter with a ReplenishmentPeriod of less than 1ms.
var options = new TokenBucketRateLimiterOptions
{
TokenLimit = 1000,
QueueLimit = int.MaxValue,
TokensPerPeriod = 1,
ReplenishmentPeriod = TimeSpan.FromMicroseconds(500),
AutoReplenishment = true
};
var rateLimiter = new TokenBucketRateLimiter(options);
Expected behavior
To allow for lower than 1ms timespans or make it explicit that we cannot do a replenishment period (or timer period, depending on where the "fix") should be applied, lower than 1ms unless it's the infinite time span which is allowed.
Actual behavior
Added it in repro steps
Regression?
No response
Known Workarounds
We have our own logic in our applications to ensure
Configuration
.NET 8.0.403
Windows (issue is found in our Linux containers too, running the aspnet image from the Microsoft MCR).
We run in x64 but target AnyCPU.
Other information
I am happy to help in any way with fixing this issue, including creating pull requests since I know the team is quite busy. I wanted to consult with you all first before applying a "fix" I would like but that might not be aligned with your goals/thinking.

Activity
dotnet-policy-service commentedon Oct 18, 2024
Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.
BrennanConroy commentedon Oct 18, 2024
Similar issue #105446
We'll need to decide what behavior to have here. Throw for "invalid" values or set a minimum timer value.
rorozcov commentedon Oct 18, 2024
I'm happy to issue a PR to address this. The question is, how do we decide what the "correct" implementation is.
I'd argue that the rate limiter should just have a check for ms > 1 and throw, with updates to docs and comments.
I don't know if you want to "fix" the timer to allow smaller timescales than 1ms. That might be a more disruptive change given it's probably used in many more places
rorozcov commentedon Oct 31, 2024
@BrennanConroy
So I'd say we have two choices in the timer:
We throw if you pass in a value < 1ms but greater than 0ms. This would be backwards compatible with anyone using insanely large Timespans (like int64.max for total ms or ns), and throwing could break users, their timers are already not working if they're passing in < 1ms Timespans.
We change the math to use total nanoseconds instead, considering the idea of anyone using int64.max as their Timespan is extremely unlikely. This could break someone passing in int64.max for the total milliseconds, but I'd argue that is an unlikely edge case (int64.max in nanoseconds is around 292 years, with int64.max ms being 292.5 million years) exceeding human and hardware lifespans that we are unlikely to break anyone. This is my preferred option and it would enable much smaller periods for Timers.
Either fix will require documentation changes to both the Timer docs and the TokenBucketRateLimiter docs.
I am happy to write a PR if you and the .NET team agree in concept so we can iterate and solve it.
Let me know!
rorozcov commentedon Nov 17, 2024
I've checked the underlying APIs and it appears we do need to keep using milliseconds. I dug deeper and found that the other timers in .NET use different approaches.
System.Threading.Timer (this timer) and System.Threading.PeriodicTimer both just cast the milliseconds to a long.
However, System.Timers.Timer uses Math.Ceiling to avoid the problems I've mentioned in this thread.
Now, while any number < 1ms is going to be way less than the minimum timer resolution, the intent of a user passing in < 1ms should still be that the timer will run. Assuming less than 1ms is 0 is confusing from a user's point of view, because only by explicitly setting it to zero would one assume it'll only run once.
I've consulted with others and have reached the following two options:
Updated System.Threading.Timer to use Match.Ceiling. This brings backcompat risks since other users could've written code around this problem already.
Updating the docs to make it extremely clear that any value below 1ms will be treated as 0 and that users should mitigate this themselves.
BrennanConroy commentedon Nov 19, 2024
It's very unlikely that we would make a change in the timer implementation for this.
Really we have 3 options IMO:
I think I'm leaning towards option 2 (with some doc updates). Limiters will keep working, and if someone uses settings that would have made the interval less than 1ms the limiter will worst case be more restrictive than the user expects, which seems better than less restrictive.
TonyValenti commentedon Aug 21, 2025
I believe that option 2 is the right option as it captures user intent.
rorozcov commentedon Sep 2, 2025
@BrennanConroy is someone actively looking into fixing this?
I might have some time in the next month or so to address the issue. It should only be a couple of lines of code to set the limit to 1ms.
Thoughts?
su-senka commentedon Jun 26, 2026
@BrennanConroy @rorozcov — checking in on this one. It looks like the team's direction settled on option 2 (clamp ReplenishmentPeriod to a 1ms floor inside the rate limiter implementations, with accompanying doc updates), which is also what @TonyValenti landed on.
@rorozcov, you mentioned last September you might have time to pick this up — has that happened, or is there a PR in flight I'm missing? If not, I'd like to take a crack at it: validate in TokenBucketRateLimiter's constructor and throw/clamp (matching the agreed approach) for periods below 1ms, plus a test covering the repro above and a doc/comment update on TokenBucketRateLimiterOptions.ReplenishmentPeriod. Let me know if that scope sounds right before I start.
BrennanConroy commentedon Jun 29, 2026
Yeah, clamp to 1ms, update docs, and the other limiters also probably need updating.
Clamp replenishment timer period to 1ms in rate limiters
Clamp replenishment timer period to 1ms in rate limiters