From 489476ae57b46667160c29b9ef102b82420cfea4 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:28:51 -1000 Subject: [PATCH] Format rate limiting code (#23) * RejectionStatusCode * code format --- .../rate-limit/WebRateLimitAuth/Program.cs | 13 ++++++++----- .../WebRateLimitAuth/SampleRateLimiterPolicy.cs | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs b/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs index faf606a..71c9f48 100644 --- a/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs +++ b/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs @@ -247,7 +247,8 @@ options.GlobalLimiter = PartitionedRateLimiter.Create(co new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit2, queueProcessingOrder: QueueProcessingOrder.OldestFirst, queueLimit: myOptions.queueLimit, - replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), + replenishmentPeriod: + TimeSpan.FromSeconds(myOptions.replenishmentPeriod), tokensPerPeriod: myOptions.tokensPerPeriod, autoReplenishment: myOptions.autoReplenishment)); } @@ -263,8 +264,8 @@ app.MapRazorPages().RequireRateLimiting(userPolicyName); app.MapDefaultControllerRoute(); static string GetUserEndPoint(HttpContext context) => - $"User {context.User?.Identity?.Name ?? "Anonymous"} endpoint: {context.Request.Path}" + - $" {context.Connection.RemoteIpAddress}"; + $"User {context.User?.Identity?.Name ?? "Anonymous"} endpoint:{context.Request.Path}" + + $" {context.Connection.RemoteIpAddress}"; static string GetTicks() => (DateTime.Now.Ticks & 0x11111).ToString("00000"); app.MapGet("/a", (HttpContext context) => $"{GetUserEndPoint(context)} {GetTicks()}") @@ -411,7 +412,8 @@ var options = new RateLimiterOptions() new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit2, queueProcessingOrder: QueueProcessingOrder.OldestFirst, queueLimit: myOptions.queueLimit, - replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), + replenishmentPeriod: + TimeSpan.FromSeconds(myOptions.replenishmentPeriod), tokensPerPeriod: myOptions.tokensPerPeriod, autoReplenishment: myOptions.autoReplenishment)); } @@ -421,7 +423,8 @@ var options = new RateLimiterOptions() new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit, queueProcessingOrder: QueueProcessingOrder.OldestFirst, queueLimit: myOptions.queueLimit, - replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), + replenishmentPeriod: + TimeSpan.FromSeconds(myOptions.replenishmentPeriod), tokensPerPeriod: myOptions.tokensPerPeriod, autoReplenishment: true)); } diff --git a/fundamentals/middleware/rate-limit/WebRateLimitAuth/SampleRateLimiterPolicy.cs b/fundamentals/middleware/rate-limit/WebRateLimitAuth/SampleRateLimiterPolicy.cs index b746a60..f734bf9 100644 --- a/fundamentals/middleware/rate-limit/WebRateLimitAuth/SampleRateLimiterPolicy.cs +++ b/fundamentals/middleware/rate-limit/WebRateLimitAuth/SampleRateLimiterPolicy.cs @@ -15,9 +15,9 @@ public class SampleRateLimiterPolicy : IRateLimiterPolicy public SampleRateLimiterPolicy(ILogger logger, IOptions options) { - _onRejected = (context, token) => + _onRejected = (ctx, token) => { - context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests; + ctx.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests; logger.LogWarning($"Request rejected by {nameof(SampleRateLimiterPolicy)}"); return ValueTask.CompletedTask; }; @@ -25,7 +25,7 @@ public class SampleRateLimiterPolicy : IRateLimiterPolicy } public Func? - OnRejected { get => _onRejected; } + OnRejected { get => _onRejected; } // public RateLimitPartition GetPartition(HttpContext httpContext)