public class RateLimiterTokenBucket extends Object
RateLimiterTokenBucket keeps track of past throttling responses and adapts to slow down the send rate to adapt to
the service. It does this by suggesting a delay amount as result of a tryAcquire() call. Callers must update its
internal state by calling updateRateAfterThrottling() when getting a throttling response or
updateRateAfterSuccess() when getting successful response.
This class is thread-safe, its internal current state is kept in the inner class PersistentState which is stored
using an AtomicReference. This class is converted to TransientState when the state needs to be mutated and
converted back to a PersistentState and stored using AtomicReference.compareAndSet(Object, Object).
The algorithm used is adapted from the network congestion avoidance algorithm CUBIC.
| Modifier and Type | Method and Description |
|---|---|
RateLimiterAcquireResponse |
tryAcquire()
Acquire tokens from the bucket.
|
RateLimiterUpdateResponse |
updateRateAfterSuccess()
Updates the estimated send rate after a successful response.
|
RateLimiterUpdateResponse |
updateRateAfterThrottling()
Updates the estimated send rate after a throttling response.
|
public RateLimiterAcquireResponse tryAcquire()
RateLimiterAcquireResponse.delay() a Duration.ZERO value, otherwise it will return the amount of time the
callers need to wait until enough tokens are refilled.public RateLimiterUpdateResponse updateRateAfterThrottling()
public RateLimiterUpdateResponse updateRateAfterSuccess()
Copyright © 2026. All rights reserved.