Rate Limiting¶
The Rate Limiting module configures ASP.NET Core rate limiting with four built-in policy tiers: global, per-tenant, per-user, and per-IP. All limits are configurable via RateLimitingOptions.
Quick Start¶
using Deepstaging.RateLimiting;
// In Program.cs
builder.Services.AddDeepstagingRateLimiting(builder.Configuration);
app.UseRateLimiter();
// Configuration (appsettings.json)
{
"Deepstaging": {
"RateLimiting": {
"GlobalLimit": 1000,
"GlobalWindowSeconds": 60,
"PerTenantLimit": 200,
"PerUserLimit": 60,
"PerIpLimit": 30
}
}
}
Features¶
| Feature | Description |
|---|---|
| Global limiter | Fixed window across all requests |
| Per-tenant limiter | Sliding window keyed by TenantId from CorrelationContext |
| Per-user limiter | Sliding window keyed by authenticated user ID |
| Per-IP limiter | Fixed window keyed by client IP (for unauthenticated endpoints) |
RateLimitingOptions |
[ConfigSection] with all limits, window sizes, rejection status code, queue limit |
| Retry-After header | Automatically included on 429 responses when IncludeRetryAfter = true |
| Queuing | Optional request queuing before rejection (QueueLimit) |
Configuration¶
| Property | Default | Description |
|---|---|---|
GlobalLimit |
1000 | Requests per window (all clients combined) |
GlobalWindowSeconds |
60 | Global window size |
PerTenantLimit |
200 | Per-tenant sliding window limit |
PerTenantWindowSeconds |
60 | Per-tenant window size |
PerUserLimit |
60 | Per-user sliding window limit |
PerUserWindowSeconds |
60 | Per-user window size |
PerIpLimit |
30 | Per-IP fixed window limit |
PerIpWindowSeconds |
60 | Per-IP window size |
RejectionStatusCode |
429 | HTTP status for rate-limited responses |
QueueLimit |
0 | Queue excess requests before rejecting (0 = no queue) |
IncludeRetryAfter |
true |
Include Retry-After header on rejections |
Policy Tiers¶
Policies are evaluated in order. A request is rejected if it exceeds any applicable limit:
- Global — applies to all requests
- Per-Tenant — keyed by
CorrelationContext.TenantId(skipped if no tenant) - Per-User — keyed by authenticated user claim (skipped if unauthenticated)
- Per-IP — keyed by
HttpContext.Connection.RemoteIpAddress(for public endpoints)
Per-Handler Override¶
Override the default policy on individual handlers with [RateLimit]:
[CommandHandler]
[HttpPost("/transfers")]
[RateLimit("ds_per_user")]
public static Eff<AppRuntime, TransferResult> Handle(CreateTransfer cmd) => ...
See Attributes for the full attribute reference and built-in policy constants.
Sub-Pages¶
| Page | Description |
|---|---|
| Attributes | [RateLimit] attribute, built-in policy constants, custom policies |
Source¶
src/Core/Deepstaging.Runtime/RateLimiting/