Class RateLimitingTokenBucket
java.lang.Object
software.amazon.awssdk.core.internal.retry.RateLimitingTokenBucket
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
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.
-
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 offastFail
. If it istrue
, then it will returnfalse
immediately, signaling that enough capacity could not be acquired. Otherwise iffastFail
isfalse
, then it will wait the required amount of time to fill the bucket with enough tokens to satisfyamount
._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 ifamount
exceeds the current capacity.- Returns:
- Whether the amount was successfully acquired.
-
acquireNonBlocking
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)
-