Package com.linkedin.venice.throttle
Class TokenBucket
java.lang.Object
com.linkedin.venice.throttle.TokenBucket
- All Implemented Interfaces:
VeniceRateLimiter
- See Also:
-
- Token Bucket on Wikipedia This implementation aims to be very high performance with the goal of supporting a very large number of TokenBuckets in an application; thus avoiding an auxilliary thread to refill the bucket.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.linkedin.venice.throttle.VeniceRateLimiter
VeniceRateLimiter.RateLimiterType
-
Constructor Summary
ConstructorDescriptionTokenBucket
(long capacity, long refillAmount, long refillInterval, TimeUnit refillUnit, Clock clock) This constructor should only be used by tests. -
Method Summary
Modifier and TypeMethodDescriptionfloat
long
long
long
getQuota()
long
long
This method does not call #update(), so it is only accurate as of the last time #tryConsume() was calleddouble
void
setQuota
(long quota) The following methods are used only when checking if the new quota requests are different from the existing quota.static TokenBucket
tokenBucketFromRcuPerSecond
(long totalRcuPerSecond, double thisBucketProportionOfTotalRcu, long enforcementIntervalMilliseconds, int enforcementCapacityMultiple, Clock clock) toString()
boolean
tryAcquirePermit
(int units) Try to acquire permit for the given rcu.boolean
tryConsume
(long tokensToConsume)
-
Constructor Details
-
TokenBucket
public TokenBucket(long capacity, long refillAmount, long refillInterval, TimeUnit refillUnit, Clock clock) This constructor should only be used by tests. Application should not specify it's own instance of Clock- Parameters:
capacity
-refillAmount
-refillInterval
-refillUnit
-clock
-
-
-
Method Details
-
getStaleTokenCount
public long getStaleTokenCount()This method does not call #update(), so it is only accurate as of the last time #tryConsume() was called- Returns:
- number of tokens remaining in the bucket
-
tryConsume
public boolean tryConsume(long tokensToConsume) -
getAmortizedRefillPerSecond
public float getAmortizedRefillPerSecond() -
getStaleUsageRatio
public double getStaleUsageRatio() -
tryAcquirePermit
public boolean tryAcquirePermit(int units) Description copied from interface:VeniceRateLimiter
Try to acquire permit for the given rcu. Will not block if permit is not available.- Specified by:
tryAcquirePermit
in interfaceVeniceRateLimiter
- Parameters:
units
- Number of units to acquire.- Returns:
- true if permit is acquired, false otherwise.
-
tokenBucketFromRcuPerSecond
public static TokenBucket tokenBucketFromRcuPerSecond(long totalRcuPerSecond, double thisBucketProportionOfTotalRcu, long enforcementIntervalMilliseconds, int enforcementCapacityMultiple, Clock clock) - Parameters:
totalRcuPerSecond
- Number of units per second to allowthisBucketProportionOfTotalRcu
- For maximum fidelity of calculations. If you need a smaller portion of the RCU to be applied then set this to an appropriate multiplier. Otherwise set this to 1.- Returns:
- See Also:
-
setQuota
public void setQuota(long quota) Description copied from interface:VeniceRateLimiter
The following methods are used only when checking if the new quota requests are different from the existing quota.- Specified by:
setQuota
in interfaceVeniceRateLimiter
-
getQuota
public long getQuota()- Specified by:
getQuota
in interfaceVeniceRateLimiter
-
toString
-
getCapacity
public long getCapacity() -
getRefillAmount
public long getRefillAmount() -
getEnforcementInterval
public long getEnforcementInterval()
-