feat: improve rate limit partition keys + less strict limits

This commit is contained in:
2026-06-18 21:46:43 -04:00
parent 6ed2974e42
commit 6cadc0b74d
+26 -12
View File
@@ -190,12 +190,12 @@ namespace SharpbinV3.Server
"Sliding", "Sliding",
httpContext => httpContext =>
RateLimitPartition.GetSlidingWindowLimiter( RateLimitPartition.GetSlidingWindowLimiter(
httpContext.GetRequestIP(), GetRateLimitPartitionKey(httpContext),
_ => new SlidingWindowRateLimiterOptions _ => new SlidingWindowRateLimiterOptions
{ {
Window = TimeSpan.FromSeconds(10), Window = TimeSpan.FromSeconds(10),
PermitLimit = 10, PermitLimit = 15,
QueueLimit = 2, QueueLimit = 5,
SegmentsPerWindow = 5, SegmentsPerWindow = 5,
QueueProcessingOrder = QueueProcessingOrder.OldestFirst, QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
} }
@@ -206,13 +206,13 @@ namespace SharpbinV3.Server
"Strict", "Strict",
httpContext => httpContext =>
RateLimitPartition.GetTokenBucketLimiter( RateLimitPartition.GetTokenBucketLimiter(
httpContext.GetRequestIP(), GetRateLimitPartitionKey(httpContext),
_ => new TokenBucketRateLimiterOptions _ => new TokenBucketRateLimiterOptions
{ {
TokenLimit = 8, TokenLimit = 15,
QueueLimit = 0, QueueLimit = 0,
TokensPerPeriod = 1, TokensPerPeriod = 1,
ReplenishmentPeriod = TimeSpan.FromSeconds(30), ReplenishmentPeriod = TimeSpan.FromSeconds(10),
AutoReplenishment = true, AutoReplenishment = true,
QueueProcessingOrder = QueueProcessingOrder.OldestFirst, QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
} }
@@ -223,7 +223,7 @@ namespace SharpbinV3.Server
"Sensitive", "Sensitive",
httpContext => httpContext =>
RateLimitPartition.GetTokenBucketLimiter( RateLimitPartition.GetTokenBucketLimiter(
httpContext.GetRequestIP(), GetRateLimitPartitionKey(httpContext),
_ => new TokenBucketRateLimiterOptions _ => new TokenBucketRateLimiterOptions
{ {
TokenLimit = 5, TokenLimit = 5,
@@ -240,13 +240,11 @@ namespace SharpbinV3.Server
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
{ {
var remoteIp = httpContext.GetRequestIP();
return RateLimitPartition.GetFixedWindowLimiter( return RateLimitPartition.GetFixedWindowLimiter(
remoteIp, GetRateLimitPartitionKey(httpContext),
_ => new FixedWindowRateLimiterOptions _ => new FixedWindowRateLimiterOptions
{ {
PermitLimit = 120, PermitLimit = 180,
Window = TimeSpan.FromMinutes(1), Window = TimeSpan.FromMinutes(1),
QueueProcessingOrder = QueueProcessingOrder.OldestFirst, QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
QueueLimit = 0, QueueLimit = 0,
@@ -257,6 +255,9 @@ namespace SharpbinV3.Server
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
options.OnRejected = async (context, cancellationToken) => options.OnRejected = async (context, cancellationToken) =>
{ {
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
context.HttpContext.Response.Headers.RetryAfter = Math.Ceiling(retryAfter.TotalSeconds).ToString();
await context.HttpContext.Response.WriteAsJsonAsync( await context.HttpContext.Response.WriteAsJsonAsync(
new { success = false, Message = "Too many requests. Please try again later." }, new { success = false, Message = "Too many requests. Please try again later." },
cancellationToken: cancellationToken cancellationToken: cancellationToken
@@ -287,7 +288,6 @@ namespace SharpbinV3.Server
.SetApplicationName("SharpbinV3"); .SetApplicationName("SharpbinV3");
var app = builder.Build(); var app = builder.Build();
app.UseRateLimiter();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
app.MapOpenApi(); app.MapOpenApi();
@@ -296,6 +296,7 @@ namespace SharpbinV3.Server
app.UseCors(); app.UseCors();
app.UseAuthentication(); app.UseAuthentication();
app.UseRateLimiter();
app.UseAuthorization(); app.UseAuthorization();
app.UseOutputCache(); app.UseOutputCache();
@@ -378,5 +379,18 @@ namespace SharpbinV3.Server
return new DirectoryInfo(path); return new DirectoryInfo(path);
} }
static string GetRateLimitPartitionKey(HttpContext httpContext)
{
var apiKey = httpContext.GetApiKeyFromContext();
if (apiKey != null)
return $"api-key:{apiKey.UUID}";
var user = httpContext.GetJwtUser();
if (user != null)
return $"user:{user.UUID}";
return $"ip:{httpContext.GetRequestIP()}";
}
} }
} }