Interface LimitAlgorithm
- All Known Subinterfaces:
Limit
- All Known Implementing Classes:
AimdLimit, FixedLimit, ThroughputLimit
There are two ways to use a limit: by handling the outcome returned by tryAcquireOutcome() and the token it
exposes when accepted, or by invoking a callable or runnable through call(Callable) or run(Runnable).
The convenience methods are backed by the same tryAcquireOutcome(boolean) decisions, so behavior is consistent.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents the outcome of a limit algorithm decision.static interfaceCarrier for both the result of aCallablesubjected to concurrency limits and the correspondingLimitAlgorithm.Outcome.static interfaceWhen a token is retrieved fromtryAcquireOutcome(), 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 <T> LimitAlgorithm.Result<T> Invoke a callable within the limits of this limiter.default LimitAlgorithm.OutcomeInvoke a runnable within the limits of this limiter.default LimitAlgorithm.OutcomeTry to acquire a token, waiting for available permits for the configured amount of time, if queuing is enabled.tryAcquireOutcome(boolean wait) Tries to acquire a token, waiting for available permits for the configured amount of time ifwaitis enabled and the implementation supports queueing, returning immediately otherwise.
-
Method Details
-
call
Invoke a callable within the limits of this limiter.Custom implementations that override this method should:
- Obtain the outcome for the submitted work from
tryAcquireOutcome(boolean). - Invoke
LimitAlgorithm.Token.success(),LimitAlgorithm.Token.ignore(), orLimitAlgorithm.Token.dropped()on an accepted token to reflect the final state of the work item. - Throw
LimitExceptionif no token is available.
If the callable throws
IgnoreTaskException, callIgnoreTaskException.handle()after marking the token as ignored so the return value or wrapped exception is preserved.- Type Parameters:
T- the callable return type- Parameters:
callable- callable to execute within the limit- Returns:
- result of the callable with the outcome
- Throws:
LimitException- in case the limiter did not have an available permitException- in case the task failed with an exception
- Obtain the outcome for the submitted work from
-
run
Invoke a runnable within the limits of this limiter.Custom implementations that override this method should:
- Obtain the outcome for the submitted work from
tryAcquireOutcome(boolean). - Invoke
LimitAlgorithm.Token.success(),LimitAlgorithm.Token.ignore(), orLimitAlgorithm.Token.dropped()on an accepted token to reflect the final state of the work item. - Throw
LimitExceptionif no token is available.
If the runnable throws
IgnoreTaskException, callIgnoreTaskException.handle()after marking the token as ignored so the wrapped exception is preserved when present.- Parameters:
runnable- runnable to execute within the limit- Returns:
Outcomefrom the limit algorithm- Throws:
LimitException- in case the limiter did not have an available permitException- in case the task failed with an exception
- Obtain the outcome for the submitted work from
-
tryAcquireOutcome
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.Tokenoperations to release the token. If the response is rejected, the limit does not have an available token.- Returns:
LimitAlgorithm.Outcomeof the tryAcquire attempt.
-
tryAcquireOutcome
Tries to acquire a token, waiting for available permits for the configured amount of time ifwaitis enabled and the implementation supports queueing, returning immediately otherwise.If acquired, the caller must call one of the
LimitAlgorithm.Tokenoperations to release the token. If the response is rejected, 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:
- outcome of the acquisition attempt
-