Interface ConcurrencyLimit

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@UnstableApi @FunctionalInterface public interface ConcurrencyLimit
Limits the concurrency of client requests.
  • Method Details

    • of

      static ConcurrencyLimit of(int maxConcurrency)
      Returns a newly-created ConcurrencyLimit with the specified maxConcurrency.
      Parameters:
      maxConcurrency - the maximum number of concurrent active requests. Specify 0 to disable the limit.
    • of

      @UnstableApi static ConcurrencyLimit of(IntSupplier maxConcurrency)
      Returns a newly-created ConcurrencyLimit with the specified IntSupplier. IntSupplier.getAsInt() might be frequently called, so please consider using SettableIntSupplier if supplying the value needs heavy computation. For example:
      
       ConcurrencyLimit limit = ConcurrencyLimit.of(new DynamicLimit());
      
       class DynamicLimit implements IntSupplier {
           private final SettableIntSupplier settableIntSupplier = SettableIntSupplier.of(16);
      
           DynamicLimit() {
               LimitChangeListener<Integer> listener = ...
               listener.addListener(updatedValue -> settableIntSupplier.set(updatedValue));
           }
      
           @Override
           public int getAsInt() {
               return settableIntSupplier.getAsInt();
           }
       }

      Note that IntSupplier must supply a positive number. Otherwise, all requests will end up in pending state.

    • builder

      static ConcurrencyLimitBuilder builder(int maxConcurrency)
      Returns a new ConcurrencyLimitBuilder with the specified maxConcurrency.
      Parameters:
      maxConcurrency - the maximum number of concurrent active requests. Specify 0 to disable the limit.
    • builder

      @UnstableApi static ConcurrencyLimitBuilder builder(IntSupplier maxConcurrency)
      Returns a new ConcurrencyLimitBuilder with the specified IntSupplier. IntSupplier.getAsInt() might be frequently called, so please consider using SettableIntSupplier if supplying the value needs heavy computation. For example:
      
       ConcurrencyLimitBuilder builder = ConcurrencyLimit.builder(new DynamicLimit());
      
       class DynamicLimit implements IntSupplier {
           private final SettableIntSupplier settableIntSupplier = SettableIntSupplier.of(16);
      
           DynamicLimit() {
               LimitChangeListener<Integer> listener = ...
               listener.addListener(updatedValue -> settableIntSupplier.set(updatedValue));
           }
      
           @Override
           public int getAsInt() {
               return settableIntSupplier.getAsInt();
           }
       }

      Note that IntSupplier must supply a positive number. Otherwise, all requests will end up in pending state.

    • acquire

      Acquires a SafeCloseable that allows you to execute a job under the limit. The SafeCloseable must be closed after the job is done:
      
       ConcurrencyLimit limit = ...
       limit.acquire(ctx).handle((permit, cause) -> {
           if (cause != null) {
               // Failed to acquire a permit.
               ...
           }
           // Execute your job.
           ...
           // Release the permit.
           permit.close();
       });