Interface LimitAlgorithm
- All Known Subinterfaces:
Limit
- All Known Implementing Classes:
AimdLimit
,FixedLimit
There are two options how to use a limit - by handling a token provided by tryAcquire()
,
or by invoking a callable or runnable through one of the invoke methods (such as invoke(Runnable)
.
The invoke methods are backed by the same tryAcquire()
methods, so behavior is consistent.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
When a token is retrieved fromtryAcquire()
, one of its methods must be called when the task is over, to release the token back to the pool (such as a permit returned to aSemaphore
). -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Invoke a runnable within the limits of this limiter.default <T> T
Invoke a callable within the limits of this limiter.default Optional
<LimitAlgorithm.Token> Try to acquire a token, waiting for available permits for the configured amount of time, if queuing is enabled.tryAcquire
(boolean wait) Try to acquire a token, waiting for available permits for the configured amount of time, ifwait
is enabled, returning immediately otherwise.
-
Method Details
-
invoke
Invoke a callable within the limits of this limiter.Limit
implementors note: Make sure to catchIgnoreTaskException
from the callable, and call itsIgnoreTaskException.handle()
to either return the provided result, or throw the exception after ignoring the timing for future decisions.- Type Parameters:
T
- the callable return type- Parameters:
callable
- callable to execute within the limit- Returns:
- result of the callable
- Throws:
LimitException
- in case the limiter did not have an available permitException
- in case the task failed with an exception
-
invoke
Invoke a runnable within the limits of this limiter.Limit
implementors note: Make sure to catchIgnoreTaskException
from the runnable, and call itsIgnoreTaskException.handle()
to either return the provided result, or throw the exception after ignoring the timing for future decisions.- Parameters:
runnable
- runnable to execute within the limit- Throws:
LimitException
- in case the limiter did not have an available permitException
- in case the task failed with an exception
-
tryAcquire
Try to acquire a token, waiting for available permits for the configured amount of time, if queuing is enabled.If acquired, the caller must call one of the
LimitAlgorithm.Token
operations to release the token. If the response is empty, the limit does not have an available token.- Returns:
- acquired token, or empty if there is no available token
-
tryAcquire
Try to acquire a token, waiting for available permits for the configured amount of time, ifwait
is enabled, returning immediately otherwise.If acquired, the caller must call one of the
LimitAlgorithm.Token
operations to release the token. If the response is empty, the limit does not have an available token.- Parameters:
wait
- whether to wait in the queue (if one is configured/available in the limit), or to return immediately- Returns:
- acquired token, or empty if there is no available token
-