public interface RateLimiter
A RateLimiter distributes permits at a configurable rate. getPermission(java.time.Duration) blocks if necessary
until a permit is available, and then takes it. Once acquired, permits need not be released.
| Modifier and Type | Interface and Description |
|---|---|
static interface |
RateLimiter.EventPublisher
An EventPublisher which can be used to register event consumers.
|
static interface |
RateLimiter.Metrics |
| Modifier and Type | Method and Description |
|---|---|
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,
java.util.concurrent.Callable<T> callable) |
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 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,
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,
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,
java.util.function.Consumer<T> consumer)
Creates a consumer 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 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,
java.util.function.Supplier<T> supplier)
Creates a supplier which is restricted by a RateLimiter.
|
default <T> T |
executeCallable(java.util.concurrent.Callable<T> callable)
Decorates and executes the decorated Callable.
|
default void |
executeRunnable(java.lang.Runnable runnable)
Decorates and executes the decorated Runnable.
|
default <T> T |
executeSupplier(java.util.function.Supplier<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
|
boolean |
getPermission(java.time.Duration timeoutDuration)
Acquires a permission from this rate limiter, blocking until one is
available.
|
RateLimiterConfig |
getRateLimiterConfig()
Get the RateLimiterConfig of 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,
java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier)
Creates a RateLimiter with a custom RateLimiterConfig configuration.
|
static RateLimiter |
ofDefaults(java.lang.String name)
Creates a RateLimiter with a default RateLimiterConfig configuration.
|
long |
reservePermission(java.time.Duration timeoutDuration)
Reserves a permission 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 RateLimiter of(java.lang.String name, RateLimiterConfig rateLimiterConfig)
name - the name of the RateLimiterrateLimiterConfig - a custom RateLimiter configurationRateLimiterstatic RateLimiter of(java.lang.String name, java.util.function.Supplier<RateLimiterConfig> rateLimiterConfigSupplier)
name - the name of the RateLimiterrateLimiterConfigSupplier - a supplier of a custom RateLimiterConfig configurationRateLimiterstatic RateLimiter ofDefaults(java.lang.String name)
name - the name of the RateLimiterRateLimiterstatic <T> java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> decorateCompletionStage(RateLimiter rateLimiter, java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
T - the type of the returned CompletionStage's resultrateLimiter - the rateLimitersupplier - the original supplierstatic <T> io.vavr.CheckedFunction0<T> decorateCheckedSupplier(RateLimiter rateLimiter, io.vavr.CheckedFunction0<T> supplier)
T - the type of results supplied supplierrateLimiter - the RateLimitersupplier - the original supplierstatic io.vavr.CheckedRunnable decorateCheckedRunnable(RateLimiter rateLimiter, io.vavr.CheckedRunnable runnable)
rateLimiter - the RateLimiterrunnable - the original runnablestatic <T,R> io.vavr.CheckedFunction1<T,R> decorateCheckedFunction(RateLimiter rateLimiter, io.vavr.CheckedFunction1<T,R> function)
T - the type of function argumentR - the type of function resultsrateLimiter - the RateLimiterfunction - the original functionstatic <T> java.util.function.Supplier<T> decorateSupplier(RateLimiter rateLimiter, java.util.function.Supplier<T> supplier)
T - the type of results supplied supplierrateLimiter - the RateLimitersupplier - the original supplierstatic <T> java.util.concurrent.Callable<T> decorateCallable(RateLimiter rateLimiter, java.util.concurrent.Callable<T> callable)
static <T> java.util.function.Consumer<T> decorateConsumer(RateLimiter rateLimiter, java.util.function.Consumer<T> consumer)
T - the type of the input to the consumerrateLimiter - the RateLimiterconsumer - the original consumerstatic java.lang.Runnable decorateRunnable(RateLimiter rateLimiter, java.lang.Runnable runnable)
rateLimiter - the RateLimiterrunnable - the original runnablestatic <T,R> java.util.function.Function<T,R> decorateFunction(RateLimiter rateLimiter, java.util.function.Function<T,R> function)
T - the type of the input to the functionR - the type of the result of the functionrateLimiter - the RateLimiterfunction - the original functionstatic void waitForPermission(RateLimiter rateLimiter) throws java.lang.IllegalStateException, RequestNotPermitted
rateLimiter - the RateLimiter to get permission fromRequestNotPermitted - if waiting time elapsed before a permit was acquired.java.lang.IllegalStateException - if thread was interrupted during permission waitvoid changeTimeoutDuration(java.time.Duration timeoutDuration)
timeoutDuration - new timeout durationvoid changeLimitForPeriod(int limitForPeriod)
limitForPeriod - new permissions limitboolean getPermission(java.time.Duration timeoutDuration)
If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.
timeoutDuration - the maximum time to waittrue if a permit was acquired and false
if waiting timeoutDuration elapsed before a permit was acquiredlong reservePermission(java.time.Duration timeoutDuration)
timeoutDuration is less then time to wait for permission.timeoutDuration - the maximum time you want to wait.long amount of nanoseconds you should wait for reserved permission. if negative, it means you failed to reserve.java.lang.String getName()
RateLimiterConfig getRateLimiterConfig()
RateLimiter.Metrics getMetrics()
RateLimiter.EventPublisher getEventPublisher()
default <T> T executeSupplier(java.util.function.Supplier<T> supplier)
T - the type of results supplied by this suppliersupplier - the original Supplierdefault <T> T executeCallable(java.util.concurrent.Callable<T> callable)
throws java.lang.Exception
T - the result type of callablecallable - the original Callablejava.lang.Exception - if unable to compute a resultdefault void executeRunnable(java.lang.Runnable runnable)
runnable - the original Runnable