parent
bae4c1836f
commit
489476ae57
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue