dotnet / dotnet/aspnetcore

Retriving RateLimiterStatistics from PartitionedRateLimiter without providing a resource

Open
#59,899 0 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-middleware feature-rate-limit
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

## 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

```diff
namespace System.Threading.RateLimiting;

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

## Usage Examples

```csharp
public class RateLimiterHealthCheck : BackgroundService {
private readonly PartitionedRateLimiter _partitionedRateLimiter
public RateLimiterHealthCheck(PartitionedRateLimiter 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.