Package dev.failsafe.internal
Class RateLimiterImpl<R>
- java.lang.Object
-
- dev.failsafe.internal.RateLimiterImpl<R>
-
- Type Parameters:
R
- result type
- All Implemented Interfaces:
Policy<R>
,RateLimiter<R>
public class RateLimiterImpl<R> extends java.lang.Object implements RateLimiter<R>
A RateLimiter implementation that supports smooth and bursty rate limiting.
-
-
Field Summary
Fields Modifier and Type Field Description private RateLimiterConfig<R>
config
private RateLimiterStats
stats
-
Constructor Summary
Constructors Constructor Description RateLimiterImpl(RateLimiterConfig<R> config)
RateLimiterImpl(RateLimiterConfig<R> config, RateLimiterStats.Stopwatch stopwatch)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acquirePermits(int permits)
Attempts to acquire the requestedpermits
to perform executions against the rate limiter, waiting until they are available or the thread is interrupted.RateLimiterConfig<R>
getConfig()
Returns theRateLimiterConfig
that the RateLimiter was built with.java.time.Duration
reservePermits(int permits)
Reserves thepermits
to perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits.(package private) long
reservePermits(int permits, java.time.Duration maxWaitTime)
PolicyExecutor<R>
toExecutor(int policyIndex)
Returns aPolicyExecutor
capable of handling an execution for the Policy.boolean
tryAcquirePermits(int permits)
Tries to acquire the requestedpermits
to perform executions against the rate limiter, returning immediately without waiting.boolean
tryAcquirePermits(int permits, java.time.Duration maxWaitTime)
Tries to acquire the requestedpermits
to perform executions against the rate limiter, waiting up to themaxWaitTime
until they are available.java.time.Duration
tryReservePermits(int permits, java.time.Duration maxWaitTime)
Tries to reserve thepermits
to perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits, as long as it's less than themaxWaitTime
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface dev.failsafe.RateLimiter
acquirePermit, acquirePermit, acquirePermits, isBursty, isSmooth, reservePermit, tryAcquirePermit, tryAcquirePermit, tryReservePermit
-
-
-
-
Field Detail
-
config
private final RateLimiterConfig<R> config
-
stats
private final RateLimiterStats stats
-
-
Constructor Detail
-
RateLimiterImpl
public RateLimiterImpl(RateLimiterConfig<R> config)
-
RateLimiterImpl
RateLimiterImpl(RateLimiterConfig<R> config, RateLimiterStats.Stopwatch stopwatch)
-
-
Method Detail
-
getConfig
public RateLimiterConfig<R> getConfig()
Description copied from interface:RateLimiter
Returns theRateLimiterConfig
that the RateLimiter was built with.
-
acquirePermits
public void acquirePermits(int permits) throws java.lang.InterruptedException
Description copied from interface:RateLimiter
Attempts to acquire the requestedpermits
to perform executions against the rate limiter, waiting until they are available or the thread is interrupted.- Specified by:
acquirePermits
in interfaceRateLimiter<R>
- Throws:
java.lang.InterruptedException
- if the current thread is interrupted while waiting to acquire thepermits
- See Also:
RateLimiter.tryAcquirePermits(int)
,RateLimiter.reservePermits(int)
-
reservePermits
public java.time.Duration reservePermits(int permits)
Description copied from interface:RateLimiter
Reserves thepermits
to perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits. Returns0
if the permits are immediately available and no waiting is needed.- Specified by:
reservePermits
in interfaceRateLimiter<R>
- See Also:
RateLimiter.acquirePermits(int)
,RateLimiter.tryAcquirePermits(int)
-
tryAcquirePermits
public boolean tryAcquirePermits(int permits)
Description copied from interface:RateLimiter
Tries to acquire the requestedpermits
to perform executions against the rate limiter, returning immediately without waiting.- Specified by:
tryAcquirePermits
in interfaceRateLimiter<R>
- Returns:
- whether the requested
permits
are successfully acquired or not - See Also:
RateLimiter.acquirePermits(int)
-
tryAcquirePermits
public boolean tryAcquirePermits(int permits, java.time.Duration maxWaitTime) throws java.lang.InterruptedException
Description copied from interface:RateLimiter
Tries to acquire the requestedpermits
to perform executions against the rate limiter, waiting up to themaxWaitTime
until they are available.- Specified by:
tryAcquirePermits
in interfaceRateLimiter<R>
- Returns:
- whether the requested
permits
are successfully acquired or not - Throws:
java.lang.InterruptedException
- if the current thread is interrupted while waiting to acquire thepermits
- See Also:
RateLimiter.acquirePermits(int, Duration)
-
tryReservePermits
public java.time.Duration tryReservePermits(int permits, java.time.Duration maxWaitTime)
Description copied from interface:RateLimiter
Tries to reserve thepermits
to perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits, as long as it's less than themaxWaitTime
.- Returns the expected wait time for the permits if they were successfully reserved.
- Returns
0
if the permits were successfully reserved and no waiting is needed. - Returns
-1 nanoseconds
if the permits were not reserved because the wait time would be greater than themaxWaitTime
.
- Specified by:
tryReservePermits
in interfaceRateLimiter<R>
- See Also:
RateLimiter.acquirePermit(Duration)
,RateLimiter.tryAcquirePermit(Duration)
-
toExecutor
public PolicyExecutor<R> toExecutor(int policyIndex)
Description copied from interface:Policy
Returns aPolicyExecutor
capable of handling an execution for the Policy.- Specified by:
toExecutor
in interfacePolicy<R>
-
reservePermits
long reservePermits(int permits, java.time.Duration maxWaitTime)
-
-