Skip to content

LinearRateLimiter is a fixed-window counter and allows up to 2x the configured rate across window boundaries #3740

Todo

Description

@csviri

Problem

LinearRateLimiter implements a fixed-window counter: per resource it keeps a count and a window start, resets both once the period has elapsed, and allows a reconciliation while count < limit.

The rate limiting docs describe a stronger guarantee:

The following configuration limits each resource to reconcile at most twice within a 3 second interval:
@RateLimited(maxReconciliations = 2, within = 3, unit = TimeUnit.SECONDS)

A fixed window only enforces this per window, not over an arbitrary interval. Across a window boundary a resource can be reconciled up to 2 × maxReconciliations times in a short span.

Example

maxReconciliations = 2, within = 3s, window started at t=0:

t=2.85  allowed (1)
t=2.90  allowed (2)
t=2.95  limited, rescheduled
t=3.05  window reset -> allowed (1)
t=3.06  allowed (2)

That's 4 reconciliations in about 0.2 s, so the documented "at most 2 within 3 seconds" doesn't hold. Since reconciliations are short and events tend to come in bursts, this is a realistic case.

The name LinearRateLimiter adds to the confusion, because it suggests evenly spaced permits (leaky/token bucket). What actually happens is that a burst up to the limit goes through, then nothing is allowed until the window ends.

Proposed solution

Replace the counter with a sliding log. Per resource, keep a ring buffer of the last maxReconciliations grant timestamps:

  • allowed if the buffer is not full, or if now - oldest >= period; record now
  • otherwise limited, with wait oldest + period - now

This enforces exactly "at most N within any interval of length T", matching the docs and the annotation. It keeps the existing Optional<Duration> isLimited(...) contract and costs N longs per resource (N is typically small).

Alternatives:

  • Token bucket / GCRA: O(1) state and evenly spaced permits, but a different contract from "N within T". The annotation and docs would need rewording.
  • Keep the fixed window and document it as such, including the up-to-2×N burst at window boundaries.

Related smaller issues in the RateLimiter SPI

  • RateLimiter<S extends RateLimitState> declares S, but isLimited(RateLimitState) takes the base type. Implementations need an instanceof check, and LinearRateLimiter fails open (returns "not limited") on a state of the wrong type. This comes from EventProcessor holding a RateLimiter<? extends RateLimitState> and the config classes using raw RateLimiter. A capture helper would let the signature be isLimited(S).
  • isLimited sounds like a query but consumes a permit. tryAcquire would better describe what it does.
  • No validation of @RateLimited values: within = 0 silently disables limiting (every call resets the window), and negative values are accepted.

What works correctly

The EventProcessor integration is sound:

  • Every submission path (new events, events received during execution, retry and reschedule timers) goes through the rate limiter, so it does take precedence over retry/reschedule as documented.
  • Rate-limited events stay marked and are rescheduled, so they're postponed, not lost.
  • The state is only accessed under the EventProcessor lock.

Activity

  1. added
    kind/bug
    on Dec 3, 2026
  2. added this to the 5.8 milestone on Dec 3, 2026
  3. ash-thakur-rh commented on Dec 4, 2026

    @ash-thakur-rh

    I am looking into this issue.

  4. ash-thakur-rh commented on Dec 4, 2026

    @ash-thakur-rh

    I’m keeping PR #3744 focused on the rolling-window fix. The sliding log enforces the documented per-resource limit while preserving the existing Optional<Duration> isLimited(...) contract.

    I’ll track configuration validation separately, including how to handle zero or negative window durations when rate limiting is enabled.

    I’ll open a separate follow-up to make the RateLimiter permit method accept the limiter’s own state type and rename it to tryAcquire. Those changes would affect custom limiter implementations, so I’d target them together for the next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

corekind/bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions