Class DefaultAsyncBucketProxy
- java.lang.Object
-
- io.github.bucket4j.distributed.proxy.DefaultAsyncBucketProxy
-
- All Implemented Interfaces:
AsyncBucketProxy,AsyncOptimizationController
public class DefaultAsyncBucketProxy extends Object implements AsyncBucketProxy, AsyncOptimizationController
-
-
Constructor Summary
Constructors Constructor Description DefaultAsyncBucketProxy(AsyncCommandExecutor commandExecutor, RecoveryStrategy recoveryStrategy, Supplier<CompletableFuture<BucketConfiguration>> configurationSupplier, ImplicitConfigurationReplacement implicitConfigurationReplacement, BucketListener listener)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Void>addTokens(long tokensToAdd)Asynchronous version ofBucket.addTokens(long), follows the same semantic.SchedulingBucketasScheduler()Returns asynchronous view of this bucket that allows to use bucket as async scheduler.AsyncVerboseBucketasVerbose()Returns the verbose view of this bucket.CompletableFuture<Long>consumeIgnoringRateLimits(long tokensToConsume)Asynchronous version ofBucket.consumeIgnoringRateLimits(long), follows the same semantic.CompletableFuture<EstimationProbe>estimateAbilityToConsume(long numTokens)Asynchronous version ofBucket.estimateAbilityToConsume(long), follows the same semantic.CompletableFuture<Void>forceAddTokens(long tokensToAdd)Asynchronous version ofBucket.forceAddTokens(long), follows the same semantic.CompletableFuture<Long>getAvailableTokens()Returns amount of available tokens in this bucket.AsyncOptimizationControllergetOptimizationController()Returns optimization controller for this proxy.CompletableFuture<Void>replaceConfiguration(BucketConfiguration newConfiguration, TokensInheritanceStrategy tokensInheritanceStrategy)Has the same semantic withBucket.replaceConfiguration(BucketConfiguration, TokensInheritanceStrategy)CompletableFuture<Void>reset()Reset all tokens up to maximum capacity.CompletableFuture<Void>syncByCondition(long unsynchronizedTokens, Duration timeSinceLastSync)Initiates immediate synchronization of local copy of bucket with remote storage in case of both conditions bellow aretrue: Accumulated amount of locally consumed tokens without external synchronization is greater than or equal tounsynchronizedTokensTime passed since last synchronization with external storage is greater than or equal totimeSinceLastSyncAsyncBucketProxytoListenable(BucketListener listener)Returns new copy of this bucket instance decorated bylistener.CompletableFuture<Boolean>tryConsume(long tokensToConsume)Asynchronous version ofBucket.tryConsume(long), follows the same semantic.CompletableFuture<ConsumptionProbe>tryConsumeAndReturnRemaining(long tokensToConsume)Asynchronous version ofBucket.tryConsumeAndReturnRemaining(long), follows the same semantic.CompletableFuture<Long>tryConsumeAsMuchAsPossible()Asynchronous version ofBucket.tryConsumeAsMuchAsPossible(), follows the same semantic.CompletableFuture<Long>tryConsumeAsMuchAsPossible(long limit)Asynchronous version ofBucket.tryConsumeAsMuchAsPossible(long), follows the same semantic.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.github.bucket4j.distributed.AsyncOptimizationController
syncImmediately
-
-
-
-
Constructor Detail
-
DefaultAsyncBucketProxy
public DefaultAsyncBucketProxy(AsyncCommandExecutor commandExecutor, RecoveryStrategy recoveryStrategy, Supplier<CompletableFuture<BucketConfiguration>> configurationSupplier, ImplicitConfigurationReplacement implicitConfigurationReplacement, BucketListener listener)
-
-
Method Detail
-
asVerbose
public AsyncVerboseBucket asVerbose()
Description copied from interface:AsyncBucketProxyReturns the verbose view of this bucket.- Specified by:
asVerbosein interfaceAsyncBucketProxy
-
toListenable
public AsyncBucketProxy toListenable(BucketListener listener)
Description copied from interface:AsyncBucketProxyReturns new copy of this bucket instance decorated bylistener. The created bucket will share same tokens with source bucket and vice versa.See javadocs for
BucketListenerin order to understand semantic of listener.- Specified by:
toListenablein interfaceAsyncBucketProxy- Parameters:
listener- the listener of bucket events.- Returns:
- new bucket instance decorated by
listener
-
asScheduler
public SchedulingBucket asScheduler()
Description copied from interface:AsyncBucketProxyReturns asynchronous view of this bucket that allows to use bucket as async scheduler.- Specified by:
asSchedulerin interfaceAsyncBucketProxy- Returns:
- Asynchronous view of this bucket that allows to use bucket as async scheduler.
-
consumeIgnoringRateLimits
public CompletableFuture<Long> consumeIgnoringRateLimits(long tokensToConsume)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.consumeIgnoringRateLimits(long), follows the same semantic.- Specified by:
consumeIgnoringRateLimitsin interfaceAsyncBucketProxy- See Also:
Bucket.consumeIgnoringRateLimits(long)
-
tryConsume
public CompletableFuture<Boolean> tryConsume(long tokensToConsume)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.tryConsume(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.tryConsume(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
tryConsumein interfaceAsyncBucketProxy- Parameters:
tokensToConsume- The number of tokens to consume from the bucket, must be a positive number.- Returns:
- the future which eventually will be completed by
trueif the numTokens were consumed and completed byfalseotherwise. - See Also:
Bucket.tryConsume(long)
-
tryConsumeAndReturnRemaining
public CompletableFuture<ConsumptionProbe> tryConsumeAndReturnRemaining(long tokensToConsume)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.tryConsumeAndReturnRemaining(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.tryConsumeAndReturnRemaining(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
tryConsumeAndReturnRemainingin interfaceAsyncBucketProxy- Parameters:
tokensToConsume- The number of tokens to consume from the bucket, must be a positive number.- Returns:
- the future which eventually will be completed by
probewhich describes both result of consumption and tokens remaining in the bucket after consumption. - See Also:
Bucket.tryConsumeAndReturnRemaining(long)
-
estimateAbilityToConsume
public CompletableFuture<EstimationProbe> estimateAbilityToConsume(long numTokens)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.estimateAbilityToConsume(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.estimateAbilityToConsume(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
estimateAbilityToConsumein interfaceAsyncBucketProxy- Parameters:
numTokens- The number of tokens to consume from the bucket, must be a positive number.- Returns:
- the future which eventually will be completed by
probewhich describes the ability to consume specified amount of tokens. - See Also:
Bucket.estimateAbilityToConsume(long)
-
tryConsumeAsMuchAsPossible
public CompletableFuture<Long> tryConsumeAsMuchAsPossible()
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.tryConsumeAsMuchAsPossible(), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.tryConsumeAsMuchAsPossible() - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
tryConsumeAsMuchAsPossiblein interfaceAsyncBucketProxy- Returns:
- the future which eventually will be completed by number of tokens which has been consumed, or completed by zero if was consumed nothing.
- See Also:
Bucket.tryConsumeAsMuchAsPossible()
-
tryConsumeAsMuchAsPossible
public CompletableFuture<Long> tryConsumeAsMuchAsPossible(long limit)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.tryConsumeAsMuchAsPossible(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.tryConsumeAsMuchAsPossible(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
tryConsumeAsMuchAsPossiblein interfaceAsyncBucketProxy- Parameters:
limit- maximum number of tokens to consume, should be positive.- Returns:
- the future which eventually will be completed by number of tokens which has been consumed, or completed by zero if was consumed nothing.
- See Also:
Bucket.tryConsumeAsMuchAsPossible(long)
-
replaceConfiguration
public CompletableFuture<Void> replaceConfiguration(BucketConfiguration newConfiguration, TokensInheritanceStrategy tokensInheritanceStrategy)
Description copied from interface:AsyncBucketProxyHas the same semantic withBucket.replaceConfiguration(BucketConfiguration, TokensInheritanceStrategy)- Specified by:
replaceConfigurationin interfaceAsyncBucketProxy
-
addTokens
public CompletableFuture<Void> addTokens(long tokensToAdd)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.addTokens(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.addTokens(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
addTokensin interfaceAsyncBucketProxy- Parameters:
tokensToAdd- number of tokens to add- Returns:
- the future which eventually will be completed by null if operation successfully completed without exception, otherwise(if any exception happen in asynchronous flow) the future will be completed exceptionally.
- See Also:
Bucket.addTokens(long)
-
forceAddTokens
public CompletableFuture<Void> forceAddTokens(long tokensToAdd)
Description copied from interface:AsyncBucketProxyAsynchronous version ofBucket.forceAddTokens(long), follows the same semantic.The algorithm for distribute buckets is following:
- Implementation issues asynchronous request to back-end behind the bucket in way which specific for each particular back-end.
- Then uncompleted future returned to the caller.
- When back-end provides signal(through callback) that request is done, then future completed.
- If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
CompletableFuture.thenApplyAsync(Function, Executor).The algorithm for local buckets is following:
- Implementation just redirects request to synchronous version
Bucket.addTokens(long) - Then returns feature immediately completed by results from previous step. So using this method for local buckets is useless, because there are no differences with synchronous version.
- Specified by:
forceAddTokensin interfaceAsyncBucketProxy- Parameters:
tokensToAdd- number of tokens to add- Returns:
- the future which eventually will be completed by null if operation successfully completed without exception, otherwise(if any exception happen in asynchronous flow) the future will be completed exceptionally.
- See Also:
Bucket.addTokens(long)
-
reset
public CompletableFuture<Void> reset()
Description copied from interface:AsyncBucketProxyReset all tokens up to maximum capacity.- Specified by:
resetin interfaceAsyncBucketProxy
-
getAvailableTokens
public CompletableFuture<Long> getAvailableTokens()
Description copied from interface:AsyncBucketProxyReturns amount of available tokens in this bucket.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/multiprocess environment.
- Specified by:
getAvailableTokensin interfaceAsyncBucketProxy- Returns:
- amount of available tokens
-
getOptimizationController
public AsyncOptimizationController getOptimizationController()
Description copied from interface:AsyncBucketProxyReturns optimization controller for this proxy.This method is actual only if an optimization was applied during bucket construction via
RemoteAsyncBucketBuilder.withOptimization(Optimization)otherwise returned controller will do nothing.- Specified by:
getOptimizationControllerin interfaceAsyncBucketProxy- Returns:
- optimization controller for this proxy
-
syncByCondition
public CompletableFuture<Void> syncByCondition(long unsynchronizedTokens, Duration timeSinceLastSync)
Description copied from interface:AsyncOptimizationControllerInitiates immediate synchronization of local copy of bucket with remote storage in case of both conditions bellow aretrue:- Accumulated amount of locally consumed tokens without external synchronization is greater than or equal to
unsynchronizedTokens - Time passed since last synchronization with external storage is greater than or equal to
timeSinceLastSync
- Specified by:
syncByConditionin interfaceAsyncOptimizationController- Parameters:
unsynchronizedTokens- criterion for accumulated amount of unsynchronized tokenstimeSinceLastSync- criterion for time passed since last synchronization- Returns:
- future that will be completed when local copy of bucket will be synchronized with remote storage, or immediately completed future in case of this synchronization is not required.
- Accumulated amount of locally consumed tokens without external synchronization is greater than or equal to
-
-