Class FixedLimit

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

public class FixedLimit extends Object implements 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
    • 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
    • tryAcquireOutcome

      public LimitAlgorithm.Outcome tryAcquireOutcome(boolean wait)
      Description copied from interface: LimitAlgorithm
      Tries to acquire a token, waiting for available permits for the configured amount of time if wait is enabled and the implementation supports queueing, returning immediately otherwise.

      If acquired, the caller must call one of the LimitAlgorithm.Token operations to release the token. If the response is rejected, the limit does not have an available token.

      Specified by:
      tryAcquireOutcome in interface LimitAlgorithm
      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
    • call

      public <T> LimitAlgorithm.Result<T> call(Callable<T> callable) throws Exception
      Description copied from interface: LimitAlgorithm
      Invoke a callable within the limits of this limiter.

      Custom implementations that override this method should:

      If the callable throws IgnoreTaskException, call IgnoreTaskException.handle() after marking the token as ignored so the return value or wrapped exception is preserved.

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

      public LimitAlgorithm.Outcome run(Runnable runnable) throws Exception
      Description copied from interface: LimitAlgorithm
      Invoke a runnable within the limits of this limiter.

      Custom implementations that override this method should:

      If the runnable throws IgnoreTaskException, call IgnoreTaskException.handle() after marking the token as ignored so the wrapped exception is preserved when present.

      Specified by:
      run in interface LimitAlgorithm
      Parameters:
      runnable - runnable to execute within the limit
      Returns:
      Outcome from the limit algorithm
      Throws:
      Exception - in case the task failed with an exception
    • init

      public void init(Limit.InitializationContext context)
      Description copied from interface: Limit
      Initialization method for this limit. This method can be used for any task, including metrics initialization.
      Specified by:
      init in interface Limit
      Parameters:
      context - the context for limit initialization
    • init

      @Deprecated public void init(String originName)
      Deprecated.
      Description copied from interface: Limit
      Initialization method for this limit. This method can be used for any task, including metrics initialization.
      Specified by:
      init in interface Limit
      Parameters:
      originName - origin name for this limit, such as "@default"
    • initialPermits

      protected int initialPermits()
      Returns the initial number of permits set for this semaphore-based limit.

      The initial number of permits is used to initialize the underlying semaphore.

      Returns:
      the initial number of permits
    • concurrentRequests

      protected AtomicInteger concurrentRequests()
      Returns the AtomicInteger instance tracking the current number of concurrent requests.

      The returned AtomicInteger is used to maintain a count of the concurrent requests being processed.

      Returns:
      the AtomicInteger instance tracking concurrent requests
    • rejectedRequests

      protected AtomicInteger rejectedRequests()
      Returns the AtomicInteger instance tracking the number of rejected requests.
      Returns:
      rejected requests counter
    • doTryAcquireOutcome

      protected LimitAlgorithm.Outcome doTryAcquireOutcome(boolean wait)