// using System.Threading.RateLimiting; using Microsoft.AspNetCore.RateLimiting; using Microsoft.Extensions.Options; using WebRateLimitAuth.Models; namespace WebRateLimitAuth; // public class SampleRateLimiterPolicy : IRateLimiterPolicy { private Func? _onRejected; private readonly MyRateLimitOptions _options; public SampleRateLimiterPolicy(ILogger logger, IOptions options) { _onRejected = (ctx, token) => { ctx.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests; logger.LogWarning($"Request rejected by {nameof(SampleRateLimiterPolicy)}"); return ValueTask.CompletedTask; }; _options = options.Value; } public Func? OnRejected => _onRejected; // public RateLimitPartition GetPartition(HttpContext httpContext) { return RateLimitPartition.GetSlidingWindowLimiter(string.Empty, _ => new SlidingWindowRateLimiterOptions { PermitLimit = _options.PermitLimit, QueueProcessingOrder = QueueProcessingOrder.OldestFirst, QueueLimit = _options.QueueLimit, Window = TimeSpan.FromSeconds(_options.Window), SegmentsPerWindow = _options.SegmentsPerWindow }); } } //