public class TokenBucket extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
TokenBucket.Clock |
Constructor and Description |
---|
TokenBucket() |
Modifier and Type | Method and Description |
---|---|
boolean |
acquire(double amount)
Acquire tokens from the bucket.
|
boolean |
acquire(double amount,
boolean fastFail)
Acquire tokens from the bucket.
|
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.
|
public boolean acquire(double 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
This is equivalent to acquire(amount, false)
.
amount
- The amount of tokens to acquire.public boolean acquire(double amount, boolean fastFail)
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
amount
- The amount of tokens to acquire.fastFail
- Whether this method should return immediately instead
of waiting if amount
exceeds the current
capacity.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)
Copyright © 2023. All rights reserved.