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

Rate limiting middleware #26515

Closed
Tracked by #26326
Rick-Anderson opened this issue Jul 22, 2022 · 5 comments · Fixed by #26881
Closed
Tracked by #26326

Rate limiting middleware #26515

Rick-Anderson opened this issue Jul 22, 2022 · 5 comments · Fixed by #26881
Assignees

Comments

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Jul 22, 2022

System.Threading.RateLimiting will be documented in dotnet/docs#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
Copy link
Contributor Author

Rick-Anderson commented Jul 30, 2022

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

@Rick-Anderson Rick-Anderson added the code help wanted Sample code help wanted. label Jul 30, 2022
@Kahbazi
Copy link
Member

Kahbazi commented Jul 30, 2022

@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?

@dotnet dotnet deleted a comment from Jhstvf4t Jul 30, 2022
@Rick-Anderson
Copy link
Contributor Author

Rick-Anderson commented Jul 30, 2022

@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.

@Kahbazi
Copy link
Member

Kahbazi commented Aug 2, 2022

@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?

@Rick-Anderson
Copy link
Contributor Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants