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.
Limits the concurrency of client requests.
-
Method Summary
Modifier and TypeMethodDescriptionAcquires aSafeCloseable
that allows you to execute a job under the limit.static ConcurrencyLimitBuilder
builder
(int maxConcurrency) Returns a newConcurrencyLimitBuilder
with the specifiedmaxConcurrency
.static ConcurrencyLimitBuilder
builder
(IntSupplier maxConcurrency) Returns a newConcurrencyLimitBuilder
with the specifiedIntSupplier
.static ConcurrencyLimit
of
(int maxConcurrency) Returns a newly-createdConcurrencyLimit
with the specifiedmaxConcurrency
.static ConcurrencyLimit
of
(IntSupplier maxConcurrency) Returns a newly-createdConcurrencyLimit
with the specifiedIntSupplier
.
-
Method Details
-
of
Returns a newly-createdConcurrencyLimit
with the specifiedmaxConcurrency
.- Parameters:
maxConcurrency
- the maximum number of concurrent active requests. Specify0
to disable the limit.
-
of
Returns a newly-createdConcurrencyLimit
with the specifiedIntSupplier
.IntSupplier.getAsInt()
might be frequently called, so please consider usingSettableIntSupplier
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
Returns a newConcurrencyLimitBuilder
with the specifiedmaxConcurrency
.- Parameters:
maxConcurrency
- the maximum number of concurrent active requests. Specify0
to disable the limit.
-
builder
Returns a newConcurrencyLimitBuilder
with the specifiedIntSupplier
.IntSupplier.getAsInt()
might be frequently called, so please consider usingSettableIntSupplier
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 aSafeCloseable
that allows you to execute a job under the limit. TheSafeCloseable
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(); });
-