Interface Bulkhead
- All Known Implementing Classes:
SemaphoreBulkhead
A Bulkhead represent an entity limiting the amount of parallel operations. It does not
assume nor does it mandate usage of any particular concurrency and/or io model. These details are
left for the client to manage. This bulkhead, depending on the underlying concurrency/io model
can be used to shed load, and, where it makes sense, limit resource use (i.e. limit amount of
threads/actors involved in a particular flow, etc).
In order to execute an operation protected by this bulkhead, a permission must be obtained by
calling tryAcquirePermission() ()} If the bulkhead is full, no additional
operations will be permitted to execute until space is available.
Once the operation is complete, regardless of the result, client needs to call onComplete() in order to maintain integrity of internal bulkhead state.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classThis class decorates future with Bulkhead functionality around invocation.static interfaceAn EventPublisher which can be used to register event consumers.static interface -
Method Summary
Modifier and TypeMethodDescriptionvoidAcquires a permission to execute a call, only if one is available at the time of invocation If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.voidchangeConfig(BulkheadConfig newConfig) Dynamic bulkhead configuration change.static <T> Callable<T>decorateCallable(Bulkhead bulkhead, Callable<T> callable) Returns a callable which is decorated by a bulkhead.static <T> io.github.resilience4j.core.functions.CheckedConsumer<T>decorateCheckedConsumer(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedConsumer<T> consumer) Returns a consumer which is decorated by a bulkhead.static <T,R> io.github.resilience4j.core.functions.CheckedFunction<T, R> decorateCheckedFunction(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedFunction<T, R> function) Returns a function which is decorated by a bulkhead.static io.github.resilience4j.core.functions.CheckedRunnabledecorateCheckedRunnable(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedRunnable runnable) Returns a runnable which is decorated by a bulkhead.static <T> io.github.resilience4j.core.functions.CheckedSupplier<T>decorateCheckedSupplier(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedSupplier<T> supplier) Returns a supplier which is decorated by a bulkhead.static <T> Supplier<CompletionStage<T>>decorateCompletionStage(Bulkhead bulkhead, Supplier<CompletionStage<T>> supplier) Returns a supplier which is decorated by a bulkhead.static <T> Consumer<T>decorateConsumer(Bulkhead bulkhead, Consumer<T> consumer) Returns a consumer which is decorated by a bulkhead.static <T,R> Function<T, R> decorateFunction(Bulkhead bulkhead, Function<T, R> function) Returns a function which is decorated by a bulkhead.decorateFuture(Bulkhead bulkhead, Supplier<Future<T>> supplier) Returns a supplier of type Future which is decorated by a bulkhead.static RunnabledecorateRunnable(Bulkhead bulkhead, Runnable runnable) Returns a runnable which is decorated by a bulkhead.static <T> Supplier<T>decorateSupplier(Bulkhead bulkhead, Supplier<T> supplier) Returns a supplier which is decorated by a bulkhead.default <T> TexecuteCallable(Callable<T> callable) Decorates and executes the decorated Callable.default <T> TexecuteCheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier<T> checkedSupplier) Decorates and executes the decorated Supplier.default <T> CompletionStage<T>executeCompletionStage(Supplier<CompletionStage<T>> supplier) Decorates and executes the decorated CompletionStage.default voidexecuteRunnable(Runnable runnable) Decorates and executes the decorated Runnable.default <T> TexecuteSupplier(Supplier<T> supplier) Decorates and executes the decorated Supplier.Returns the BulkheadConfig of this Bulkhead.Returns an EventPublisher which subscribes to the reactive stream of BulkheadEvent and can be used to register event consumers.Get the Metrics of this Bulkhead.getName()Returns the name of this bulkhead.getTags()Returns an unmodifiable map with tags assigned to this Bulkhead.static Bulkheadof(String name, BulkheadConfig config) Creates a bulkhead with a custom configurationstatic BulkheadCreates a bulkhead with a custom configurationstatic Bulkheadof(String name, Supplier<BulkheadConfig> bulkheadConfigSupplier) Creates a bulkhead with a custom configurationstatic BulkheadCreates a bulkhead with a custom configurationstatic BulkheadofDefaults(String name) Create a Bulkhead with a default configuration.voidRecords a completed call and releases a permission.voidReleases a permission and increases the number of available permits by one.booleanAcquires a permission to execute a call, only if one is available at the time of invocation.
-
Method Details
-
decorateCheckedSupplier
static <T> io.github.resilience4j.core.functions.CheckedSupplier<T> decorateCheckedSupplier(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedSupplier<T> supplier) Returns a supplier which is decorated by a bulkhead.- Type Parameters:
T- the type of results supplied by this supplier- Parameters:
bulkhead- the Bulkheadsupplier- the original supplier- Returns:
- a supplier which is decorated by a Bulkhead.
-
decorateCompletionStage
static <T> Supplier<CompletionStage<T>> decorateCompletionStage(Bulkhead bulkhead, Supplier<CompletionStage<T>> supplier) Returns a supplier which is decorated by a bulkhead.- Type Parameters:
T- the type of the returned CompletionStage's result- Parameters:
bulkhead- the bulkheadsupplier- the original supplier- Returns:
- a supplier which is decorated by a Bulkhead.
-
decorateFuture
Returns a supplier of type Future which is decorated by a bulkhead. Bulkhead will reserve permission untilFuture.get()orFuture.get(long, TimeUnit)is evaluated even if the underlying call took less time to return. Any delays in evaluating future will result in holding of permission in the underlying Semaphore.- Type Parameters:
T- the type of the returned Future result- Parameters:
bulkhead- the bulkheadsupplier- the original supplier- Returns:
- a supplier which is decorated by a Bulkhead.
-
decorateCheckedRunnable
static io.github.resilience4j.core.functions.CheckedRunnable decorateCheckedRunnable(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedRunnable runnable) Returns a runnable which is decorated by a bulkhead.- Parameters:
bulkhead- the bulkheadrunnable- the original runnable- Returns:
- a runnable which is decorated by a Bulkhead.
-
decorateCallable
Returns a callable which is decorated by a bulkhead.- Type Parameters:
T- the result type of callable- Parameters:
bulkhead- the bulkheadcallable- the original Callable- Returns:
- a supplier which is decorated by a Bulkhead.
-
decorateSupplier
Returns a supplier which is decorated by a bulkhead.- Type Parameters:
T- the type of results supplied by this supplier- Parameters:
bulkhead- the bulkheadsupplier- the original supplier- Returns:
- a supplier which is decorated by a Bulkhead.
-
decorateConsumer
Returns a consumer which is decorated by a bulkhead.- Type Parameters:
T- the type of the input to the consumer- Parameters:
bulkhead- the bulkheadconsumer- the original consumer- Returns:
- a consumer which is decorated by a Bulkhead.
-
decorateCheckedConsumer
static <T> io.github.resilience4j.core.functions.CheckedConsumer<T> decorateCheckedConsumer(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedConsumer<T> consumer) Returns a consumer which is decorated by a bulkhead.- Type Parameters:
T- the type of the input to the consumer- Parameters:
bulkhead- the bulkheadconsumer- the original consumer- Returns:
- a consumer which is decorated by a Bulkhead.
-
decorateRunnable
Returns a runnable which is decorated by a bulkhead.- Parameters:
bulkhead- the bulkheadrunnable- the original runnable- Returns:
- a runnable which is decorated by a bulkhead.
-
decorateFunction
Returns a function which is decorated by a bulkhead.- Type Parameters:
T- the type of the input to the functionR- the type of the result of the function- Parameters:
bulkhead- the bulkheadfunction- the original function- Returns:
- a function which is decorated by a bulkhead.
-
decorateCheckedFunction
static <T,R> io.github.resilience4j.core.functions.CheckedFunction<T,R> decorateCheckedFunction(Bulkhead bulkhead, io.github.resilience4j.core.functions.CheckedFunction<T, R> function) Returns a function which is decorated by a bulkhead.- Type Parameters:
T- the type of the input to the functionR- the type of the result of the function- Parameters:
bulkhead- the bulkheadfunction- the original function- Returns:
- a function which is decorated by a bulkhead.
-
ofDefaults
Create a Bulkhead with a default configuration.- Parameters:
name- the name of the bulkhead- Returns:
- a Bulkhead instance
-
of
Creates a bulkhead with a custom configuration- Parameters:
name- the name of the bulkheadconfig- a custom BulkheadConfig configuration- Returns:
- a Bulkhead instance
-
of
Creates a bulkhead with a custom configuration- Parameters:
name- the name of the bulkheadconfig- a custom BulkheadConfig configurationtags- tags added to the Bulkhead- Returns:
- a Bulkhead instance
-
of
Creates a bulkhead with a custom configuration- Parameters:
name- the name of the bulkheadbulkheadConfigSupplier- custom configuration supplier- Returns:
- a Bulkhead instance
-
of
static Bulkhead of(String name, Supplier<BulkheadConfig> bulkheadConfigSupplier, Map<String, String> tags) Creates a bulkhead with a custom configuration- Parameters:
name- the name of the bulkheadbulkheadConfigSupplier- custom configuration suppliertags- tags added to the Bulkhead- Returns:
- a Bulkhead instance
-
changeConfig
Dynamic bulkhead configuration change. NOTE! New `maxWaitTime` duration won't affect threads that are currently waiting for permission.- Parameters:
newConfig- new BulkheadConfig
-
tryAcquirePermission
boolean tryAcquirePermission()Acquires a permission to execute a call, only if one is available at the time of invocation. If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.- Returns:
trueif a permission was acquired andfalseotherwise
-
acquirePermission
void acquirePermission()Acquires a permission to execute a call, only if one is available at the time of invocation If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.- Throws:
BulkheadFullException- when the Bulkhead is full and no further calls are permitted.io.github.resilience4j.core.exception.AcquirePermissionCancelledException- if thread was interrupted during permission wait
-
releasePermission
void releasePermission()Releases a permission and increases the number of available permits by one.Should only be used when a permission was acquired but not used. Otherwise use
onComplete()to signal a completed call and release a permission. -
onComplete
void onComplete()Records a completed call and releases a permission. -
getName
String getName()Returns the name of this bulkhead.- Returns:
- the name of this bulkhead
-
getBulkheadConfig
BulkheadConfig getBulkheadConfig()Returns the BulkheadConfig of this Bulkhead.- Returns:
- bulkhead config
-
getMetrics
Bulkhead.Metrics getMetrics()Get the Metrics of this Bulkhead.- Returns:
- the Metrics of this Bulkhead
-
getTags
Returns an unmodifiable map with tags assigned to this Bulkhead.- Returns:
- the tags assigned to this Retry in an unmodifiable map
-
getEventPublisher
Bulkhead.EventPublisher getEventPublisher()Returns an EventPublisher which subscribes to the reactive stream of BulkheadEvent and can be used to register event consumers.- Returns:
- an EventPublisher
-
executeSupplier
Decorates and executes the decorated Supplier.- Type Parameters:
T- the type of results supplied by this supplier- Parameters:
supplier- the original Supplier- Returns:
- the result of the decorated Supplier.
-
executeCallable
Decorates and executes the decorated Callable.- Type Parameters:
T- the result type of callable- Parameters:
callable- the original Callable- Returns:
- the result of the decorated Callable.
- Throws:
Exception- if unable to compute a result
-
executeRunnable
Decorates and executes the decorated Runnable.- Parameters:
runnable- the original Runnable
-
executeCheckedSupplier
default <T> T executeCheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier<T> checkedSupplier) throws Throwable Decorates and executes the decorated Supplier.- Type Parameters:
T- the type of results supplied by this supplier- Parameters:
checkedSupplier- the original Supplier- Returns:
- the result of the decorated Supplier.
- Throws:
Throwable- if something goes wrong applying this function to the given arguments
-
executeCompletionStage
Decorates and executes the decorated CompletionStage.- Type Parameters:
T- the type of results supplied by this supplier- Parameters:
supplier- the original CompletionStage- Returns:
- the decorated CompletionStage.
-