Skip to content

fix ChainedRateLimiter treating IdleDuration and chained ReplenishmentRateLimiters incorrectly#126158

Merged
DeagleGross merged 8 commits intodotnet:mainfrom
DeagleGross:dmkorolev/ratelimiting-idleduration
Apr 10, 2026
Merged

fix ChainedRateLimiter treating IdleDuration and chained ReplenishmentRateLimiters incorrectly#126158
DeagleGross merged 8 commits intodotnet:mainfrom
DeagleGross:dmkorolev/ratelimiting-idleduration

Conversation

@DeagleGross
Copy link
Copy Markdown
Member

@DeagleGross DeagleGross commented Mar 26, 2026

  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. Now there are 2 implementations of chained limiter depending on the passed limiters to RateLimiter.CreateChained(...)

Related #68776
Fixes #125560

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
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.

@BrennanConroy
Copy link
Copy Markdown
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.

Copilot AI review requested due to automatic review settings March 27, 2026 09:54
@DeagleGross
Copy link
Copy Markdown
Member Author

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.

Reworked like that: introduced shared static methods on ChainedRateLimiter to not duplicate code. Now there is no special case on DefaultPartitionedRateLimiter for ChainedRateLimiter

Copy link
Copy Markdown
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

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

Copy link
Copy Markdown
Member

@BrennanConroy BrennanConroy left a comment

Choose a reason for hiding this comment

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

nit: It's a bit odd to have the static shared methods inside a concrete class (ChainedRateLimiter).

Could we move the shared methods to a separate static class, similar to

internal static unsafe class XxHashShared

Copilot AI review requested due to automatic review settings April 9, 2026 13:09
Copy link
Copy Markdown
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

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@DeagleGross
Copy link
Copy Markdown
Member Author

/ba-g CI keeps failing on WASM tests, not related to the actual code change. Merging

@DeagleGross DeagleGross merged commit c6d701a into dotnet:main Apr 10, 2026
88 of 98 checks passed
@DeagleGross DeagleGross deleted the dmkorolev/ratelimiting-idleduration branch April 10, 2026 16:18
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