Class RateLimitingTokenBucket

java.lang.Object
software.amazon.awssdk.core.internal.retry.RateLimitingTokenBucket

public class RateLimitingTokenBucket extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    acquire(double amount)
    Acquire tokens from the bucket.
    boolean
    acquire(double amount, boolean fastFail)
    Acquire tokens from the bucket.
    acquireNonBlocking(double amount, boolean fastFail)
    Acquire capacity from the rate limiter without blocking the call.
    void
    updateClientSendingRate(boolean throttlingResponse)
    _UpdateClientSendingRate(response) _UpdateMeasuredRate() if IsThrottlingError(response) if not enabled rate_to_use = measured_tx_rate else rate_to_use = min(measured_tx_rate, fill_rate) # The fill_rate is from the token bucket.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RateLimitingTokenBucket

      public RateLimitingTokenBucket()
  • Method Details

    • acquire

      public boolean acquire(double amount)
      Acquire tokens from the bucket. If the bucket contains enough capacity to satisfy the request, this method will return immediately, otherwise the method will block the calling thread until enough tokens are refilled.

       _TokenBucketAcquire(amount)
         # Client side throttling is not enabled until we see a throttling error.
         if not enabled
           return
      
         _TokenBucketRefill()
         # Next see if we have enough capacity for the requested amount.
         if amount <= current_capacity
           current_capacity = current_capacity - amount
         else
           sleep((amount - current_capacity) / fill_rate)
           current_capacity = current_capacity - amount
         return
       

      This is equivalent to acquire(amount, false).

      Parameters:
      amount - The amount of tokens to acquire.
      Returns:
      Whether the amount was successfully acquired.
    • acquire

      public boolean acquire(double amount, boolean fastFail)
      Acquire tokens from the bucket. If the bucket contains enough capacity to satisfy the request, this method will return immediately. Otherwise, the behavior depends on the value of fastFail. If it is true, then it will return false immediately, signaling that enough capacity could not be acquired. Otherwise if fastFail is false, then it will wait the required amount of time to fill the bucket with enough tokens to satisfy amount.
       _TokenBucketAcquire(amount)
         # Client side throttling is not enabled until we see a throttling error.
         if not enabled
           return
      
         _TokenBucketRefill()
         # Next see if we have enough capacity for the requested amount.
         if amount <= current_capacity
           current_capacity = current_capacity - amount
         else
           sleep((amount - current_capacity) / fill_rate)
           current_capacity = current_capacity - amount
         return
       
      Parameters:
      amount - The amount of tokens to acquire.
      fastFail - Whether this method should return immediately instead of waiting if amount exceeds the current capacity.
      Returns:
      Whether the amount was successfully acquired.
    • acquireNonBlocking

      public OptionalDouble acquireNonBlocking(double amount, boolean fastFail)
      Acquire capacity from the rate limiter without blocking the call.

      This method returns an OptionalDouble whose value, or its absence correspond to the following states:

      • Empty - If the value is not present, then the call fast failed, and no capacity was acquired.
      • Present - if the value is present, then the value is the time in seconds that caller must wait before executing the request to be within the rate imposed by the rate limiter./li>
      Returns:
      The amount of time in seconds to wait before proceeding.
    • updateClientSendingRate

      public void updateClientSendingRate(boolean throttlingResponse)
       _UpdateClientSendingRate(response)
         _UpdateMeasuredRate()
      
         if IsThrottlingError(response)
           if not enabled
             rate_to_use = measured_tx_rate
           else
             rate_to_use = min(measured_tx_rate, fill_rate)
      
           # The fill_rate is from the token bucket.
           last_max_rate = rate_to_use
           _CalculateTimeWindow()
           last_throttle_time = time()
           calculated_rate = _CUBICThrottle(rate_to_use)
           TokenBucketEnable()
         else
           _CalculateTimeWindow()
           calculated_rate = _CUBICSuccess(time())
      
         new_rate = min(calculated_rate, 2 * measured_tx_rate)
         _TokenBucketUpdateRate(new_rate)