Добавил управление RateLimiter из ENV
All checks were successful
Create and publish a Docker image / Publish image (push) Successful in 4m14s

This commit is contained in:
2025-09-07 10:13:39 +03:00
parent e3bcdfa4e6
commit f15bf4dfe6

View File

@@ -15,6 +15,9 @@ string? tgChatId = configuration["TG_CHAT_ID"];
string? tgToken = configuration["TG_TOKEN"];
string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 0 4 ? * *";
int permitLimit = int.TryParse(configuration["PERMIT_LIMIT"], out var parsedPermitLimit) ? parsedPermitLimit : 40;
int timeLimit = int.TryParse(configuration["TIME_LIMIT"], out var parsedTimeLimit) ? parsedTimeLimit : 10;
// создать папку data если не существует
var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data");
if (!Directory.Exists(dataDirectory))
@@ -82,14 +85,14 @@ builder.Services.AddRateLimiter(options =>
: (httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"),
factory: _ => new FixedWindowRateLimiterOptions
{
PermitLimit = 40,
Window = TimeSpan.FromSeconds(10)
PermitLimit = permitLimit,
Window = TimeSpan.FromSeconds(timeLimit)
}));
options.OnRejected = async (context, cancellationToken) =>
{
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
context.HttpContext.Response.Headers["Retry-After"] = "60";
context.HttpContext.Response.Headers["Retry-After"] = timeLimit.ToString();
await context.HttpContext.Response.WriteAsync("Rate limit exceeded. Please try again later.",
cancellationToken);