dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.38k stars 9.99k forks source link

Convenience methods for adding well-known limiter types with more fine grained keys #44850

Open wtgodbe opened 1 year ago

wtgodbe commented 1 year ago

Background and Motivation

It's a common scenario to add a FixedWindow/SlidingWindow/etc. limiter partitioned based on, e.g., httpContext.Request.Path. Today our convenience methods only allow adding these well-known limiters blindly to an endpoint or controller

Proposed API

namespace Microsoft.AspNetCore.RateLimiting;

public static class RateLimiterOptionsExtensions
{
+    public static RateLimiterOptions AddTokenBucketLimiter<TPartitionKey>(this RateLimiterOptions options, string policyName, Action<TokenBucketRateLimiterOptions> configureOptions, Func<HttpContext, TPartitionKey> partitioner);
+    public static RateLimiterOptions AddFixedWindowLimiter<TPartitionKey>(this RateLimiterOptions options, string policyName, Action<FixedWindowRateLimiterOptions> configureOptions, Func<HttpContext, TPartitionKey> partitioner);
+    public static RateLimiterOptions AddSlidingWindowLimiter<TPartitionKey>(this RateLimiterOptions options, string policyName, Action<SlidingWindowRateLimiterOptions> configureOptions, Func<HttpContext, TPartitionKey> partitioner);
+    public static RateLimiterOptions AddConcurrencyLimiter<TPartitionKey>(this RateLimiterOptions options, string policyName, Action<ConcurrencyLimiterOptions> configureOptions, Func<HttpContext, TPartitionKey> partitioner);
}

Usage Examples

builder.Services.AddRateLimiter<string>(options =>
{
    options.AddFixedWindowLimiter("NotTooFast", limiter =>
    {
        limiter.Window = TimeSpan.FromSeconds(5);
        limiter.PermitLimit = 5;
        limiter.QueueLimit = 0;
    },
    httpContext =>
    {
        return httpContext.Request.Path;
    });
});
ghost commented 1 year ago

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.