// 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 { get => _onRejected; } // public RateLimitPartition GetPartition(HttpContext httpContext) { return RateLimitPartition.CreateSlidingWindowLimiter(string.Empty, key => new SlidingWindowRateLimiterOptions( permitLimit: _options.permitLimit, queueProcessingOrder: QueueProcessingOrder.OldestFirst, queueLimit: _options.queueLimit, window: TimeSpan.FromSeconds(_options.window), segmentsPerWindow: _options.segmentsPerWindow)); } } //