AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Rate limiting middleware

Open Rick-Anderson opened this issue 3 years ago • 5 comments

System.Threading.RateLimiting will be documented in https://github.com/dotnet/docs/issues/30426 and this article will point to that. This article will document the Microsoft.AspNetCore.RateLimiting NuGet package.

@BrennanConroy do you have any ASP.NET Core samples I can start with?

Rick-Anderson avatar Jul 22 '22 03:07 Rick-Anderson

@Kahbazi or @martincostello are you interested in writing the sample code for rate limiting middleware?

Rick-Anderson avatar Jul 30 '22 01:07 Rick-Anderson

@Rick-Anderson I have not write any docs yet, but I would like to give it a try. I'm not familiar with this repo. Where should I start? Are there any specifics samples you have in mind?

Kahbazi avatar Jul 30 '22 06:07 Kahbazi

@Kahbazi I'll handle the document. It would be great if you could write sample code showing how to use System.Threading.RateLimiting. I have a basic sample here but MyCustomLimiter : RateLimiter isn't implemented.

The sample code is ASP.NET Core focused, the .NET team will write samples and docs for .NET.

Rick-Anderson avatar Jul 30 '22 22:07 Rick-Anderson

@Rick-Anderson I created a gist of how to use RateLimiting, but it's basically what you already have in the sample. The sample is limiting the endpoints by IP and by username. I try to understand how I can help. Do you need a custom implementation of RateLimiter? Does that mean a new algorithm like Token Bucket, Sliding window?

Kahbazi avatar Aug 02 '22 12:08 Kahbazi

@Kahbazi take a look at the first sample under RateLimiting middleware. That's what I copied here but MyCustomLimiter : RateLimiter isn't implemented.

Do you need a custom implementation of RateLimiter? Does that mean a new algorithm like Token Bucket, Sliding window?

For the first sample we provide, MyCustomLimiter should be as simple as possible and not too different from the sample code:

if (!StringValues.IsNullOrEmpty(httpContext.Request.Headers["token"]))
        {
            return RateLimitPartition.CreateTokenBucketLimiter("token", key =>
                new TokenBucketRateLimiterOptions(tokenLimit: 5, queueProcessingOrder: QueueProcessingOrder.OldestFirst,
                    queueLimit: 1, replenishmentPeriod: TimeSpan.FromSeconds(5), tokensPerPeriod: 1, autoReplenishment: true));
        }

Do you need a custom implementation of RateLimiter? Does that mean a new algorithm like Token Bucket, Sliding window?

A custom implementation of RateLimiter would probably be great too.

Create a PR with the sample code here.

Rick-Anderson avatar Aug 02 '22 23:08 Rick-Anderson