java.lang.Object
io.helidon.common.concurrency.limits.AimdLimit
All Implemented Interfaces:
RuntimeType.Api<AimdLimitConfig>, Limit, LimitAlgorithm, SemaphoreLimit, NamedService

public class AimdLimit extends Object implements Limit, SemaphoreLimit, RuntimeType.Api<AimdLimitConfig>
AIMD based limiter.

The additive-increase/multiplicative-decrease (AIMD) algorithm is a feedback control algorithm best known for its use in TCP congestion control. AIMD combines linear growth of the congestion window when there is no congestion with an exponential reduction when congestion is detected.

  • Method Details

    • builder

      public static AimdLimitConfig.Builder builder()
      Create a new fluent API builder to construct AimdLimit instance.
      Returns:
      fluent API builder
    • create

      public static AimdLimit create()
      Create a new instance with all defaults.
      Returns:
      a new limit instance
    • create

      public static AimdLimit create(Config config)
      Create a new instance from configuration.
      Parameters:
      config - configuration of the AIMD limit
      Returns:
      a new limit instance configured from config
    • create

      public static AimdLimit create(AimdLimitConfig config)
      Create a new instance from configuration.
      Parameters:
      config - configuration of the AIMD limit
      Returns:
      a new limit instance configured from config
    • create

      public static AimdLimit create(Consumer<AimdLimitConfig.Builder> consumer)
      Create a new instance customizing its configuration.
      Parameters:
      consumer - consumer of configuration builder
      Returns:
      a new limit instance configured from the builder
    • invoke

      public <T> T invoke(Callable<T> callable) throws Exception
      Description copied from interface: LimitAlgorithm
      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.

      Specified by:
      invoke in interface LimitAlgorithm
      Type Parameters:
      T - the callable return type
      Parameters:
      callable - callable to execute within the limit
      Returns:
      result of the callable
      Throws:
      Exception - in case the task failed with an exception
    • invoke

      public void invoke(Runnable runnable) throws Exception
      Description copied from interface: LimitAlgorithm
      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.

      Specified by:
      invoke in interface LimitAlgorithm
      Parameters:
      runnable - runnable to execute within the limit
      Throws:
      Exception - in case the task failed with an exception
    • tryAcquire

      public Optional<LimitAlgorithm.Token> tryAcquire(boolean wait)
      Description copied from interface: LimitAlgorithm
      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.

      Specified by:
      tryAcquire in interface LimitAlgorithm
      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
    • semaphore

      public Semaphore semaphore()
      Description copied from interface: SemaphoreLimit
      Underlying semaphore of this limit.
      Specified by:
      semaphore in interface SemaphoreLimit
      Returns:
      the semaphore instance
    • name

      public String name()
      Description copied from interface: NamedService
      Name of this implementation, as provided in ConfiguredProvider.create(Config, String).
      Specified by:
      name in interface NamedService
      Returns:
      name of this service
    • type

      public String type()
      Description copied from interface: NamedService
      Type of this implementation, to distinguish instances of same type, with different NamedService.name(). Use for example ConfiguredProvider.configKey() to define the type.
      Specified by:
      type in interface NamedService
      Returns:
      type of this service
    • prototype

      public AimdLimitConfig prototype()
      Description copied from interface: RuntimeType.Api
      The prototype as it was received when creating this runtime object instance.
      Specified by:
      prototype in interface RuntimeType.Api<AimdLimitConfig>
      Returns:
      prototype object used to create this instance
    • copy

      public Limit copy()
      Description copied from interface: Limit
      Create a copy of this limit with the same configuration.
      Specified by:
      copy in interface Limit
      Returns:
      a copy of this limit