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>
A RateLimiter implementation that supports smooth and bursty rate limiting.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final RateLimiterConfig<R> private final RateLimiterStats -
Constructor Summary
ConstructorsConstructorDescriptionRateLimiterImpl(RateLimiterConfig<R> config) RateLimiterImpl(RateLimiterConfig<R> config, RateLimiterStats.Stopwatch stopwatch) -
Method Summary
Modifier and TypeMethodDescriptionvoidacquirePermits(int permits) Attempts to acquire the requestedpermitsto perform executions against the rate limiter, waiting until they are available or the thread is interrupted.Returns theRateLimiterConfigthat the RateLimiter was built with.reservePermits(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, Duration maxWaitTime) 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, Duration maxWaitTime) Tries to acquire the requestedpermitsto perform executions against the rate limiter, waiting up to themaxWaitTimeuntil they are available.tryReservePermits(int permits, 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, waitMethods inherited from interface dev.failsafe.RateLimiter
acquirePermit, acquirePermit, acquirePermits, isBursty, isSmooth, reservePermit, tryAcquirePermit, tryAcquirePermit, tryReservePermit
-
Field Details
-
config
-
stats
-
-
Constructor Details
-
RateLimiterImpl
-
RateLimiterImpl
RateLimiterImpl(RateLimiterConfig<R> config, RateLimiterStats.Stopwatch stopwatch)
-
-
Method Details
-
getConfig
Description copied from interface:RateLimiterReturns theRateLimiterConfigthat the RateLimiter was built with. -
acquirePermits
Description 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:
InterruptedException- if the current thread is interrupted while waiting to acquire thepermits- See Also:
-
reservePermits
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:
-
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:
-
tryAcquirePermits
Description 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:
InterruptedException- if the current thread is interrupted while waiting to acquire thepermits- See Also:
-
tryReservePermits
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:
-
toExecutor
Description copied from interface:PolicyReturns aPolicyExecutorcapable of handling an execution for the Policy.- Specified by:
toExecutorin interfacePolicy<R>
-
reservePermits
-