Class TokenBucketStrategy
java.lang.Object
dev.relism.flash.ext.limiter.strategy.TokenBucketStrategy
- All Implemented Interfaces:
RateLimitStrategy
Token-bucket rate limit: tokens refill continuously at a rate of
limit / windowMs tokens per millisecond, up to a maximum of limit tokens.
Each request consumes one token. Burst traffic is absorbed until the bucket empties.
Implementation
Bucket.slot0— current token count scaled by 1000L (allows sub-token precision without floating-point). Starts at 0; treated asmaxScaledwhenBucket.slot1is 0 (first call → bucket starts full).Bucket.slot1— last-refill timestamp in ms. 0 = not yet initialised.
Each request CAS-loops on slot0; slot1 is advanced monotonically via CAS
after a successful token consumption — never regresses to an older timestamp under concurrent load.
-
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
-
TokenBucketStrategy
public TokenBucketStrategy()
-
-
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
-