Class FixedWindowStrategy
java.lang.Object
dev.relism.flash.ext.limiter.strategy.FixedWindowStrategy
- All Implemented Interfaces:
RateLimitStrategy
Fixed-window rate limit: allows up to
LimitConfig.limit() requests per window of
LimitConfig.windowMs() milliseconds. The window is aligned to clock time
(e.g. 10:00:00 – 10:00:59 for a 60-second window), not sliding.
Implementation
The entire state fits in a singleAtomicLong
(Bucket.slot0), packed as:
high 32 bits = reduced epoch (currentTimeMs / windowMs) invalid input: '&' 0xFFFFFFFFL
low 32 bits = request count in the current window
Each request performs a single CAS loop — no locks, no allocations.
At a window boundary the CAS atomically resets the counter to 1.
The reduced epoch wraps every 2^32 × windowMs milliseconds
(~13,000 years for a 100 ms window) — collision-free in practice.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheck(Bucket bucket, LimitConfig cfg, long[] out) Checks whether this request is within the limit and updates the bucket atomically.
-
Constructor Details
-
FixedWindowStrategy
public FixedWindowStrategy()
-
-
Method Details
-
check
Description copied from interface:RateLimitStrategyChecks whether this request is within the limit and updates the bucket atomically.On return,
outcontains:out[0]— remaining allowed requests in the current window (≥ 0).out[1]— Unix epoch seconds at which the quota resets (forX-RateLimit-ResetandRetry-Afterheaders).
- Specified by:
checkin interfaceRateLimitStrategy- Parameters:
bucket- per-key state carrier (pre-allocated, never null)cfg- immutable rule configurationout- caller-supplied two-element array; values are overwritten on every call- Returns:
trueif the request is within the limit and should proceed
-