Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The returned Response headers is confusing #418

Open
AmmarAlhabib opened this issue Feb 18, 2023 · 0 comments
Open

The returned Response headers is confusing #418

AmmarAlhabib opened this issue Feb 18, 2023 · 0 comments

Comments

@AmmarAlhabib
Copy link

AmmarAlhabib commented Feb 18, 2023

It should not set the X-Rate-Limit headers for the longest period,
It should set it for the exact rule matched in the Identity
Because the logic inside foreach will not follow the the longest period, it follows the rule matched in the identity.

Details:
I have two general rules
"GeneralRules": [ { "Endpoint": "GET:/api/Upload/uploadfile", "Period": "50s", "Limit": 2 }, { "Endpoint": "*", "Period": "1m", "Limit": 10 } ]

However, when I call the endpoint 'api/Upload/uploadfile' it will return this response header ( the longest period)

api-supported-versions: 1.0,2.0
content-length: 24
content-type: application/json; charset=utf-8
date: Sat,18 Feb 2023 20:54:50 GMT
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
x-rate-limit-limit: 1m
x-rate-limit-remaining: 9
x-rate-limit-reset: 2023-02-18T20:55:48.2135199Z

which is related to '*' end point.

But It should return :

x-rate-limit-limit: 50s
x-rate-limit-remaining: 1

Now if I request the endpoint 'api/Upload/uploadfile' 3 times in less than 10s, it will block the request and will return the response which tells the quota is exceeded the limit.

The issue is :
when I call the endpoint 'api/Upload/uploadfile' for the first time, why it returns the response header related to '*' end point ( the longest period), as long as the logic inside foreach won't follow the longest period, it follows the exact rules passed in the Identity.

So, it should return :

x-rate-limit-limit: 50s
x-rate-limit-remaining: 1 .

I have checked the code inside RateLimitMiddleware<TProcessor>
and figured out that the issue is here

// set X-Rate-Limit headers for the longest period
if (rulesDict.Any() && !_options.DisableRateLimitHeaders)
{ var rule = rulesDict.OrderByDescending(x => x.Key.PeriodTimespan).FirstOrDefault(); var headers = _processor.GetRateLimitHeaders(rule.Value, rule.Key, context.RequestAborted); headers.Context = context; context.Response.OnStarting(SetRateLimitHeaders, state: headers); }

in the first line of the if statement
var rule = rulesDict.OrderByDescending(x => x.Key.PeriodTimespan).FirstOrDefault();
it will fetch the data for the rule which has the longest_PeriodTimespan_, and I believe it shouldn't do so, it must fetch the data from the rule passed in the identity.
something like
var rule = rulesDict.FirstOrDefault(x => x.Key.Endpoint.Split(":")[1].ToLower() == identity.Path);

Please advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant