Package com.github.twitch4j.common.util
Class BucketUtils
java.lang.Object
com.github.twitch4j.common.util.BucketUtils
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull io.github.bucket4j.Bucket
createBucket
(@NotNull io.github.bucket4j.Bandwidth limit) Creates a bucket with the specified bandwidth.static @NotNull io.github.bucket4j.Bucket
createBucket
(@NotNull io.github.bucket4j.Bandwidth... limits) Creates a bucket with the specified bandwidths.static @NotNull io.github.bucket4j.Bucket
createBucket
(@NotNull Iterable<io.github.bucket4j.Bandwidth> limits) Creates a bucket with the specified bandwidths.static @NotNull CompletableFuture<Void>
scheduleAgainstBucket
(@NotNull io.github.bucket4j.Bucket bucket, @NotNull ScheduledExecutorService executor, @NotNull Runnable action) Runs the action after a token has been consumed from the bucket using the executor.static <T> @NotNull CompletableFuture<T>
scheduleAgainstBucket
(@NotNull io.github.bucket4j.Bucket bucket, @NotNull ScheduledExecutorService executor, @NotNull Callable<T> call) Performs the callable after a token has been consumed from the bucket using the executor.static io.github.bucket4j.Bandwidth
static io.github.bucket4j.Bandwidth
Creates a bandwidth with the specified capacity and refill rate.
-
Constructor Details
-
BucketUtils
public BucketUtils()
-
-
Method Details
-
simple
- Parameters:
capacity
- the bandwidth capacitygreedyRefillPeriod
- the amount of time for the bucket bandwidth to be completely refilled- Returns:
Bandwidth
- See Also:
-
simple
public static io.github.bucket4j.Bandwidth simple(long capacity, Duration greedyRefillPeriod, String id) Creates a bandwidth with the specified capacity and refill rate.Note: the greedy refill algorithm allows for exceeding the specified capacity within the refill period.
- Parameters:
capacity
- the bandwidth capacitygreedyRefillPeriod
- the amount of time for the bucket bandwidth to be completely refilledid
- the bandwidth id- Returns:
Bandwidth
-
createBucket
@NotNull public static @NotNull io.github.bucket4j.Bucket createBucket(@NotNull @NotNull io.github.bucket4j.Bandwidth limit) Creates a bucket with the specified bandwidth.- Parameters:
limit
- the bandwidth- Returns:
- the bucket
-
createBucket
@NotNull public static @NotNull io.github.bucket4j.Bucket createBucket(@NotNull @NotNull io.github.bucket4j.Bandwidth... limits) Creates a bucket with the specified bandwidths.- Parameters:
limits
- the bandwidths- Returns:
- the bucket
-
createBucket
@NotNull public static @NotNull io.github.bucket4j.Bucket createBucket(@NotNull @NotNull Iterable<io.github.bucket4j.Bandwidth> limits) Creates a bucket with the specified bandwidths.- Parameters:
limits
- the bandwidths- Returns:
- the bucket
-
scheduleAgainstBucket
@NotNull public static <T> @NotNull CompletableFuture<T> scheduleAgainstBucket(@NotNull @NotNull io.github.bucket4j.Bucket bucket, @NotNull @NotNull ScheduledExecutorService executor, @NotNull @NotNull Callable<T> call) Performs the callable after a token has been consumed from the bucket using the executor.Note: ExecutionException should be inspected if the passed action can throw an exception.
- Parameters:
bucket
- rate limit bucketexecutor
- scheduled executor service for async callscall
- task that requires a bucket point- Returns:
- the future result of the call
-
scheduleAgainstBucket
@NotNull public static @NotNull CompletableFuture<Void> scheduleAgainstBucket(@NotNull @NotNull io.github.bucket4j.Bucket bucket, @NotNull @NotNull ScheduledExecutorService executor, @NotNull @NotNull Runnable action) Runs the action after a token has been consumed from the bucket using the executor.Note: while the executor is used to consume the bucket token, the action is performed on the fork-join common pool, by default.
- Parameters:
bucket
- rate limit bucketexecutor
- scheduled executor service for async callsaction
- runnable that requires a bucket point- Returns:
- a future to track completion progress
-