Class BucketGlobalRateLimiter

java.lang.Object
discord4j.rest.request.BucketGlobalRateLimiter
All Implemented Interfaces:
GlobalRateLimiter

public class BucketGlobalRateLimiter extends Object implements GlobalRateLimiter
An implementation of GlobalRateLimiter that uses a RateLimitOperator coordinate requests, injecting an additional delay if a source trips the limiter early through rateLimitFor(Duration).
  • Method Details

    • create

      public static BucketGlobalRateLimiter create()
      Creates a new global rate limiter of 50 requests per second using Schedulers.parallel() to inject delays.
      Returns:
      a BucketGlobalRateLimiter with default parameters
    • create

      public static BucketGlobalRateLimiter create(int capacity, Duration refillPeriod, Scheduler delayScheduler)
      Creates a new global rate limiter with the given parameters. Be aware that modifying these parameters can lead your bot hitting 429 TOO MANY REQUESTS errors.
      Parameters:
      capacity - the number of requests that can be performed in the given refillPeriod
      refillPeriod - the Duration before refilling request permits
      delayScheduler - the Scheduler used to inject delays
      Returns:
      a BucketGlobalRateLimiter with the given parameters.
    • rateLimitFor

      public Mono<Void> rateLimitFor(Duration duration)
      Description copied from interface: GlobalRateLimiter
      Sets a new rate limit that will be applied to every operation performed using GlobalRateLimiter.withLimiter(Publisher).
      Specified by:
      rateLimitFor in interface GlobalRateLimiter
      Parameters:
      duration - the Duration every new operation should wait before being used
    • getRemaining

      public Mono<Duration> getRemaining()
      Description copied from interface: GlobalRateLimiter
      Returns the Duration remaining until the current global rate limit is completed. Can be negative or zero if there is no currently active global rate limit.
      Specified by:
      getRemaining in interface GlobalRateLimiter
      Returns:
      a positive Duration indicating the remaining time a global rate limit is being applied. Zero or negative if no global rate limit is currently active.
    • withLimiter

      public <T> Flux<T> withLimiter(Publisher<T> stage)
      Description copied from interface: GlobalRateLimiter
      Provides a scope to perform reactive operations under this global rate limiter. Limiter resources are acquired on subscription and released when the given stage is cancelled, has completed or has been terminated with an error.
      Specified by:
      withLimiter in interface GlobalRateLimiter
      Type Parameters:
      T - the type of the stage supplier
      Parameters:
      stage - a Publisher that will manage this global rate limiter resources
      Returns:
      a Flux where each subscription represents acquiring a rate limiter resource