Class TokenBucket
- java.lang.Object
-
- software.amazon.awssdk.core.internal.capacity.TokenBucket
-
public class TokenBucket extends Object
A lock-free implementation of a token bucket. Tokens can be acquired from the bucket as long as there is sufficient capacity in the bucket.
-
-
Constructor Summary
Constructors Constructor Description TokenBucket(int maxCapacity)
Create a bucket containing the specified number of tokens.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
currentCapacity()
Retrieve a snapshot of the current number of tokens in the bucket.boolean
equals(Object o)
int
hashCode()
int
maxCapacity()
Retrieve the maximum capacity of the bucket configured when the bucket was created.void
release(int amountToRelease)
Release a certain number of tokens back to this bucket.Optional<TokenBucketRetryCondition.Capacity>
tryAcquire(int amountToAcquire)
Try to acquire a certain number of tokens from this bucket.
-
-
-
Method Detail
-
tryAcquire
public Optional<TokenBucketRetryCondition.Capacity> tryAcquire(int amountToAcquire)
Try to acquire a certain number of tokens from this bucket. If there aren't sufficient tokens in this bucket,Optional.empty()
is returned.
-
release
public void release(int amountToRelease)
Release a certain number of tokens back to this bucket. If this number of tokens would exceed the maximum number of tokens configured for the bucket, the bucket is instead set to the maximum value and the additional tokens are discarded.
-
currentCapacity
public int currentCapacity()
Retrieve a snapshot of the current number of tokens in the bucket. Because this number is constantly changing, it's recommended to refer to theTokenBucketRetryCondition.Capacity.capacityRemaining()
returned by thetryAcquire(int)
method whenever possible.
-
maxCapacity
public int maxCapacity()
Retrieve the maximum capacity of the bucket configured when the bucket was created.
-
-