[英]Limit concurrency per API endpoint
如何限制 .net 核心中每個 API 端點的並發性。 我需要一個端點來限制我的微服務中的單個請求。
盡管我對支持您的單例 Web 服務特定用例的實際細節感到好奇。 我會回答你的問題。
想到的最優雅的解決方案是使用自定義中間件: 編寫自定義 ASP.NET Core 中間件
它的代碼可能是這樣的:
// Example middleware
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
namespace YourNamespaceHere
{
public class MySingleRequestLimitingMiddleware
{
private readonly RequestDelegate _next;
private readonly IMemoryCache _cache;
private readonly string FlagCacheKey = "RequestAvailability";
private readonly string _statusLocked = "locked";
private readonly string _statusAvailable = "available";
public MySingleRequestLimitingMiddleware(RequestDelegate next,
IMemoryCache cache)
{
_next = next;
_cache = cache;
}
public async Task Invoke(HttpContext httpContext)
{
string cacheEntry;
// Set cache options.
var cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for 2 minutes.
.SetAbsoluteExpiration(DateTimeOffset.Now.AddMinutes(2));
// Look for the cache key.
if (!_cache.TryGetValue(FlagCacheKey, out cacheEntry))
{
// Key not in cache, so set lock.
cacheEntry = _statusAvailable;
// Save data in cache.
_cache.Set(FlagCacheKey, cacheEntry, cacheEntryOptions);
}
if (cacheEntry.Equals(_statusLocked, StringComparison.OrdinalIgnoreCase))
{
httpContext.Response.StatusCode = (int)HttpStatusCode.TooManyRequests;
httpContext.Response.ContentType = "text/plain";
await httpContext.Response.WriteAsync("Maximum API calls admitted: 1.");
return;
}
/* ***BEWARE: You are entering the request the next line
* will lock up the call until this flag is changed.
* You must set the flag in your controller or somewhere else every single
* request no fail or your call will be left in an unusable state.
* The only thing that fixes the state if you fail to change state
* is restarting the service or waiting 2 minutes. */
_cache.Set(FlagCacheKey, _statusLocked, cacheEntryOptions);
await _next.Invoke(httpContext);
}
}
}
// In your Startup.cs
namespace YourNamespaceHere
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache(); // Add this line to yours somewhere
}
public void Configure(IApplicationBuilder app)
{
// Put it at very top because you don't need to run anything
// else if the limit of 1 request has already been reached.
app.UseWhen(context => context.Request.Path.StartsWithSegments("/somepathtoyourcallhere"), appBuilder =>
{
appBuilder.UseMySingleRequestLimitingMiddleware();
});
// The rest of your code
}
}
}
// Example Controller
// In the controller containing your singleton request call
// ***NOTE you MUST set the flag even if there is an error!!!
public class SomeController : Controller
{
private IMemoryCache _cache;
public SomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
[HttpPost]
public IActionResult YourSingleServiceRequestActionNameHere(SomeMode model)
{
// Some code here...
_cache.Set(FlagCacheKey, "available", new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(DateTimeOffset.Now.AddMinutes(2)));
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Just in case you didn't set that flag already lets make sure.
_cache.Set(FlagCacheKey, "available", new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(DateTimeOffset.Now.AddMinutes(2)));
}
base.Dispose(disposing);
}
}
也可以考慮使用IP或者Client limit rating中間件比如: AspNetCoreRateLimit
由於我們不知道請求何時完成,因此我們將緩存過期時間設置為 2 分鍾,因此如果您的代碼中的標志無法更新,服務將在不超過 2 分鍾的時間內無法使用。 您的通話是否需要 2 分鍾才能完成? 如果是這樣,那么 Web 請求可能會在請求完成之前超時。
如果完成時間超過 2 分鍾,請考慮 Web 服務調用是否是正確的解決方案。
希望有幫助。
快樂編碼!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.