Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retriving RateLimiterStatistics from PartitionedRateLimiter without providing a resource #59899

Open
tnuanchuay opened this issue Jan 16, 2025 · 0 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares feature-rate-limit Work related to use of rate limit primitives

Comments

@tnuanchuay
Copy link

Background and Motivation

Currently, retrieving RateLimitStatistics from a PartitionedRateLimiter requires specifying the resource for which the statistics are needed. This parameter is used to identify the appropriate rate limiter associated with those resources. However, in certain use cases, it may be desirable to retrieve the statistics for all rate limiters managed by the PartitionedRateLimiter.

Proposed API

namespace System.Threading.RateLimiting;

public abstract class PartitionedRateLimiter<TResource> : IAsyncDisposable, IDisposable
{
+    public abstract RateLimiterStatistics[] GetStatistics();
}

Usage Examples

public class RateLimiterHealthCheck : BackgroundService {
   private readonly PartitionedRateLimiter<HttpContext> _partitionedRateLimiter
   public RateLimiterHealthCheck(PartitionedRateLimiter<HttpContext> partitionedRateLimiter){
      _partitionedRateLimiter = partitionedRateLimiter
   }

   protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
      while(!stoppingToken.IsCancellationRequested) {
         foreach(var stats in _partitionedRateLimiter.GetStatistics()){
            //Sending measurement
            MeasurementSender.Send(stats)
         }
      }
   }
}

Alternative Designs

We attempted to retrieve metrics from metric Microsoft.AspNetCore.RateLimiting, but found that the provided data is aggregated, which may not satisfy scenarios requiring more detailed or per-partition insights.

Risks

There is a possibility of encountering race conditions when accessing the dictionary that holds rate limiter instances.

@tnuanchuay tnuanchuay added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 16, 2025
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Jan 16, 2025
@samsp-msft samsp-msft added the feature-rate-limit Work related to use of rate limit primitives label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares feature-rate-limit Work related to use of rate limit primitives
Projects
None yet
Development

No branches or pull requests

2 participants