Ocelot
Ocelot copied to clipboard
Rate Limiting works incorrectly if PeriodTimespan value is greater than Period
Expected Behavior
Rate Limiting works as documented for such parameter values: Period: 1 second PeriodTimeSpan: 30 seconds Limit: 100
Scenario: Request flow, 10-20 requests per sec, should be allowed.
Status: not equal to 429
Actual Behavior / Motivation for New Feature
With the same parameter values from Expected Behavior Ocelot returns status 429 aka "Rate limit exceeded". It seems this issue caused by code in RateLimitCore.cs lines 35-43
// entry has not expired
if (entry.Value.Timestamp + TimeSpan.FromSeconds(rule.PeriodTimespan) >= DateTime.UtcNow)
{
// increment request count
var totalRequests = entry.Value.TotalRequests + 1;
// deep copy
counter = new RateLimitCounter(entry.Value.Timestamp, totalRequests);
}
Ocelot increments request counter if request has been received in interval [FirstRequestTime : FirstRequestTime + PeriodTimeSpan] According to the documentation counter should be incremented if request is in interval [FirstRequestTime : FirstRequestTime + Period].
Steps to Reproduce the Problem
- Set Rate Limiting configuration parameters:
- Period: 1 second
- PeriodTimeSpan: 30 seconds
- Limit: 100
- Generate a consequent request flow (10-20 requests per second) for more than 12 seconds
- Ensure Ocelot returns status 429
Specifications
- Ocelot Version: Latest, 19.0.2
- Feature: Rate Limiting
Hi Sergii!
Do not you mind if we collaborate on the issue description first?
Check the issue Title please! Is this title correct?
Sergii, I've changed Expected Behavior description. Could you confirm it's correct please? I need to know we are on the same page.
I think yes. But give me a little time, please, to dive into the context. Also I apologize for inacurate title.
So, bug description has been changed and updated. Could you review it plz?
@sergio-str Hi Sergii! Do you accept current description?
@sergio-str Sergii
I would like your checking this bug for the latest build/version which is 19.0.2 and .NET 7. Your reported Specifications info with "Ocelot Version: 18" is quite outdated. Please, use the latest package and/or just use top commits from develop branch version!
Do not you mind if I change Ocelot Version to 19.0.2 in Specifications paragraph? That's how I and team can accept this bug after our common validation. And the team will not accept bugs for old versions, because of outdating.
The issue has been accepted due to open PR #1592
- #1592
Also, is it possible to configure RateLimiting globally? Looks like the limits work only when specified at Route level. It's inconvenient to configure Limits for large number of routes. Limits configured at Route level can then take precedence over Global configuration.
@MithunChopda commented on Nov 3
Hi Mithun! No, impossible! You have to configure Rate Limiter for each route. Look at this PR #1659 for global Headers Transformation. If you will be inspired by this PR, you could open new PR especially for global Rate Liming.