private void releaseBulkhead() { if (permitted.compareAndSet(Permit.ACQUIRED, Permit.RELEASED)) { bulkhead.onComplete(); } } }
private void releaseBulkhead() { if (wasCallPermitted()) { bulkhead.onComplete(); } } }
private void releaseBulkhead() { if (wasCallPermitted()) { bulkhead.onComplete(); } } }
/** * Returns a callable which is decorated by a bulkhead. * * @param bulkhead the bulkhead * @param callable the original Callable * @param <T> the result type of callable * * @return a supplier which is decorated by a Bulkhead. */ static <T> Callable<T> decorateCallable(Bulkhead bulkhead, Callable<T> callable){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try { return callable.call(); } finally { bulkhead.onComplete(); } }; }
/** * Returns a consumer which is decorated by a bulkhead. * @param bulkhead the bulkhead * @param consumer the original consumer * @param <T> the type of the input to the consumer * * @return a consumer which is decorated by a Bulkhead. */ static <T> Consumer<T> decorateConsumer(Bulkhead bulkhead, Consumer<T> consumer){ return (t) -> { BulkheadUtils.isCallPermitted(bulkhead); try { consumer.accept(t); } finally { bulkhead.onComplete(); } }; }
/** * Returns a runnable which is decorated by a bulkhead. * * @param bulkhead the bulkhead * @param runnable the original runnable * * @return a runnable which is decorated by a bulkhead. */ static Runnable decorateRunnable(Bulkhead bulkhead, Runnable runnable){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try{ runnable.run(); } finally { bulkhead.onComplete(); } }; }
/** * Returns a function which is decorated by a bulkhead. * @param bulkhead the bulkhead * @param function the original function * @param <T> the type of the input to the function * @param <R> the type of the result of the function * @return a function which is decorated by a bulkhead. */ static <T, R> Function<T, R> decorateFunction(Bulkhead bulkhead, Function<T, R> function){ return (T t) -> { BulkheadUtils.isCallPermitted(bulkhead); try{ return function.apply(t); } finally { bulkhead.onComplete(); } }; }
/** * Returns a supplier which is decorated by a bulkhead. * * @param bulkhead the bulkhead * @param supplier the original supplier * @param <T> the type of results supplied by this supplier * * @return a supplier which is decorated by a Bulkhead. */ static <T> Supplier<T> decorateSupplier(Bulkhead bulkhead, Supplier<T> supplier){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try { return supplier.get(); } finally { bulkhead.onComplete(); } }; }
@Override public void onError(Throwable e) { if (isInvocationPermitted()) { bulkhead.onComplete(); childSubscriber.onError(e); } }
@Override public void hookOnError(Throwable t) { if (wasCallPermitted()) { bulkhead.onComplete(); actual.onError(t); } }
.whenComplete( (result, throwable) -> { bulkhead.onComplete(); if (throwable != null) { promise.completeExceptionally(throwable); bulkhead.onComplete(); promise.completeExceptionally(throwable);
/** * Returns a consumer which is decorated by a bulkhead. * @param bulkhead the bulkhead * @param consumer the original consumer * @param <T> the type of the input to the consumer * * @return a consumer which is decorated by a Bulkhead. */ static <T> CheckedConsumer<T> decorateCheckedConsumer(Bulkhead bulkhead, CheckedConsumer<T> consumer){ return (t) -> { BulkheadUtils.isCallPermitted(bulkhead); try { consumer.accept(t); } finally { bulkhead.onComplete(); } }; }
/** * Returns a function which is decorated by a bulkhead. * * @param bulkhead the bulkhead * @param function the original function * @param <T> the type of the input to the function * @param <R> the type of the result of the function * @return a function which is decorated by a bulkhead. */ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(Bulkhead bulkhead, CheckedFunction1<T, R> function){ return (T t) -> { BulkheadUtils.isCallPermitted(bulkhead); try{ return function.apply(t); } finally { bulkhead.onComplete(); } }; }
/** * Returns a supplier which is decorated by a bulkhead. * * @param bulkhead the Bulkhead * @param supplier the original supplier * @param <T> the type of results supplied by this supplier * @return a supplier which is decorated by a Bulkhead. */ static <T> CheckedFunction0<T> decorateCheckedSupplier(Bulkhead bulkhead, CheckedFunction0<T> supplier){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try { return supplier.apply(); } finally { bulkhead.onComplete(); } }; }
/** * Returns a runnable which is decorated by a bulkhead. * * @param bulkhead the bulkhead * @param runnable the original runnable * * @return a runnable which is decorated by a Bulkhead. */ static CheckedRunnable decorateCheckedRunnable(Bulkhead bulkhead, CheckedRunnable runnable){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try{ runnable.run(); } finally { bulkhead.onComplete(); } }; }
private Object handleOther(MethodInvocation invocation, io.github.resilience4j.bulkhead.Bulkhead bulkhead, RecoveryFunction<?> recoveryFunction) throws Throwable { boolean permission = bulkhead.isCallPermitted(); if (!permission) { Throwable t = new BulkheadFullException(String.format("Bulkhead '%s' is full", bulkhead.getName())); return recoveryFunction.apply(t); } try { if (Thread.interrupted()) { throw new IllegalStateException("Thread was interrupted during permission wait"); } return invocation.proceed(); } catch (Exception e) { return recoveryFunction.apply(e); } finally { bulkhead.onComplete(); } } }
if (bulkhead.isCallPermitted()) { return ((CompletionStage<?>) invocation.proceed()).handle((o, throwable) -> { bulkhead.onComplete(); if (throwable != null) { try {
@Test public void testBulkhead() throws InterruptedException { bulkhead.isCallPermitted(); bulkhead.isCallPermitted(); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0); bulkhead.isCallPermitted(); bulkhead.onComplete(); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1); bulkhead.onComplete(); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(2); bulkhead.isCallPermitted(); testSubscriber.assertValueCount(6) .assertValues(CALL_PERMITTED, CALL_PERMITTED, CALL_REJECTED, CALL_FINISHED, CALL_FINISHED, CALL_PERMITTED); }
@Test public void shouldConsumeOnCallFinishedEventOnComplete() throws Exception { // Given Bulkhead bulkhead = Bulkhead.of("test", config); // When bulkhead.getEventPublisher() .onCallFinished(event -> logger.info(event.getEventType().toString())); bulkhead.onComplete(); // Then then(logger).should(times(1)).info("CALL_FINISHED"); } }
private void releaseBulkhead() { if (permitted.compareAndSet(Permit.ACQUIRED, Permit.RELEASED)) { bulkhead.onComplete(); } } }