public interface Bucket
Use following links for further details:
Modifier and Type | Method and Description |
---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to bucket.
|
BlockingBucket |
asBlocking()
Returns the
BlockingBucket view of this bucket, that provides operations which are able to block caller thread. |
ScheduledBucket |
asScheduler()
TODO
|
VerboseBucket |
asVerbose()
Returns the verbose view of this bucket.
|
static LocalBucketBuilder |
builder()
Creates the new builder of in-memory buckets.
|
long |
consumeIgnoringRateLimits(long tokens)
Consumes
tokens from bucket ignoring all limits. |
BucketState |
createSnapshot()
Creates the copy of internal state.
|
EstimationProbe |
estimateAbilityToConsume(long numTokens)
Estimates ability to consume a specified number of tokens.
|
long |
getAvailableTokens()
Returns amount of available tokens in this bucket.
|
void |
replaceConfiguration(BucketConfiguration newConfiguration)
Replaces configuration of this bucket instance.
|
Bucket |
toListenable(BucketListener listener)
Returns new copy of this bucket instance decorated by
listener . |
boolean |
tryConsume(long numTokens)
Tries to consume a specified number of tokens from this bucket.
|
ConsumptionProbe |
tryConsumeAndReturnRemaining(long numTokens)
Tries to consume a specified number of tokens from this bucket.
|
long |
tryConsumeAsMuchAsPossible()
Tries to consume as much tokens from this bucket as available at the moment of invocation.
|
long |
tryConsumeAsMuchAsPossible(long limit)
Tries to consume as much tokens from bucket as available in the bucket at the moment of invocation,
but tokens which should be consumed is limited by
limit . |
static LocalBucketBuilder builder()
LocalBucketBuilder
BlockingBucket asBlocking()
BlockingBucket
view of this bucket, that provides operations which are able to block caller thread.ScheduledBucket asScheduler()
VerboseBucket asVerbose()
boolean tryConsume(long numTokens)
numTokens
- The number of tokens to consume from the bucket, must be a positive number.true
if the tokens were consumed, false
otherwise.long consumeIgnoringRateLimits(long tokens)
tokens
from bucket ignoring all limits.
In result of this operation amount of tokens in the bucket could became negative.
There are two possible reasons to use this method:
asScheduler().consume(tokens)
tokens
- amount of tokens that should be consumed from bucket.consumeIgnoringRateLimits(2)
,
after invocation there are 3 tokens remain in the bucket, since limits were not violated zero returned as result.consumeIgnoringRateLimits(6)
called.
300_000_000 will be returned as result and available tokens in the bucket will became -3, and any variation of tryConsume...
will not be successful for 400 milliseconds(time required to refill amount of available tokens until 1).
ConsumptionProbe tryConsumeAndReturnRemaining(long numTokens)
numTokens
- The number of tokens to consume from the bucket, must be a positive number.ConsumptionProbe
which describes both result of consumption and tokens remaining in the bucket after consumption.EstimationProbe estimateAbilityToConsume(long numTokens)
numTokens
- The number of tokens to consume, must be a positive number.EstimationProbe
which describes the ability to consume.long tryConsumeAsMuchAsPossible()
long tryConsumeAsMuchAsPossible(long limit)
limit
.limit
- maximum number of tokens to consume, should be positive.void addTokens(long tokensToAdd)
newTokens = Math.min(capacity, currentTokens + tokensToAdd)in other words resulted number of tokens never exceeds capacity independent of tokensToAdd.
Bucket wallet;
...
if(wallet.tryConsume(50)) {// get 50 cents from wallet
try {
buyCocaCola();
} catch(NoCocaColaException e) {
// return money to wallet
wallet.addTokens(50);
}
};
tokensToAdd
- number of tokens to addlong getAvailableTokens()
This method designed to be used only for monitoring and testing, you should never use this method for business cases, because available tokens can be changed by concurrent transactions for case of multithreaded/multi-process environment.
void replaceConfiguration(BucketConfiguration newConfiguration)
Rules of reconfiguration:
IncompatibleConfigurationException
will be thrown.
In other words you must not try to reduce or increase count of bandwidths,
it is impossible to create bucket with one bandwidth and reconfigure to use two bandwidths and vice-versa.
newConfiguration
- the new configuration for this bucketIncompatibleConfigurationException
- if newConfiguration
incompatible with previous configurationBucketState createSnapshot()
This method is designed to be used only for monitoring and testing, you should never use this method for business cases.
Bucket toListenable(BucketListener listener)
listener
.
The created bucket will share same tokens with source bucket and vice versa.
See javadocs for BucketListener
in order to understand semantic of listener.listener
- the listener of bucket events.listener
Copyright © 2020. All rights reserved.