diff --git a/Proxy/Program.cs b/Proxy/Program.cs index f3bb6cd..e78d714 100644 --- a/Proxy/Program.cs +++ b/Proxy/Program.cs @@ -2,6 +2,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using AspNetCoreRateLimit; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Caching.Memory; using Models.Model.External; using NetMQ; using NetMQ.Sockets; @@ -19,23 +20,7 @@ builder.Services.AddCors(options => options.AddPolicy(name: myAllowSpecificOrigins, x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); -// Add memory cache and rate limiting services -builder.Services.AddMemoryCache(); -builder.Services.Configure(options => -{ - options.GeneralRules = - [ - new() - { - Endpoint = "*", // Apply to all endpoints - Period = "10s", // Rate limiting window of 10 second - Limit = 5 // Maximum 5 requests per 10 seconds - } - ]; -}); - -builder.Services.AddInMemoryRateLimiting(); -builder.Services.AddSingleton(); +builder.Services.AddMemoryCache(options => options.ExpirationScanFrequency = TimeSpan.FromSeconds(5)); WebApplication app = builder.Build(); @@ -44,14 +29,17 @@ app.UseForwardedHeaders(new() ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); - -app.UseIpRateLimiting(); // Apply IP-based rate limiting middleware - app.UseCors(myAllowSpecificOrigins); RouteGroupBuilder progressApi = app.MapGroup("/progress"); -progressApi.MapGet("/", () => +progressApi.MapGet("/", (IMemoryCache memoryCache) => { + const string cacheKey = "progress_status"; + if (memoryCache.TryGetValue(cacheKey, out ScanningStatus scanningStatus)) + { + return scanningStatus; + } + CommunicationObject communicationObject = new() { Command = CommunicationCommand.GetScanningProgress @@ -65,7 +53,11 @@ progressApi.MapGet("/", () => byte[] msg = client.ReceiveFrameBytes(); client.Close(); - return JsonSerializer.Deserialize(msg); + scanningStatus = JsonSerializer.Deserialize(msg); + + memoryCache.Set(cacheKey, scanningStatus, DateTimeOffset.Now.AddSeconds(5)); + + return scanningStatus; }); /* diff --git a/RSE.sln.DotSettings.user b/RSE.sln.DotSettings.user index e2d6b61..a5706a8 100644 --- a/RSE.sln.DotSettings.user +++ b/RSE.sln.DotSettings.user @@ -1,7 +1,9 @@  + ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded \ No newline at end of file