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>configprivate RateLimiterStatsstats
-
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 voidacquirePermits(int permits)Attempts to acquire the requestedpermitsto perform executions against the rate limiter, waiting until they are available or the thread is interrupted.RateLimiterConfig<R>getConfig()Returns theRateLimiterConfigthat the RateLimiter was built with.java.time.DurationreservePermits(int permits)Reserves thepermitsto perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits.(package private) longreservePermits(int permits, java.time.Duration maxWaitTime)PolicyExecutor<R>toExecutor(int policyIndex)Returns aPolicyExecutorcapable of handling an execution for the Policy.booleantryAcquirePermits(int permits)Tries to acquire the requestedpermitsto perform executions against the rate limiter, returning immediately without waiting.booleantryAcquirePermits(int permits, java.time.Duration maxWaitTime)Tries to acquire the requestedpermitsto perform executions against the rate limiter, waiting up to themaxWaitTimeuntil they are available.java.time.DurationtryReservePermits(int permits, java.time.Duration maxWaitTime)Tries to reserve thepermitsto 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:RateLimiterReturns theRateLimiterConfigthat the RateLimiter was built with.
-
acquirePermits
public void acquirePermits(int permits) throws java.lang.InterruptedExceptionDescription copied from interface:RateLimiterAttempts to acquire the requestedpermitsto perform executions against the rate limiter, waiting until they are available or the thread is interrupted.- Specified by:
acquirePermitsin 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:RateLimiterReserves thepermitsto perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits. Returns0if the permits are immediately available and no waiting is needed.- Specified by:
reservePermitsin interfaceRateLimiter<R>- See Also:
RateLimiter.acquirePermits(int),RateLimiter.tryAcquirePermits(int)
-
tryAcquirePermits
public boolean tryAcquirePermits(int permits)
Description copied from interface:RateLimiterTries to acquire the requestedpermitsto perform executions against the rate limiter, returning immediately without waiting.- Specified by:
tryAcquirePermitsin interfaceRateLimiter<R>- Returns:
- whether the requested
permitsare successfully acquired or not - See Also:
RateLimiter.acquirePermits(int)
-
tryAcquirePermits
public boolean tryAcquirePermits(int permits, java.time.Duration maxWaitTime) throws java.lang.InterruptedExceptionDescription copied from interface:RateLimiterTries to acquire the requestedpermitsto perform executions against the rate limiter, waiting up to themaxWaitTimeuntil they are available.- Specified by:
tryAcquirePermitsin interfaceRateLimiter<R>- Returns:
- whether the requested
permitsare 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:RateLimiterTries to reserve thepermitsto 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
0if the permits were successfully reserved and no waiting is needed. - Returns
-1 nanosecondsif the permits were not reserved because the wait time would be greater than themaxWaitTime.
- Specified by:
tryReservePermitsin interfaceRateLimiter<R>- See Also:
RateLimiter.acquirePermit(Duration),RateLimiter.tryAcquirePermit(Duration)
-
toExecutor
public PolicyExecutor<R> toExecutor(int policyIndex)
Description copied from interface:PolicyReturns aPolicyExecutorcapable of handling an execution for the Policy.- Specified by:
toExecutorin interfacePolicy<R>
-
reservePermits
long reservePermits(int permits, java.time.Duration maxWaitTime)
-
-