I only allow {parallelRequestsToController} parallel requests per user. Try again soon.
"; + body = System.Text.Encoding.UTF8.GetBytes(bodyText); + } + + public async Task InvokeAsync(HttpContext context, int idUser, string controllerName) + { + if(controllerNames?.Any(n => controllerName.StartsWith(n)) == false) + { + await next(context); + return; + } + + var userStat = stat.GetOrAdd(idUser, idUser => new()); + var count = userStat.AddOrUpdate(controllerName, 1, (key, value) => value + 1); + if(count < parallelRequestsToController) + { + try + { + await next(context); + } + finally + { + userStat[controllerName]--; + } + } + else + { + context.Response.Clear(); + context.Response.StatusCode = (int)System.Net.HttpStatusCode.TooManyRequests; + + context.Response.Headers.RetryAfter = "1000"; + context.Response.Headers.ContentType = "text/html"; + await context.Response.BodyWriter.WriteAsync(body); + } + } + } +#nullable disable +} diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 73410e21..8046d92c 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -117,7 +117,7 @@ namespace AsbCloudWebApi app.UseAuthentication(); app.UseAuthorization(); - app.UseMiddleware