question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rate Limiting configuration - policy validation

See original GitHub issue

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 https://github.com/microsoft/reverse-proxy/pull/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

See YARP: https://github.com/microsoft/reverse-proxy/blob/26ce1d15f868cb8da1891d65db1e59a20fd6ecbf/src/ReverseProxy/Configuration/ConfigValidator.cs#L312-L318

Alternative Designs

None

Risks

None

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
adityamandaleekacommented, Jan 13, 2023

This proposal will be discussed by our team in an upcoming API review meeting, after which we’ll provide feedback/suggestions.

Once a proposal gets to the api-approved state, we’ll be ready to take a PR to implement the change.

1reaction
mburumaxwellcommented, Jan 10, 2023

@adityamandaleeka this is done

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rate Limiting Policy
When you configure the Rate Limiting policy, you can specify any number of pairs of quota (number of requests) and time window (time...
Read more >
Rate Limiting Policy | Zuplo Docs
Rate -limiting allows you to set a maximum rate of requests for your API gateway. This is useful to enforce rate limits agreed...
Read more >
7 - Configure Rate Limiting, Header Validation and Routing ...
Learn how to configure API Rate Limiting, Header Validation and Resource Based Routing policies in Oracle API Platform Cloud Service.
Read more >
Rate limiting
Rate limiting is performed by taking the incoming request and identifying the parts of the request that makes it unique to a client....
Read more >
Rate Limit Policy
To ensure the quality of Auth0's services, the Auth0 APIs are subject to rate limiting. Depending on the API endpoint, the request limit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found