AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Rate limiting middleware
- RateLimiting middleware from the blog Announcing Rate Limiting for .NET
- Rate limiting middleware
- Blog update
- https://github.com/dotnet/aspnetcore/issues/41667
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?
@Kahbazi or @martincostello are you interested in writing the sample code for rate limiting middleware?
@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 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 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 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.