Adding RateLimit Feature ...
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using System.Net;
|
||||
using xCommons.Models;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Interfaces;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
|
||||
namespace xCommons.Middlewares
|
||||
{
|
||||
public class XRateLimitMiddleware
|
||||
{
|
||||
//
|
||||
private readonly RequestDelegate next;
|
||||
private readonly IXRateLimitProvider provider;
|
||||
private readonly XRateLimitConfiguration configuration;
|
||||
|
||||
public XRateLimitMiddleware(
|
||||
RequestDelegate next,
|
||||
IXRateLimitProvider provider,
|
||||
XRateLimitConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
this.next = next;
|
||||
this.provider = provider;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
//
|
||||
if (!provider.IsEnabled)
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var endpoint = context.Features.Get<IEndpointFeature>()?.Endpoint;
|
||||
if (!endpoint.IsNull() &&
|
||||
!endpoint.Metadata
|
||||
.GetMetadata<XRateLimitAttribute>()
|
||||
.IsNull()
|
||||
)
|
||||
{
|
||||
//
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var rule = configuration.GetMatchingRule(context.Request.Path);
|
||||
if (rule.IsNull())
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var key = context.GetXRateLimitKey(rule.KeyMode);
|
||||
var result = provider.TryConsume(key, rule);
|
||||
|
||||
//
|
||||
WriteHeaders(context, result);
|
||||
|
||||
//
|
||||
if (!result.IsAllowed)
|
||||
{
|
||||
//
|
||||
await context.WriteResponseAsJsonAsync(
|
||||
status: (HttpStatusCode)configuration.StatusCode,
|
||||
data: new
|
||||
{
|
||||
message = "Too many requests. Please try again later.",
|
||||
retryAfter = result.RetryAfterSeconds,
|
||||
resetOn = result.ResetOn
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
await next(context);
|
||||
}
|
||||
|
||||
private static void WriteHeaders(
|
||||
HttpContext context,
|
||||
XRateLimitResult result
|
||||
)
|
||||
{
|
||||
//
|
||||
context.Response.Headers[XRateLimitConstants.LimitHeader] =
|
||||
result.Limit.ToString();
|
||||
|
||||
//
|
||||
context.Response.Headers[XRateLimitConstants.RemainingHeader] =
|
||||
result.Remaining.ToString();
|
||||
|
||||
//
|
||||
context.Response.Headers[XRateLimitConstants.ResetHeader] =
|
||||
result.ResetOn.ToUnixTimeSeconds().ToString();
|
||||
|
||||
//
|
||||
if (!result.IsAllowed)
|
||||
{
|
||||
context.Response.Headers[XRateLimitConstants.RetryAfterHeader] =
|
||||
result.RetryAfterSeconds.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user