public abstract class AbstractBucket extends Object implements Bucket, BlockingBucket
| Constructor and Description |
|---|
AbstractBucket(BucketListener listener) |
| Modifier and Type | Method and Description |
|---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to bucket.
|
protected abstract CompletableFuture<Void> |
addTokensAsyncImpl(long tokensToAdd) |
protected abstract void |
addTokensImpl(long tokensToAdd) |
AsyncBucket |
asAsync()
Returns asynchronous view of this bucket.
|
AsyncScheduledBucket |
asAsyncScheduler()
Returns asynchronous view of this bucket that allows to use bucket as async scheduler.
|
BlockingBucket |
asScheduler()
Returns the
BlockingBucket view of this bucket, that provides operations which are able to block caller thread. |
void |
consume(long tokensToConsume,
BlockingStrategy blockingStrategy)
Consumes a specified number of tokens from the bucket.
|
protected abstract long |
consumeAsMuchAsPossibleImpl(long limit) |
void |
consumeUninterruptibly(long tokensToConsume,
UninterruptibleBlockingStrategy blockingStrategy)
Has same semantic with
BlockingBucket.consume(long, BlockingStrategy) but ignores interrupts(just restores interruption flag on exit). |
void |
replaceConfiguration(BucketConfiguration newConfiguration)
Replaces configuration of this bucket instance.
|
protected abstract CompletableFuture<Void> |
replaceConfigurationAsyncImpl(BucketConfiguration newConfiguration) |
protected abstract void |
replaceConfigurationImpl(BucketConfiguration newConfiguration) |
protected abstract CompletableFuture<Long> |
reserveAndCalculateTimeToSleepAsyncImpl(long tokensToConsume,
long maxWaitTimeNanos) |
protected abstract long |
reserveAndCalculateTimeToSleepImpl(long tokensToConsume,
long waitIfBusyNanos) |
boolean |
tryConsume(long tokensToConsume)
Tries to consume a specified number of tokens from this bucket.
|
boolean |
tryConsume(long tokensToConsume,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
Tries to consume a specified number of tokens from the bucket.
|
ConsumptionProbe |
tryConsumeAndReturnRemaining(long tokensToConsume)
Tries to consume a specified number of tokens from this bucket.
|
protected abstract CompletableFuture<ConsumptionProbe> |
tryConsumeAndReturnRemainingTokensAsyncImpl(long tokensToConsume) |
protected abstract ConsumptionProbe |
tryConsumeAndReturnRemainingTokensImpl(long tokensToConsume) |
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. |
protected abstract CompletableFuture<Long> |
tryConsumeAsMuchAsPossibleAsyncImpl(long limit) |
protected abstract CompletableFuture<Boolean> |
tryConsumeAsyncImpl(long tokensToConsume) |
protected abstract boolean |
tryConsumeImpl(long tokensToConsume) |
boolean |
tryConsumeUninterruptibly(long tokensToConsume,
long maxWaitTimeNanos,
UninterruptibleBlockingStrategy blockingStrategy)
Has same semantic with
BlockingBucket.tryConsume(long, long, BlockingStrategy) but ignores interrupts(just restores interruption flag on exit). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcreateSnapshot, getAvailableTokens, isAsyncModeSupported, toListenableconsume, consumeUninterruptibly, tryConsume, tryConsume, tryConsume, tryConsumeUninterruptibly, tryConsumeUninterruptibly, tryConsumeUninterruptiblypublic AbstractBucket(BucketListener listener)
protected abstract long consumeAsMuchAsPossibleImpl(long limit)
protected abstract boolean tryConsumeImpl(long tokensToConsume)
protected abstract ConsumptionProbe tryConsumeAndReturnRemainingTokensImpl(long tokensToConsume)
protected abstract long reserveAndCalculateTimeToSleepImpl(long tokensToConsume,
long waitIfBusyNanos)
protected abstract void addTokensImpl(long tokensToAdd)
protected abstract CompletableFuture<Long> tryConsumeAsMuchAsPossibleAsyncImpl(long limit)
protected abstract CompletableFuture<Boolean> tryConsumeAsyncImpl(long tokensToConsume)
protected abstract CompletableFuture<ConsumptionProbe> tryConsumeAndReturnRemainingTokensAsyncImpl(long tokensToConsume)
protected abstract CompletableFuture<Long> reserveAndCalculateTimeToSleepAsyncImpl(long tokensToConsume, long maxWaitTimeNanos)
protected abstract CompletableFuture<Void> addTokensAsyncImpl(long tokensToAdd)
protected abstract void replaceConfigurationImpl(BucketConfiguration newConfiguration)
protected abstract CompletableFuture<Void> replaceConfigurationAsyncImpl(BucketConfiguration newConfiguration)
public AsyncBucket asAsync()
BucketIf asynchronous mode is not supported by particular extension behind this bucket,
then any attempt to call this method will fail with UnsupportedOperationException.
public AsyncScheduledBucket asAsyncScheduler()
BucketIf asynchronous mode is not supported by particular extension behind this bucket,
then any attempt to call this method will fail with UnsupportedOperationException.
asAsyncScheduler in interface Bucketpublic BlockingBucket asScheduler()
BucketBlockingBucket view of this bucket, that provides operations which are able to block caller thread.asScheduler in interface Bucketpublic boolean tryConsume(long tokensToConsume)
BuckettryConsume in interface BuckettokensToConsume - The number of tokens to consume from the bucket, must be a positive number.true if the tokens were consumed, false otherwise.public boolean tryConsume(long tokensToConsume,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
throws InterruptedException
BlockingBucketThe algorithm is following:
maxWaitTimeNanos nanoseconds,
then consumes nothing and returns false immediately.
maxWaitTimeNanos nanoseconds,
then tokens consumed(reserved in fair manner) from bucket and current thread blocked for a time required to close deficit,
after unblocking method returns true.
Note: If InterruptedException happen when thread was blocked
then tokens will be not returned back to bucket,
but you can use Bucket.addTokens(long) to returned tokens back.
tryConsume in interface BlockingBuckettokensToConsume - The number of tokens to consume from the bucket.maxWaitTimeNanos - limit of time(in nanoseconds) which thread can wait.blockingStrategy - specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketnumTokens has been consumed or false when numTokens has not been consumedInterruptedException - in case of current thread has been interrupted during the waitingpublic boolean tryConsumeUninterruptibly(long tokensToConsume,
long maxWaitTimeNanos,
UninterruptibleBlockingStrategy blockingStrategy)
BlockingBucketBlockingBucket.tryConsume(long, long, BlockingStrategy) but ignores interrupts(just restores interruption flag on exit).tryConsumeUninterruptibly in interface BlockingBuckettokensToConsume - The number of tokens to consume from the bucket.maxWaitTimeNanos - limit of time(in nanoseconds) which thread can wait.blockingStrategy - specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketnumTokens has been consumed or false when numTokens has not been consumedBlockingBucket.tryConsume(long, long, BlockingStrategy)public void consume(long tokensToConsume,
BlockingStrategy blockingStrategy)
throws InterruptedException
BlockingBucketThe algorithm is following:
Bucket.addTokens(long) to returned tokens back.
consume in interface BlockingBuckettokensToConsume - The number of tokens to consume from the bucket.blockingStrategy - specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketInterruptedException - in case of current thread has been interrupted during the waitingpublic void consumeUninterruptibly(long tokensToConsume,
UninterruptibleBlockingStrategy blockingStrategy)
BlockingBucketBlockingBucket.consume(long, BlockingStrategy) but ignores interrupts(just restores interruption flag on exit).consumeUninterruptibly in interface BlockingBuckettokensToConsume - The number of tokens to consume from the bucket.blockingStrategy - specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketBlockingBucket.consume(long, BlockingStrategy)public long tryConsumeAsMuchAsPossible(long limit)
Bucketlimit.tryConsumeAsMuchAsPossible in interface Bucketlimit - maximum number of tokens to consume, should be positive.public long tryConsumeAsMuchAsPossible()
BuckettryConsumeAsMuchAsPossible in interface Bucketpublic ConsumptionProbe tryConsumeAndReturnRemaining(long tokensToConsume)
BuckettryConsumeAndReturnRemaining in interface BuckettokensToConsume - 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.public void addTokens(long tokensToAdd)
BucketnewTokens = 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);
}
};
public void replaceConfiguration(BucketConfiguration newConfiguration)
BucketRules 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.
replaceConfiguration in interface BucketnewConfiguration - the new configuration for this bucketCopyright © 2018. All rights reserved.