Created
July 12, 2025 18:36
-
-
Save emonarafat/ccb72c861840185860690569d0f1c4c9 to your computer and use it in GitHub Desktop.
Complete Minimal API setup with Fixed Window strategy and global limiter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Program.cs — Minimal API setup for global rate limiting | |
| var builder = WebApplication.CreateBuilder(args); | |
| // Register native ASP.NET Core rate limiter | |
| builder.Services.AddRateLimiter(options => | |
| { | |
| options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(context => | |
| RateLimitPartition.GetFixedWindowLimiter( | |
| partitionKey: context.Connection.RemoteIpAddress?.ToString() ?? "unknown", | |
| factory: _ => new FixedWindowRateLimiterOptions | |
| { | |
| PermitLimit = 5, // 5 requests | |
| Window = TimeSpan.FromSeconds(10), // per 10 seconds | |
| QueueProcessingOrder = QueueProcessingOrder.OldestFirst, | |
| QueueLimit = 2 // 2 requests max in queue | |
| })); | |
| }); | |
| var app = builder.Build(); | |
| // Use rate limiter | |
| app.UseRateLimiter(); | |
| app.MapGet("/", () => "Welcome to your protected API").RequireRateLimiting(); | |
| app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment