| | 1 | | using System.Threading.RateLimiting; |
| | 2 | |
|
| | 3 | | namespace Syki.Back.Configs; |
| | 4 | |
|
| | 5 | | public static class RateLimiterConfigs |
| | 6 | | { |
| | 7 | | public static void AddRateLimiterConfigs(this IServiceCollection services) |
| | 8 | | { |
| 1 | 9 | | services.AddRateLimiter(options => |
| 1 | 10 | | { |
| 1 | 11 | | options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| 1 | 12 | |
|
| 1 | 13 | | if (Env.IsTesting() || Env.IsDevelopment()) |
| 1 | 14 | | { |
| 4 | 15 | | options.AddFixedWindowLimiter("SuperVerySmall", o => { o.PermitLimit = 10_000; o.Window = TimeSpan.FromH |
| 4 | 16 | | options.AddFixedWindowLimiter("VerySmall", o => { o.PermitLimit = 10_000; o.Window = TimeSpan.FromHours( |
| 4 | 17 | | options.AddFixedWindowLimiter("Small", o => { o.PermitLimit = 10_000; o.Window = TimeSpan.FromHours(1); |
| 4 | 18 | | options.AddFixedWindowLimiter("Medium", o => { o.PermitLimit = 10_000; o.Window = TimeSpan.FromHours(1); |
| 1 | 19 | | return; |
| 1 | 20 | | } |
| 1 | 21 | |
|
| 0 | 22 | | options.AddPolicy("SuperVerySmall", httpContext => |
| 0 | 23 | | RateLimitPartition.GetFixedWindowLimiter( |
| 0 | 24 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 0 | 25 | | factory: _ => new FixedWindowRateLimiterOptions |
| 0 | 26 | | { |
| 0 | 27 | | PermitLimit = 2, |
| 0 | 28 | | Window = TimeSpan.FromHours(1) |
| 0 | 29 | | })); |
| 1 | 30 | |
|
| 0 | 31 | | options.AddPolicy("VerySmall", httpContext => |
| 0 | 32 | | RateLimitPartition.GetFixedWindowLimiter( |
| 0 | 33 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 0 | 34 | | factory: _ => new FixedWindowRateLimiterOptions |
| 0 | 35 | | { |
| 0 | 36 | | PermitLimit = 10, |
| 0 | 37 | | Window = TimeSpan.FromHours(1) |
| 0 | 38 | | })); |
| 1 | 39 | |
|
| 0 | 40 | | options.AddPolicy("Small", httpContext => |
| 0 | 41 | | RateLimitPartition.GetFixedWindowLimiter( |
| 0 | 42 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 0 | 43 | | factory: _ => new FixedWindowRateLimiterOptions |
| 0 | 44 | | { |
| 0 | 45 | | PermitLimit = 20, |
| 0 | 46 | | Window = TimeSpan.FromHours(1) |
| 0 | 47 | | })); |
| 1 | 48 | |
|
| 0 | 49 | | options.AddPolicy("Medium", httpContext => |
| 0 | 50 | | RateLimitPartition.GetFixedWindowLimiter( |
| 0 | 51 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 0 | 52 | | factory: _ => new FixedWindowRateLimiterOptions |
| 0 | 53 | | { |
| 0 | 54 | | PermitLimit = 120, |
| 0 | 55 | | Window = TimeSpan.FromHours(1) |
| 0 | 56 | | })); |
| 1 | 57 | | }); |
| 1 | 58 | | } |
| | 59 | | } |