Class BucketUtils

java.lang.Object
com.github.twitch4j.common.util.BucketUtils

public class BucketUtils extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static @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
    simple(long capacity, Duration greedyRefillPeriod)
     
    static io.github.bucket4j.Bandwidth
    simple(long capacity, Duration greedyRefillPeriod, String id)
    Creates a bandwidth with the specified capacity and refill rate.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BucketUtils

      public BucketUtils()
  • Method Details

    • simple

      public static io.github.bucket4j.Bandwidth simple(long capacity, Duration greedyRefillPeriod)
      Parameters:
      capacity - the bandwidth capacity
      greedyRefillPeriod - 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 capacity
      greedyRefillPeriod - the amount of time for the bucket bandwidth to be completely refilled
      id - 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 bucket
      executor - scheduled executor service for async calls
      call - 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 bucket
      executor - scheduled executor service for async calls
      action - runnable that requires a bucket point
      Returns:
      a future to track completion progress