Skip to content

Rate Limiting works incorrectly if PeriodTimespan value is greater than Period #1590

@sergio-str

Description

@sergio-str

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 the if (entry.HasValue) block:

// 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

  1. Set Rate Limiting configuration parameters:
  • Period: 1 second
  • PeriodTimespan: 30 seconds
  • Limit: 100
  1. Generate a consequent request flow (10-20 requests per second) for more than 12 seconds
  2. Ensure Ocelot returns status 429

Specifications

Metadata

Metadata

Labels

Rate LimitingOcelot feature: Rate LimitingSpring'24Spring 2024 releasebugIdentified as a potential bughighHigh prioritymergedIssue has been merged to dev and is waiting for the next release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions