Repository navigation
Description
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; recordnow - 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>declaresS, butisLimited(RateLimitState)takes the base type. Implementations need aninstanceofcheck, andLinearRateLimiterfails open (returns "not limited") on a state of the wrong type. This comes fromEventProcessorholding aRateLimiter<? extends RateLimitState>and the config classes using rawRateLimiter. A capture helper would let the signature beisLimited(S).isLimitedsounds like a query but consumes a permit.tryAcquirewould better describe what it does.- No validation of
@RateLimitedvalues:within = 0silently 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
EventProcessorlock.
Activity
ash-thakur-rh commentedon Dec 4, 2026
I am looking into this issue.
ash-thakur-rh commentedon Dec 4, 2026
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.