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 TypeMethodDescriptionvoid
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.Returns theRateLimiterConfig
that the RateLimiter was built with.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, Duration maxWaitTime) 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, Duration maxWaitTime) Tries to acquire the requestedpermits
to perform executions against the rate limiter, waiting up to themaxWaitTime
until they are available.tryReservePermits
(int permits, 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 Details
-
config
-
stats
-
-
Constructor Details
-
RateLimiterImpl
-
RateLimiterImpl
RateLimiterImpl(RateLimiterConfig<R> config, RateLimiterStats.Stopwatch stopwatch)
-
-
Method Details
-
getConfig
Description copied from interface:RateLimiter
Returns theRateLimiterConfig
that the RateLimiter was built with. -
acquirePermits
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:
InterruptedException
- if the current thread is interrupted while waiting to acquire thepermits
- See Also:
-
reservePermits
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:
-
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:
-
tryAcquirePermits
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:
InterruptedException
- if the current thread is interrupted while waiting to acquire thepermits
- See Also:
-
tryReservePermits
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:
-
toExecutor
Description copied from interface:Policy
Returns aPolicyExecutor
capable of handling an execution for the Policy.- Specified by:
toExecutor
in interfacePolicy<R>
-
reservePermits
-