All Known Subinterfaces:
Limit
All Known Implementing Classes:
AimdLimit, FixedLimit

public interface LimitAlgorithm
Concurrency limit algorithm.

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

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    When a token is retrieved from tryAcquire(), 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 a Semaphore).
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    invoke(Runnable runnable)
    Invoke a runnable within the limits of this limiter.
    default <T> T
    invoke(Callable<T> callable)
    Invoke a callable within the limits of this limiter.
    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, if wait is enabled, returning immediately otherwise.
  • Method Details

    • invoke

      default <T> T invoke(Callable<T> callable) throws LimitException, Exception
      Invoke a callable within the limits of this limiter.

      Limit implementors note: Make sure to catch IgnoreTaskException from the callable, and call its IgnoreTaskException.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 permit
      Exception - in case the task failed with an exception
    • invoke

      default void invoke(Runnable runnable) throws LimitException, Exception
      Invoke a runnable within the limits of this limiter.

      Limit implementors note: Make sure to catch IgnoreTaskException from the runnable, and call its IgnoreTaskException.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 permit
      Exception - in case the task failed with an exception
    • tryAcquire

      default Optional<LimitAlgorithm.Token> 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

      Optional<LimitAlgorithm.Token> tryAcquire(boolean wait)
      Try to acquire a token, waiting for available permits for the configured amount of time, if wait 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