Interface RateLimiter


  • public interface RateLimiter
    Used for scheduling tasks according to rate limits.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  RateLimiter.BucketBuilder
      Used for building instances of RateLimiter that respect a bucket system.
      static class  RateLimiter.SimpleBuilder
      Used for building instances of RateLimiter that respect a minimal spacing between tasks.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      static RateLimiter.BucketBuilder bucketBuilder()
      For creating instances of RateLimiter that will respect a bucket system.
      void enqueue​(java.util.function.IntFunction<java.util.concurrent.CompletableFuture<java.lang.Boolean>> task)
      Enqueues an asynchronous task to be executed according to the rate limit this instance represents.
      boolean isShutDown()
      Whether the instance is already shut down
      java.util.concurrent.CompletableFuture<java.lang.Void> shutdown()
      Shuts down the instance gracefully.
      static RateLimiter.SimpleBuilder simpleBuilder()
      For creating instances of RateLimiter that will respect a simple system where tasks are spaced by a certain limit.
    • Method Detail

      • enqueue

        void enqueue​(@Nonnull
                     java.util.function.IntFunction<java.util.concurrent.CompletableFuture<java.lang.Boolean>> task)
        Enqueues an asynchronous task to be executed according to the rate limit this instance represents. The int applied to the function is the number of retries this task is on in this iteration. If this is the first time this task is executed it will be 0, it will be 1 on the first retry and so on. Complete the CompletableFuture you return with true in the function if you want to retry this task.
        Parameters:
        task - the task to enqueue
        Throws:
        java.lang.IllegalArgumentException - task is null
        java.lang.IllegalStateException - if the instance is already shut down
      • shutdown

        @Nonnull
        java.util.concurrent.CompletableFuture<java.lang.Void> shutdown()
        Shuts down the instance gracefully. Already enqueued tasks will still be executed.
        Returns:
        a CompletableFuture that will be completed once the shutdown is completed
      • isShutDown

        boolean isShutDown()
        Whether the instance is already shut down
        Returns:
        true if the instance is shut down
      • simpleBuilder

        @Nonnull
        static RateLimiter.SimpleBuilder simpleBuilder()
        For creating instances of RateLimiter that will respect a simple system where tasks are spaced by a certain limit.
        Returns:
        a new SimpleBuilder for building a RateLimiter
      • bucketBuilder

        @Nonnull
        static RateLimiter.BucketBuilder bucketBuilder()
        For creating instances of RateLimiter that will respect a bucket system.
        Returns:
        a new BucketBuilder for building a RateLimter