Skip to content

remove unnecessary RateLimiterHelper#128247

Open
WeihanLi wants to merge 7 commits into
dotnet:mainfrom
WeihanLi:remove-rate-limit-helper
Open

remove unnecessary RateLimiterHelper#128247
WeihanLi wants to merge 7 commits into
dotnet:mainfrom
WeihanLi:remove-rate-limit-helper

Conversation

@WeihanLi
Copy link
Copy Markdown
Contributor

@WeihanLi WeihanLi commented May 15, 2026

This pull request refactors how elapsed time is calculated throughout the rate limiter implementations. The main change is to use Stopwatch.GetElapsedTime directly instead of routing these calls through the RateLimiterHelper class, which is simplified as a result. This reduces indirection and makes the code more straightforward.

Refactoring elapsed time calculation:

  • All uses of RateLimiterHelper.GetElapsedTime are replaced with direct calls to Stopwatch.GetElapsedTime in the ConcurrencyLimiter, FixedWindowRateLimiter, SlidingWindowRateLimiter, and TokenBucketRateLimiter classes, including their IdleDuration properties and internal replenishment logic. [1] [2] [3] [4] [5] [6] [7] [8]

Code cleanup and simplification:

  • The unnecessary method GetElapsedTime(long, long) is removed from RateLimiterHelper, and the class is simplified to only provide a helper for nullable timestamps. [1] [2]

Copilot AI review requested due to automatic review settings May 15, 2026 08:28
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 15, 2026
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

Note

Copilot was unable to run its full agentic suite in this review.

Removes the internal RateLimiterHelper wrapper and replaces calls with direct Stopwatch.GetElapsedTime invocations across the rate limiter implementations.

Changes:

  • Delete RateLimiterHelper.cs and remove its entry from the project file.
  • Replace RateLimiterHelper.GetElapsedTime(...) calls with Stopwatch.GetElapsedTime(...) in the four rate limiter classes.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
RateLimiterHelper.cs Removed helper class.
System.Threading.RateLimiting.csproj Dropped compile reference to deleted helper.
ConcurrencyLimiter.cs Switched IdleDuration to call Stopwatch.GetElapsedTime directly.
FixedWindowRateLimiter.cs Replaced helper usages, including the _getElapsedTime delegate default and IdleDuration.
SlidingWindowRateLimiter.cs Replaced helper usages in IdleDuration and ReplenishInternal.
TokenBucketRateLimiter.cs Replaced helper usages in IdleDuration and ReplenishInternal.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

@WeihanLi WeihanLi marked this pull request as draft May 15, 2026 08:30
Copilot AI review requested due to automatic review settings May 16, 2026 00:46
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 5 out of 5 changed files in this pull request and generated 4 comments.

@WeihanLi WeihanLi marked this pull request as ready for review May 16, 2026 14:15
Copilot AI review requested due to automatic review settings May 16, 2026 14:15
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 5 out of 5 changed files in this pull request and generated 4 comments.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 16, 2026 14:31
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 5 out of 5 changed files in this pull request and generated 5 comments.

Comment on lines +10 to +12
extension(Stopwatch)
{
if (startTimestamp is null)
public static TimeSpan? GetElapsedTime(long? startTimestamp)
/// In tests, this field can be reassigned via reflection to inject custom time behavior without modifying the public API.
/// </summary>
private readonly Func<long?, TimeSpan?> _getElapsedTime = RateLimiterHelper.GetElapsedTime;
private readonly Func<long?, TimeSpan?> _getElapsedTime = Stopwatch.GetElapsedTime;
/// <summary>
/// Function to calculate elapsed time from a given tick value.
/// Defaults to <see cref="RateLimiterHelper.GetElapsedTime(long?)"/>.
/// Defaults to the <c>Stopwatch.GetElapsedTime(long?)</c> extension which returns <see langword="null"/> when the timestamp is <see langword="null"/>.
extension(Stopwatch)
{
if (startTimestamp is null)
public static TimeSpan? GetElapsedTime(long? startTimestamp)
Copilot AI review requested due to automatic review settings May 16, 2026 14:52
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 4 out of 4 changed files in this pull request and generated 1 comment.

/// <summary>
/// Function to calculate elapsed time from a given tick value.
/// Defaults to <see cref="RateLimiterHelper.GetElapsedTime(long?)"/>.
/// Defaults to the <see cref="RateLimiterHelper.GetElapsedTime(long?)"/> which returns <see langword="null"/> when the timestamp is <see langword="null"/>, and returns <c>Stopwatch.GetElapsedTime(long)</c> when not null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Threading community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants