fix: ratelimit e erros de mongodb
This commit is contained in:
parent
d8f3b97c32
commit
51f0820668
26
Program.cs
26
Program.cs
@ -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
|
||||
|
||||
@ -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 =>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user