Class RateLimiterTokenBucket

java.lang.Object
software.amazon.awssdk.retries.internal.ratelimiter.RateLimiterTokenBucket

public class RateLimiterTokenBucket extends Object
The 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 RateLimiterTokenBucket.PersistentState which is stored using an AtomicReference. This class is converted to RateLimiterTokenBucket.TransientState when the state needs to be mutated and converted back to a RateLimiterTokenBucket.PersistentState and stored using AtomicReference.compareAndSet(Object, Object).

The algorithm used is adapted from the network congestion avoidance algorithm CUBIC.

  • Method Details

    • tryAcquire

      public RateLimiterAcquireResponse tryAcquire()
      Acquire tokens from the bucket. If the bucket contains enough capacity to satisfy the request, this method will return in RateLimiterAcquireResponse.delay() a Duration.ZERO value, otherwise it will return the amount of time the callers need to wait until enough tokens are refilled.
    • updateRateAfterThrottling

      public RateLimiterUpdateResponse updateRateAfterThrottling()
      Updates the estimated send rate after a throttling response.
    • updateRateAfterSuccess

      public RateLimiterUpdateResponse updateRateAfterSuccess()
      Updates the estimated send rate after a successful response.