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

public class FixedLimit extends Object implements Limit, SemaphoreLimit, RuntimeType.Api<FixedLimitConfig>
Semaphore based limit, that supports queuing for a permit, and timeout on the queue. The default behavior is non-queuing.
See Also:
  • Field Details

    • DEFAULT_LIMIT

      public static final int DEFAULT_LIMIT
      Default limit, meaning unlimited execution.
      See Also:
    • DEFAULT_QUEUE_LENGTH

      public static final int DEFAULT_QUEUE_LENGTH
      Default length of the queue.
      See Also:
    • DEFAULT_QUEUE_TIMEOUT_DURATION

      public static final String DEFAULT_QUEUE_TIMEOUT_DURATION
      Timeout of a request that is enqueued.
      See Also:
  • Method Details

    • builder

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

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

      public static FixedLimit create(Semaphore semaphore)
      Create an instance from the provided semaphore.
      Parameters:
      semaphore - semaphore to use
      Returns:
      a new fixed limit backed by the provided semaphore
    • create

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

      public static FixedLimit create(FixedLimitConfig config)
      Create a new instance from configuration.
      Parameters:
      config - configuration of the fixed limit
      Returns:
      a new limit instance configured from config
    • create

      public static FixedLimit create(Consumer<FixedLimitConfig.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
    • prototype

      public FixedLimitConfig 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<FixedLimitConfig>
      Returns:
      prototype object used to create this 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
    • 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