Class BucketUtils

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

public class BucketUtils extends Object
  • Constructor Details

    • BucketUtils

      public BucketUtils()
  • Method Details

    • simple

      public static 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 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 Bucket createBucket(@NotNull @NotNull Bandwidth limit)
      Creates a bucket with the specified bandwidth.
      Parameters:
      limit - the bandwidth
      Returns:
      the bucket
    • createBucket

      @NotNull public static @NotNull Bucket createBucket(@NotNull @NotNull Bandwidth... limits)
      Creates a bucket with the specified bandwidths.
      Parameters:
      limits - the bandwidths
      Returns:
      the bucket
    • createBucket

      @NotNull public static @NotNull Bucket createBucket(@NotNull @NotNull Iterable<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 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

      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