Format rate limiting code (#23)

* RejectionStatusCode

* code format
pull/24/head
Rick Anderson 2022-08-24 12:28:51 -10:00 committed by GitHub
parent bae4c1836f
commit 489476ae57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -247,7 +247,8 @@ options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, IPAddress>(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));
}

View File

@ -15,9 +15,9 @@ public class SampleRateLimiterPolicy : IRateLimiterPolicy<string>
public SampleRateLimiterPolicy(ILogger<SampleRateLimiterPolicy> logger,
IOptions<MyRateLimitOptions> 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<string>
}
public Func<OnRejectedContext, CancellationToken, ValueTask>?
OnRejected { get => _onRejected; }
OnRejected { get => _onRejected; }
// </snippet>
public RateLimitPartition<string> GetPartition(HttpContext httpContext)