twerner revised this gist 1 year ago. Go to revision
1 file changed, 7 insertions
appsettings.json(file created)
| @@ -0,0 +1,7 @@ | |||
| 1 | + | { | |
| 2 | + | "RateLimit": { | |
| 3 | + | "HttpStatusCode": 429, | |
| 4 | + | "WindowInMinutes": 1, | |
| 5 | + | "Limit": 100 | |
| 6 | + | } | |
| 7 | + | } | |
twerner revised this gist 1 year ago. Go to revision
1 file changed, 1 insertion, 1 deletion
Program.cs
| @@ -15,7 +15,7 @@ builder.Services.AddRateLimiter(options => | |||
| 15 | 15 | options.RejectionStatusCode = Convert.ToInt32(builder.Configuration["RateLimit:HttpStatusCode"]); | |
| 16 | 16 | }); | |
| 17 | 17 | ||
| 18 | - | // configuration of HTTP request pipeline | |
| 18 | + | // existing configuration of HTTP request pipeline... | |
| 19 | 19 | ||
| 20 | 20 | app.UseRouting(); | |
| 21 | 21 | app.UseRateLimiter(); | |
twerner revised this gist 1 year ago. Go to revision
No changes
twerner revised this gist 1 year ago. Go to revision
1 file changed, 21 insertions
Program.cs(file created)
| @@ -0,0 +1,21 @@ | |||
| 1 | + | // Existing services ... | |
| 2 | + | ||
| 3 | + | builder.Services.AddRateLimiter(options => | |
| 4 | + | { | |
| 5 | + | options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => | |
| 6 | + | RateLimitPartition.GetFixedWindowLimiter( | |
| 7 | + | partitionKey: httpContext.User.Identity?.Name ?? httpContext.Request.Headers.Host.ToString(), | |
| 8 | + | factory: partition => new FixedWindowRateLimiterOptions | |
| 9 | + | { | |
| 10 | + | AutoReplenishment = true, | |
| 11 | + | PermitLimit = Convert.ToInt32(builder.Configuration["RateLimit:Limit"]), | |
| 12 | + | QueueLimit = 0, | |
| 13 | + | Window = TimeSpan.FromMinutes(Convert.ToDouble(builder.Configuration["RateLimit:WindowInMinutes"])), | |
| 14 | + | })); | |
| 15 | + | options.RejectionStatusCode = Convert.ToInt32(builder.Configuration["RateLimit:HttpStatusCode"]); | |
| 16 | + | }); | |
| 17 | + | ||
| 18 | + | // configuration of HTTP request pipeline | |
| 19 | + | ||
| 20 | + | app.UseRouting(); | |
| 21 | + | app.UseRateLimiter(); | |