Skip to content

fix ChainedRateLimiter treating IdleDuration and chained ReplenishmentRateLimiters incorrectly#126158

Open
DeagleGross wants to merge 3 commits intodotnet:mainfrom
DeagleGross:dmkorolev/ratelimiting-idleduration
Open

fix ChainedRateLimiter treating IdleDuration and chained ReplenishmentRateLimiters incorrectly#126158
DeagleGross wants to merge 3 commits intodotnet:mainfrom
DeagleGross:dmkorolev/ratelimiting-idleduration

Conversation

@DeagleGross
Copy link
Member

  1. Fixed ChainedRateLimiter.IdleDuration. In the current PR if it detects any of chained child rateLimiters having null IdleDuration, it will return null now. IdleDuration equalling now means rateLimiter is in use or is not ready to be idle.

  2. DefaultPartitionedRateLimiter does not replenish the children rateLimiters of ChainedRateLimiter. I decided not to make ChainedRateLimiter implement ReplenishingRateLimiter, because it is unknown of how to implement ReplenishmentPeriod and IsAutoReplenishing properties. Instead it now has an internal method TryReplenish() which is called on the DefaultPartitionedRateLimiter.Heartbeat()

Related #68776
Fixes #125560

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes ChainedRateLimiter integration with PartitionedRateLimiter by correcting idle detection semantics and ensuring replenishment is invoked for replenishing limiters wrapped inside a ChainedRateLimiter.

Changes:

  • Update ChainedRateLimiter.IdleDuration to return null if any child limiter reports IdleDuration == null.
  • Add internal ChainedRateLimiter.TryReplenish() and call it from DefaultPartitionedRateLimiter.Heartbeat() so inner replenishing limiters get replenished.
  • Add/adjust unit tests covering chained replenishment and idle-based eviction behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/ChainedRateLimiter.cs Fixes IdleDuration semantics and adds TryReplenish() that replenishes inner replenishing limiters.
src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/DefaultPartitionedRateLimiter.cs Special-cases ChainedRateLimiter during heartbeat to call its replenishment logic.
src/libraries/System.Threading.RateLimiting/tests/PartitionedRateLimiterTests.cs Adds coverage ensuring heartbeat replenishes chained inner replenishing limiters and validates eviction behavior with null idle durations.
src/libraries/System.Threading.RateLimiting/tests/ChainedLimiterTests.cs Updates/extends tests for the corrected IdleDuration behavior.

Comment on lines +87 to +100
foreach (RateLimiter limiter in _limiters)
{
if (limiter is ReplenishingRateLimiter replenishingRateLimiter)
{
try
{
replenishingRateLimiter.TryReplenish();
}
catch (Exception ex)
{
exceptions ??= [];
exceptions.Add(ex);
}
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryReplenish() only calls TryReplenish() on direct children that are ReplenishingRateLimiter. If a child limiter is itself a ChainedRateLimiter (nested chains are possible since RateLimiter.CreateChained doesn’t flatten), replenishing limiters inside that nested chain won’t be replenished. Consider handling nested ChainedRateLimiter instances recursively (or flattening chains at construction) so replenishment reaches all inner replenishing limiters.

Copilot uses AI. Check for mistakes.
@BrennanConroy
Copy link
Member

2. because it is unknown of how to implement ReplenishmentPeriod and IsAutoReplenishing properties

IsAutoReplenishing would be false if any of the limiters were of type ReplenishingRateLimiter and had IsAutoReplenshing set as false.

ReplenishmentPeriod would have to be the lowest non-zero (or non-negative) value from the sub-limiters.

Wonder if RateLimiter.CreateChained(...) should return 2 different ChainedRateLimiter implementations, one that is just RateLimiter and one that is ReplenishingRateLimiter depending on the passed in limiters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Threading.RateLimiting.ChainedRateLimiter

3 participants