Open
Description
Background and Motivation
The ASP.NET Core rate limiting middleware is great, but "limited" in terms of policy validation. Let's start with some code that you can write today in .NET 7:
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter("customPolicy", opt =>
{
opt.PermitLimit = 4;
opt.Window = TimeSpan.FromSeconds(12);
opt.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
opt.QueueLimit = 2;
});
// ...
});
There is no way to validate that customPolicy
actually exists. This is useful when configuring multiple routes from configuration such as is the case for YARP. See dotnet/yarp#1967
Proposed API
It would be preferred to something similar to IAuthorizationPolicyProvider
implemented via DefaultAuthorizationPolicyProvider
and ICorsPolicyProvider
implemented via DefaultCorsPolicyProvider
namespace Microsoft.AspNetCore.RateLimiting;
- internal struct DefaultKeyType
+ public struct DefaultKeyType
{
// omitted ...
}
+
+ public interface IRateLimiterPolicyProvider
+ {
+ ValueTask<IRateLimiterPolicy<DefaultKeyType>?> GetDefaultPolicyAsync();
+ ValueTask<IRateLimiterPolicy<DefaultKeyType>?> GetPolicyAsync(string policyName);
+ }
+
+ public class DefaultRateLimiterPolicyProvider : IRateLimiterPolicyProvider
+ {
+ private readonly RateLimiterOptions _options;
+
+ public DefaultRateLimiterPolicyProvider(IOptions<RateLimiterOptions> options)
+ {
+
+ }
+
+ public ValueTask<IRateLimiterPolicy<DefaultKeyType>?> GetPolicyAsync(string policyName)
+ {
+ options.PolicyMap[policyName] ?? options.UnactivatedPolicyMap[policyName];
+ }
+ }
RateLimiterOptions.PolicyMap
is internal hence this feature cannot be added in another library or the final application.
Usage Examples
Alternative Designs
None
Risks
None
Metadata
Metadata
Assignees
Labels
API needs work before it is approved, it is NOT ready for implementationIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsWork related to use of rate limit primitives