Interface RateLimiter

  • All Known Implementing Classes:
    AtomicRateLimiter, SemaphoreBasedRateLimiter

    public interface RateLimiter
    A RateLimiter instance is thread-safe can be used to decorate multiple requests.

    A RateLimiter distributes permits at a configurable rate. acquirePermission() blocks if necessary until a permit is available, and then takes it. Once acquired, permits need not be released.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  RateLimiter.EventPublisher
      An EventPublisher which can be used to register event consumers.
      static interface  RateLimiter.Metrics  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default boolean acquirePermission()
      Acquires a permission from this rate limiter, blocking until one is available, or the thread is interrupted.
      boolean acquirePermission​(int permits)
      Acquires the given number of permits from this rate limiter, blocking until one is available, or the thread is interrupted.
      void changeLimitForPeriod​(int limitForPeriod)
      Dynamic rate limiter configuration change.
      void changeTimeoutDuration​(java.time.Duration timeoutDuration)
      Dynamic rate limiter configuration change.
      static <T> java.util.concurrent.Callable<T> decorateCallable​(RateLimiter rateLimiter, int permits, java.util.concurrent.Callable<T> callable)
      Creates a callable which is restricted by a RateLimiter.
      static <T> java.util.concurrent.Callable<T> decorateCallable​(RateLimiter rateLimiter, java.util.concurrent.Callable<T> callable)
      Creates a callable which is restricted by a RateLimiter.
      static <T,​R>
      io.vavr.CheckedFunction1<T,​R>
      decorateCheckedFunction​(RateLimiter rateLimiter, int permits, io.vavr.CheckedFunction1<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static <T,​R>
      io.vavr.CheckedFunction1<T,​R>
      decorateCheckedFunction​(RateLimiter rateLimiter, io.vavr.CheckedFunction1<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static <T,​R>
      io.vavr.CheckedFunction1<T,​R>
      decorateCheckedFunction​(RateLimiter rateLimiter, java.util.function.Function<T,​java.lang.Integer> permitsCalculator, io.vavr.CheckedFunction1<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static io.vavr.CheckedRunnable decorateCheckedRunnable​(RateLimiter rateLimiter, int permits, io.vavr.CheckedRunnable runnable)
      Creates a runnable which is restricted by a RateLimiter.
      static io.vavr.CheckedRunnable decorateCheckedRunnable​(RateLimiter rateLimiter, io.vavr.CheckedRunnable runnable)
      Creates a runnable which is restricted by a RateLimiter.
      static <T> io.vavr.CheckedFunction0<T> decorateCheckedSupplier​(RateLimiter rateLimiter, int permits, io.vavr.CheckedFunction0<T> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> io.vavr.CheckedFunction0<T> decorateCheckedSupplier​(RateLimiter rateLimiter, io.vavr.CheckedFunction0<T> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> decorateCompletionStage​(RateLimiter rateLimiter, int permits, java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
      Returns a supplier which is decorated by a rateLimiter.
      static <T> java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> decorateCompletionStage​(RateLimiter rateLimiter, java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
      Returns a supplier which is decorated by a rateLimiter.
      static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter, int permits, java.util.function.Consumer<T> consumer)
      Creates a consumer which is restricted by a RateLimiter.
      static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter, java.util.function.Consumer<T> consumer)
      Creates a consumer which is restricted by a RateLimiter.
      static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter, java.util.function.Function<T,​java.lang.Integer> permitsCalculator, java.util.function.Consumer<T> consumer)
      Creates a consumer which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<io.vavr.control.Either<java.lang.Exception,​T>> decorateEitherSupplier​(RateLimiter rateLimiter, int permits, java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<io.vavr.control.Either<java.lang.Exception,​T>> decorateEitherSupplier​(RateLimiter rateLimiter, java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T,​R>
      java.util.function.Function<T,​R>
      decorateFunction​(RateLimiter rateLimiter, int permits, java.util.function.Function<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static <T,​R>
      java.util.function.Function<T,​R>
      decorateFunction​(RateLimiter rateLimiter, java.util.function.Function<T,​java.lang.Integer> permitsCalculator, java.util.function.Function<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static <T,​R>
      java.util.function.Function<T,​R>
      decorateFunction​(RateLimiter rateLimiter, java.util.function.Function<T,​R> function)
      Creates a function which is restricted by a RateLimiter.
      static <T,​F extends java.util.concurrent.Future<T>>
      java.util.function.Supplier<F>
      decorateFuture​(RateLimiter rateLimiter, int permits, java.util.function.Supplier<? extends F> supplier)
      Returns a Supplier which is decorated by a RateLimiter.
      static <T,​F extends java.util.concurrent.Future<T>>
      java.util.function.Supplier<F>
      decorateFuture​(RateLimiter rateLimiter, java.util.function.Supplier<? extends F> supplier)
      Returns a Supplier which is decorated by a RateLimiter.
      static java.lang.Runnable decorateRunnable​(RateLimiter rateLimiter, int permits, java.lang.Runnable runnable)
      Creates a runnable which is restricted by a RateLimiter.
      static java.lang.Runnable decorateRunnable​(RateLimiter rateLimiter, java.lang.Runnable runnable)
      Creates a runnable which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<T> decorateSupplier​(RateLimiter rateLimiter, int permits, java.util.function.Supplier<T> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<T> decorateSupplier​(RateLimiter rateLimiter, java.util.function.Supplier<T> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<io.vavr.control.Try<T>> decorateTrySupplier​(RateLimiter rateLimiter, int permits, java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      static <T> java.util.function.Supplier<io.vavr.control.Try<T>> decorateTrySupplier​(RateLimiter rateLimiter, java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
      Creates a supplier which is restricted by a RateLimiter.
      default <T> T executeCallable​(int permits, java.util.concurrent.Callable<T> callable)
      Decorates and executes the decorated Callable.
      default <T> T executeCallable​(java.util.concurrent.Callable<T> callable)
      Decorates and executes the decorated Callable.
      default <T> T executeCheckedSupplier​(int permits, io.vavr.CheckedFunction0<T> checkedSupplier)
      Decorates and executes the decorated Supplier.
      default <T> T executeCheckedSupplier​(io.vavr.CheckedFunction0<T> checkedSupplier)
      Decorates and executes the decorated Supplier.
      default <T> java.util.concurrent.CompletionStage<T> executeCompletionStage​(java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
      Decorates and executes the decorated CompletionStage.
      default <T> io.vavr.control.Either<java.lang.Exception,​T> executeEitherSupplier​(int permits, java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
      Decorates and executes the decorated Supplier.
      default <T> io.vavr.control.Either<java.lang.Exception,​T> executeEitherSupplier​(java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
      Decorates and executes the decorated Supplier.
      default void executeRunnable​(int permits, java.lang.Runnable runnable)
      Decorates and executes the decorated Runnable.
      default void executeRunnable​(java.lang.Runnable runnable)
      Decorates and executes the decorated Runnable.
      default <T> T executeSupplier​(int permits, java.util.function.Supplier<T> supplier)
      Decorates and executes the decorated Supplier.
      default <T> T executeSupplier​(java.util.function.Supplier<T> supplier)
      Decorates and executes the decorated Supplier.
      default <T> io.vavr.control.Try<T> executeTrySupplier​(int permits, java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
      Decorates and executes the decorated Supplier.
      default <T> io.vavr.control.Try<T> executeTrySupplier​(java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
      Decorates and executes the decorated Supplier.
      RateLimiter.EventPublisher getEventPublisher()
      Returns an EventPublisher which can be used to register event consumers.
      RateLimiter.Metrics getMetrics()
      Get the Metrics of this RateLimiter.
      java.lang.String getName()
      Get the name of this RateLimiter
      RateLimiterConfig getRateLimiterConfig()
      Get the RateLimiterConfig of this RateLimiter.
      io.vavr.collection.Map<java.lang.String,​java.lang.String> getTags()
      Returns an unmodifiable map with tags assigned to this RateLimiter.
      static RateLimiter of​(java.lang.String name, RateLimiterConfig rateLimiterConfig)
      Creates a RateLimiter with a custom RateLimiter configuration.
      static RateLimiter of​(java.lang.String name, RateLimiterConfig rateLimiterConfig, io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
      Creates a RateLimiter with a custom RateLimiter configuration.
      static RateLimiter of​(java.lang.String name, java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier)
      Creates a RateLimiter with a custom RateLimiterConfig configuration.
      static RateLimiter of​(java.lang.String name, java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier, io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
      Creates a RateLimiter with a custom RateLimiterConfig configuration.
      static RateLimiter ofDefaults​(java.lang.String name)
      Creates a RateLimiter with a default RateLimiterConfig configuration.
      default long reservePermission()
      Reserves a permission from this rate limiter and returns nanoseconds you should wait for it.
      long reservePermission​(int permits)
      Reserves the given number permits from this rate limiter and returns nanoseconds you should wait for it.
      static void waitForPermission​(RateLimiter rateLimiter)
      Will wait for permission within default timeout duration.
      static void waitForPermission​(RateLimiter rateLimiter, int permits)
      Will wait for required number of permits within default timeout duration.
    • Method Detail

      • of

        static RateLimiter of​(java.lang.String name,
                              RateLimiterConfig rateLimiterConfig)
        Creates a RateLimiter with a custom RateLimiter configuration.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - a custom RateLimiter configuration
        Returns:
        The RateLimiter
      • of

        static RateLimiter of​(java.lang.String name,
                              RateLimiterConfig rateLimiterConfig,
                              io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
        Creates a RateLimiter with a custom RateLimiter configuration.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - a custom RateLimiter configuration
        tags - tags to assign to the RateLimiter
        Returns:
        The RateLimiter
      • of

        static RateLimiter of​(java.lang.String name,
                              java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier)
        Creates a RateLimiter with a custom RateLimiterConfig configuration.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfigSupplier - a supplier of a custom RateLimiterConfig configuration
        Returns:
        The RateLimiter
      • of

        static RateLimiter of​(java.lang.String name,
                              java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier,
                              io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
        Creates a RateLimiter with a custom RateLimiterConfig configuration.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfigSupplier - a supplier of a custom RateLimiterConfig configuration
        tags - tags to assign to the RateLimiter
        Returns:
        The RateLimiter
      • ofDefaults

        static RateLimiter ofDefaults​(java.lang.String name)
        Creates a RateLimiter with a default RateLimiterConfig configuration.
        Parameters:
        name - the name of the RateLimiter
        Returns:
        The RateLimiter
      • decorateCompletionStage

        static <T> java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> decorateCompletionStage​(RateLimiter rateLimiter,
                                                                                                                java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
        Returns a supplier which is decorated by a rateLimiter.
        Type Parameters:
        T - the type of the returned CompletionStage's result
        Parameters:
        rateLimiter - the rateLimiter
        supplier - the original supplier
        Returns:
        a supplier which is decorated by a RateLimiter.
      • decorateCompletionStage

        static <T> java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> decorateCompletionStage​(RateLimiter rateLimiter,
                                                                                                                int permits,
                                                                                                                java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
        Returns a supplier which is decorated by a rateLimiter.
        Type Parameters:
        T - the type of the returned CompletionStage's result
        Parameters:
        rateLimiter - the rateLimiter
        permits - number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is decorated by a RateLimiter.
      • decorateFuture

        static <T,​F extends java.util.concurrent.Future<T>> java.util.function.Supplier<F> decorateFuture​(RateLimiter rateLimiter,
                                                                                                                java.util.function.Supplier<? extends F> supplier)
        Returns a Supplier which is decorated by a RateLimiter.
        Type Parameters:
        T - the type of the returned Future's result
        F - the return type of the original Supplier (extends Future<T>)
        Parameters:
        rateLimiter - the rate limiter
        supplier - the original supplier
        Returns:
        a supplier which is decorated by a rate limiter.
      • decorateFuture

        static <T,​F extends java.util.concurrent.Future<T>> java.util.function.Supplier<F> decorateFuture​(RateLimiter rateLimiter,
                                                                                                                int permits,
                                                                                                                java.util.function.Supplier<? extends F> supplier)
        Returns a Supplier which is decorated by a RateLimiter.
        Type Parameters:
        T - the type of the returned Future's result
        F - the return type of the original Supplier (extends Future<T>)
        Parameters:
        rateLimiter - the rate limiter
        permits - the number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is decorated by a rate limiter.
      • decorateCheckedSupplier

        static <T> io.vavr.CheckedFunction0<T> decorateCheckedSupplier​(RateLimiter rateLimiter,
                                                                       io.vavr.CheckedFunction0<T> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateCheckedSupplier

        static <T> io.vavr.CheckedFunction0<T> decorateCheckedSupplier​(RateLimiter rateLimiter,
                                                                       int permits,
                                                                       io.vavr.CheckedFunction0<T> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateCheckedRunnable

        static io.vavr.CheckedRunnable decorateCheckedRunnable​(RateLimiter rateLimiter,
                                                               io.vavr.CheckedRunnable runnable)
        Creates a runnable which is restricted by a RateLimiter.
        Parameters:
        rateLimiter - the RateLimiter
        runnable - the original runnable
        Returns:
        a runnable which is restricted by a RateLimiter.
      • decorateCheckedRunnable

        static io.vavr.CheckedRunnable decorateCheckedRunnable​(RateLimiter rateLimiter,
                                                               int permits,
                                                               io.vavr.CheckedRunnable runnable)
        Creates a runnable which is restricted by a RateLimiter.
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        runnable - the original runnable
        Returns:
        a runnable which is restricted by a RateLimiter.
      • decorateCheckedFunction

        static <T,​R> io.vavr.CheckedFunction1<T,​R> decorateCheckedFunction​(RateLimiter rateLimiter,
                                                                                       io.vavr.CheckedFunction1<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of function argument
        R - the type of function results
        Parameters:
        rateLimiter - the RateLimiter
        function - the original function
        Returns:
        a function which is restricted by a RateLimiter.
      • decorateCheckedFunction

        static <T,​R> io.vavr.CheckedFunction1<T,​R> decorateCheckedFunction​(RateLimiter rateLimiter,
                                                                                       int permits,
                                                                                       io.vavr.CheckedFunction1<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of function argument
        R - the type of function results
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        function - the original function
        Returns:
        a function which is restricted by a RateLimiter.
      • decorateCheckedFunction

        static <T,​R> io.vavr.CheckedFunction1<T,​R> decorateCheckedFunction​(RateLimiter rateLimiter,
                                                                                       java.util.function.Function<T,​java.lang.Integer> permitsCalculator,
                                                                                       io.vavr.CheckedFunction1<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of function argument
        R - the type of function results
        Parameters:
        rateLimiter - the RateLimiter
        permitsCalculator - calculates the number of permits required by this call based on the functions argument
        function - the original function
        Returns:
        a function which is restricted by a RateLimiter.
      • decorateSupplier

        static <T> java.util.function.Supplier<T> decorateSupplier​(RateLimiter rateLimiter,
                                                                   java.util.function.Supplier<T> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateSupplier

        static <T> java.util.function.Supplier<T> decorateSupplier​(RateLimiter rateLimiter,
                                                                   int permits,
                                                                   java.util.function.Supplier<T> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateTrySupplier

        static <T> java.util.function.Supplier<io.vavr.control.Try<T>> decorateTrySupplier​(RateLimiter rateLimiter,
                                                                                           java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateTrySupplier

        static <T> java.util.function.Supplier<io.vavr.control.Try<T>> decorateTrySupplier​(RateLimiter rateLimiter,
                                                                                           int permits,
                                                                                           java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateEitherSupplier

        static <T> java.util.function.Supplier<io.vavr.control.Either<java.lang.Exception,​T>> decorateEitherSupplier​(RateLimiter rateLimiter,
                                                                                                                           java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateEitherSupplier

        static <T> java.util.function.Supplier<io.vavr.control.Either<java.lang.Exception,​T>> decorateEitherSupplier​(RateLimiter rateLimiter,
                                                                                                                           int permits,
                                                                                                                           java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
        Creates a supplier which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied supplier
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        supplier - the original supplier
        Returns:
        a supplier which is restricted by a RateLimiter.
      • decorateCallable

        static <T> java.util.concurrent.Callable<T> decorateCallable​(RateLimiter rateLimiter,
                                                                     java.util.concurrent.Callable<T> callable)
        Creates a callable which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied by callable
        Parameters:
        rateLimiter - the RateLimiter
        callable - the original callable
        Returns:
        a callable which is restricted by a RateLimiter.
      • decorateCallable

        static <T> java.util.concurrent.Callable<T> decorateCallable​(RateLimiter rateLimiter,
                                                                     int permits,
                                                                     java.util.concurrent.Callable<T> callable)
        Creates a callable which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of results supplied by callable
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        callable - the original callable
        Returns:
        a callable which is restricted by a RateLimiter.
      • decorateConsumer

        static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter,
                                                                   java.util.function.Consumer<T> consumer)
        Creates a consumer which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the consumer
        Parameters:
        rateLimiter - the RateLimiter
        consumer - the original consumer
        Returns:
        a consumer which is restricted by a RateLimiter.
      • decorateConsumer

        static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter,
                                                                   int permits,
                                                                   java.util.function.Consumer<T> consumer)
        Creates a consumer which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the consumer
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        consumer - the original consumer
        Returns:
        a consumer which is restricted by a RateLimiter.
      • decorateConsumer

        static <T> java.util.function.Consumer<T> decorateConsumer​(RateLimiter rateLimiter,
                                                                   java.util.function.Function<T,​java.lang.Integer> permitsCalculator,
                                                                   java.util.function.Consumer<T> consumer)
        Creates a consumer which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the consumer
        Parameters:
        rateLimiter - the RateLimiter
        permitsCalculator - calculates the number of permits required by this call based on the functions argument
        consumer - the original consumer
        Returns:
        a consumer which is restricted by a RateLimiter.
      • decorateRunnable

        static java.lang.Runnable decorateRunnable​(RateLimiter rateLimiter,
                                                   java.lang.Runnable runnable)
        Creates a runnable which is restricted by a RateLimiter.
        Parameters:
        rateLimiter - the RateLimiter
        runnable - the original runnable
        Returns:
        a runnable which is restricted by a RateLimiter.
      • decorateRunnable

        static java.lang.Runnable decorateRunnable​(RateLimiter rateLimiter,
                                                   int permits,
                                                   java.lang.Runnable runnable)
        Creates a runnable which is restricted by a RateLimiter.
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        runnable - the original runnable
        Returns:
        a runnable which is restricted by a RateLimiter.
      • decorateFunction

        static <T,​R> java.util.function.Function<T,​R> decorateFunction​(RateLimiter rateLimiter,
                                                                                   java.util.function.Function<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the function
        R - the type of the result of the function
        Parameters:
        rateLimiter - the RateLimiter
        function - the original function
        Returns:
        a function which is restricted by a RateLimiter.
      • decorateFunction

        static <T,​R> java.util.function.Function<T,​R> decorateFunction​(RateLimiter rateLimiter,
                                                                                   int permits,
                                                                                   java.util.function.Function<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the function
        R - the type of the result of the function
        Parameters:
        rateLimiter - the RateLimiter
        permits - number of permits that this call requires
        function - the original function
        Returns:
        a function which is restricted by a RateLimiter.
      • decorateFunction

        static <T,​R> java.util.function.Function<T,​R> decorateFunction​(RateLimiter rateLimiter,
                                                                                   java.util.function.Function<T,​java.lang.Integer> permitsCalculator,
                                                                                   java.util.function.Function<T,​R> function)
        Creates a function which is restricted by a RateLimiter.
        Type Parameters:
        T - the type of the input to the function
        R - the type of the result of the function
        Parameters:
        rateLimiter - the RateLimiter
        function - the original function
        permitsCalculator - calculates the number of permits required by this call based on the functions argument
        Returns:
        a function which is restricted by a RateLimiter.
      • waitForPermission

        static void waitForPermission​(RateLimiter rateLimiter)
        Will wait for permission within default timeout duration.
        Parameters:
        rateLimiter - the RateLimiter to get permission from
        Throws:
        RequestNotPermitted - if waiting time elapsed before a permit was acquired.
        io.github.resilience4j.core.exception.AcquirePermissionCancelledException - if thread was interrupted during permission wait
      • waitForPermission

        static void waitForPermission​(RateLimiter rateLimiter,
                                      int permits)
        Will wait for required number of permits within default timeout duration.
        Parameters:
        rateLimiter - the RateLimiter to get permission from
        permits - number of permits we have to acquire
        Throws:
        RequestNotPermitted - if waiting time elapsed before a permit was acquired.
        io.github.resilience4j.core.exception.AcquirePermissionCancelledException - if thread was interrupted during permission wait
      • executeCompletionStage

        default <T> java.util.concurrent.CompletionStage<T> executeCompletionStage​(java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
        Decorates and executes the decorated CompletionStage.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        supplier - the original CompletionStage
        Returns:
        the decorated CompletionStage.
      • changeTimeoutDuration

        void changeTimeoutDuration​(java.time.Duration timeoutDuration)
        Dynamic rate limiter configuration change. This method allows to change timeout duration of current limiter. NOTE! New timeout duration won't affect threads that are currently waiting for permission.
        Parameters:
        timeoutDuration - new timeout duration
      • changeLimitForPeriod

        void changeLimitForPeriod​(int limitForPeriod)
        Dynamic rate limiter configuration change. This method allows to change count of permissions available during refresh period. NOTE! New limit won't affect current period permissions and will apply only from next one.
        Parameters:
        limitForPeriod - new permissions limit
      • acquirePermission

        default boolean acquirePermission()
        Acquires a permission from this rate limiter, blocking until one is available, or the thread is interrupted. Maximum wait time is RateLimiterConfig.getTimeoutDuration()

        If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.

        Returns:
        true if a permit was acquired and false if waiting timeoutDuration elapsed before a permit was acquired
      • acquirePermission

        boolean acquirePermission​(int permits)
        Acquires the given number of permits from this rate limiter, blocking until one is available, or the thread is interrupted. Maximum wait time is RateLimiterConfig.getTimeoutDuration()

        If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.

        Parameters:
        permits - number of permits - use for systems where 1 call != 1 permit
        Returns:
        true if a permit was acquired and false if waiting timeoutDuration elapsed before a permit was acquired
      • reservePermission

        default long reservePermission()
        Reserves a permission from this rate limiter and returns nanoseconds you should wait for it. If returned long is negative, it means that you failed to reserve permission, possibly your RateLimiterConfig.getTimeoutDuration() is less then time to wait for permission.
        Returns:
        long amount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.
      • reservePermission

        long reservePermission​(int permits)
        Reserves the given number permits from this rate limiter and returns nanoseconds you should wait for it. If returned long is negative, it means that you failed to reserve permission, possibly your RateLimiterConfig.getTimeoutDuration() is less then time to wait for permission.
        Parameters:
        permits - number of permits - use for systems where 1 call != 1 permit
        Returns:
        long amount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.
      • getName

        java.lang.String getName()
        Get the name of this RateLimiter
        Returns:
        the name of this RateLimiter
      • getRateLimiterConfig

        RateLimiterConfig getRateLimiterConfig()
        Get the RateLimiterConfig of this RateLimiter.
        Returns:
        the RateLimiterConfig of this RateLimiter
      • getTags

        io.vavr.collection.Map<java.lang.String,​java.lang.String> getTags()
        Returns an unmodifiable map with tags assigned to this RateLimiter.
        Returns:
        the tags assigned to this Retry in an unmodifiable map
      • getMetrics

        RateLimiter.Metrics getMetrics()
        Get the Metrics of this RateLimiter.
        Returns:
        the Metrics of this RateLimiter
      • getEventPublisher

        RateLimiter.EventPublisher getEventPublisher()
        Returns an EventPublisher which can be used to register event consumers.
        Returns:
        an EventPublisher
      • executeSupplier

        default <T> T executeSupplier​(java.util.function.Supplier<T> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeSupplier

        default <T> T executeSupplier​(int permits,
                                      java.util.function.Supplier<T> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        permits - number of permits that this call requires
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeTrySupplier

        default <T> io.vavr.control.Try<T> executeTrySupplier​(java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeTrySupplier

        default <T> io.vavr.control.Try<T> executeTrySupplier​(int permits,
                                                              java.util.function.Supplier<io.vavr.control.Try<T>> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        permits - number of permits that this call requires
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeEitherSupplier

        default <T> io.vavr.control.Either<java.lang.Exception,​T> executeEitherSupplier​(java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeEitherSupplier

        default <T> io.vavr.control.Either<java.lang.Exception,​T> executeEitherSupplier​(int permits,
                                                                                              java.util.function.Supplier<io.vavr.control.Either<? extends java.lang.Exception,​T>> supplier)
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        permits - number of permits that this call requires
        supplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
      • executeCallable

        default <T> T executeCallable​(java.util.concurrent.Callable<T> callable)
                               throws java.lang.Exception
        Decorates and executes the decorated Callable.
        Type Parameters:
        T - the result type of callable
        Parameters:
        callable - the original Callable
        Returns:
        the result of the decorated Callable.
        Throws:
        java.lang.Exception - if unable to compute a result
      • executeCallable

        default <T> T executeCallable​(int permits,
                                      java.util.concurrent.Callable<T> callable)
                               throws java.lang.Exception
        Decorates and executes the decorated Callable.
        Type Parameters:
        T - the result type of callable
        Parameters:
        permits - number of permits that this call requires
        callable - the original Callable
        Returns:
        the result of the decorated Callable.
        Throws:
        java.lang.Exception - if unable to compute a result
      • executeRunnable

        default void executeRunnable​(java.lang.Runnable runnable)
        Decorates and executes the decorated Runnable.
        Parameters:
        runnable - the original Runnable
      • executeRunnable

        default void executeRunnable​(int permits,
                                     java.lang.Runnable runnable)
        Decorates and executes the decorated Runnable.
        Parameters:
        permits - number of permits that this call requires
        runnable - the original Runnable
      • executeCheckedSupplier

        default <T> T executeCheckedSupplier​(io.vavr.CheckedFunction0<T> checkedSupplier)
                                      throws java.lang.Throwable
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        checkedSupplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
        Throws:
        java.lang.Throwable - if something goes wrong applying this function to the given arguments
      • executeCheckedSupplier

        default <T> T executeCheckedSupplier​(int permits,
                                             io.vavr.CheckedFunction0<T> checkedSupplier)
                                      throws java.lang.Throwable
        Decorates and executes the decorated Supplier.
        Type Parameters:
        T - the type of results supplied by this supplier
        Parameters:
        permits - number of permits that this call requires
        checkedSupplier - the original Supplier
        Returns:
        the result of the decorated Supplier.
        Throws:
        java.lang.Throwable - if something goes wrong applying this function to the given arguments