Skip to content

Instantly share code, notes, and snippets.

@emonarafat
Created July 12, 2025 18:36
Show Gist options
  • Select an option

  • Save emonarafat/ccb72c861840185860690569d0f1c4c9 to your computer and use it in GitHub Desktop.

Select an option

Save emonarafat/ccb72c861840185860690569d0f1c4c9 to your computer and use it in GitHub Desktop.
Complete Minimal API setup with Fixed Window strategy and global limiter
// 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