fix: ratelimit e erros de mongodb
All checks were successful
Deploy QR Rapido / test (push) Successful in 35s
Deploy QR Rapido / build-and-push (push) Successful in 6m58s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Successful in 1m35s

This commit is contained in:
Ricardo Carneiro 2025-08-25 20:00:50 -03:00
parent d8f3b97c32
commit 51f0820668
2 changed files with 40 additions and 9 deletions

View File

@ -21,6 +21,8 @@ using Serilog.Sinks.SystemConsole.Themes;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
var builder = WebApplication.CreateBuilder(args);
@ -178,10 +180,10 @@ if (builder.Configuration.GetValue<bool>("ResourceMonitoring:Enabled", true))
builder.Services.AddHostedService<ResourceMonitoringService>();
}
if (builder.Configuration.GetValue<bool>("MongoDbMonitoring:Enabled", true))
{
builder.Services.AddHostedService<MongoDbMonitoringService>();
}
//if (builder.Configuration.GetValue<bool>("MongoDbMonitoring:Enabled", true))
//{
// builder.Services.AddHostedService<MongoDbMonitoringService>();
//}
// CORS for API endpoints
builder.Services.AddCors(options =>
@ -212,8 +214,24 @@ builder.Services.Configure<ForwardedHeadersOptions>(options =>
options.KnownNetworks.Clear();
});
builder.Services.AddRateLimiter(options =>
{
options.RejectionStatusCode = 429;
options.AddFixedWindowLimiter("api", options =>
{
options.PermitLimit = 600; // 10 req/s = 600 req/min
options.Window = TimeSpan.FromMinutes(1);
//options.PermitLimit = 100;
//options.Window = TimeSpan.FromMinutes(1);
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 10;
});
});
var app = builder.Build();
app.UseRateLimiter();
app.UseForwardedHeaders();
// Configure the HTTP request pipeline

View File

@ -200,12 +200,12 @@ namespace QRRapidoApp.Services.Monitoring
{
var command = new BsonDocument("collStats", collectionName);
var result = await context.Database!.RunCommandAsync<BsonDocument>(command);
var size = result.GetValue("size", BsonValue.Create(0)).AsDouble;
var totalIndexSize = result.GetValue("totalIndexSize", BsonValue.Create(0)).AsDouble;
var size = GetDoubleValue(result, "size");
var totalIndexSize = GetDoubleValue(result, "totalIndexSize");
var count = result.GetValue("count", BsonValue.Create(0)).ToInt64();
var avgObjSize = result.GetValue("avgObjSize", BsonValue.Create(0)).AsDouble;
var avgObjSize = GetDoubleValue(result, "avgObjSize");
collectionStats.Add(new CollectionStatistics
{
Name = collectionName,
@ -225,6 +225,19 @@ namespace QRRapidoApp.Services.Monitoring
return collectionStats.OrderByDescending(c => c.SizeMB).ToList();
}
private static double GetDoubleValue(BsonDocument document, string fieldName)
{
var value = document.GetValue(fieldName, BsonValue.Create(0));
return value.BsonType switch
{
BsonType.Double => value.AsDouble,
BsonType.Int32 => (double)value.AsInt32,
BsonType.Int64 => (double)value.AsInt64,
BsonType.Decimal128 => (double)value.AsDecimal128,
_ => 0.0
};
}
private bool ShouldMonitorCollection(string collectionName)
{
return _collectionsToMonitor.Any(monitored =>