Search in sources :

Example 1 with BulkheadFullException

use of io.github.resilience4j.bulkhead.BulkheadFullException in project resilience4j by resilience4j.

the class BulkheadTransformer method apply.

@Override
public Upstream<T> apply(Upstream<? extends T> upstream) throws Exception {
    return down -> {
        if (bulkhead.isCallPermitted()) {
            // do not allow permits to leak
            upstream.connect(new Downstream<T>() {

                @Override
                public void success(T value) {
                    bulkhead.onComplete();
                    down.success(value);
                }

                @Override
                public void error(Throwable throwable) {
                    bulkhead.onComplete();
                    try {
                        if (recover != null) {
                            down.success(recover.apply(throwable));
                        } else {
                            down.error(throwable);
                        }
                    } catch (Throwable t) {
                        down.error(t);
                    }
                }

                @Override
                public void complete() {
                    bulkhead.onComplete();
                    down.complete();
                }
            });
        } else {
            Throwable t = new BulkheadFullException(String.format("Bulkhead '%s' is full", bulkhead.getName()));
            if (recover != null) {
                try {
                    down.success(recover.apply(t));
                } catch (Throwable t2) {
                    down.error(t2);
                }
            } else {
                down.error(t);
            }
        }
    };
}
Also used : Downstream(ratpack.exec.Downstream) Function(ratpack.func.Function) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) Upstream(ratpack.exec.Upstream) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Downstream(ratpack.exec.Downstream)

Example 2 with BulkheadFullException

use of io.github.resilience4j.bulkhead.BulkheadFullException in project resilience4j by resilience4j.

the class BulkheadSubscriber method onSubscribe.

@Override
public void onSubscribe(Subscription subscription) {
    if (SubscriptionHelper.setOnce(this, subscription)) {
        if (acquireCallPermit()) {
            childSubscriber.onSubscribe(this);
        } else {
            cancel();
            childSubscriber.onSubscribe(this);
            childSubscriber.onError(new BulkheadFullException(String.format("Bulkhead '%s' is full", bulkhead.getName())));
        }
    }
}
Also used : BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException)

Example 3 with BulkheadFullException

use of io.github.resilience4j.bulkhead.BulkheadFullException in project resilience4j by resilience4j.

the class BulkheadMethodInterceptor method invoke.

@SuppressWarnings("unchecked")
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Bulkhead annotation = invocation.getMethod().getAnnotation(Bulkhead.class);
    RecoveryFunction<?> recoveryFunction = annotation.recovery().newInstance();
    if (registry == null) {
        registry = BulkheadRegistry.ofDefaults();
    }
    io.github.resilience4j.bulkhead.Bulkhead bulkhead = registry.bulkhead(annotation.name());
    if (bulkhead == null) {
        return invocation.proceed();
    }
    Class<?> returnType = invocation.getMethod().getReturnType();
    if (Promise.class.isAssignableFrom(returnType)) {
        Promise<?> result = (Promise<?>) invocation.proceed();
        if (result != null) {
            BulkheadTransformer transformer = BulkheadTransformer.of(bulkhead).recover(recoveryFunction);
            result = result.transform(transformer);
        }
        return result;
    } else if (Observable.class.isAssignableFrom(returnType)) {
        Observable<?> result = (Observable<?>) invocation.proceed();
        if (result != null) {
            BulkheadOperator operator = BulkheadOperator.of(bulkhead);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Flowable.class.isAssignableFrom(returnType)) {
        Flowable<?> result = (Flowable<?>) invocation.proceed();
        if (result != null) {
            BulkheadOperator operator = BulkheadOperator.of(bulkhead);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Single.class.isAssignableFrom(returnType)) {
        Single<?> result = (Single<?>) invocation.proceed();
        if (result != null) {
            BulkheadOperator operator = BulkheadOperator.of(bulkhead);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (CompletionStage.class.isAssignableFrom(returnType)) {
        if (bulkhead.isCallPermitted()) {
            return ((CompletionStage<?>) invocation.proceed()).handle((o, throwable) -> {
                bulkhead.onComplete();
                if (throwable != null) {
                    try {
                        return recoveryFunction.apply(throwable);
                    } catch (Exception e) {
                        throw Exceptions.uncheck(throwable);
                    }
                } else {
                    return o;
                }
            });
        } else {
            final CompletableFuture promise = new CompletableFuture<>();
            Throwable t = new BulkheadFullException(String.format("Bulkhead '%s' is full", bulkhead.getName()));
            try {
                promise.complete(recoveryFunction.apply(t));
            } catch (Throwable t2) {
                promise.completeExceptionally(t2);
            }
            return promise;
        }
    }
    return handleOther(invocation, bulkhead, recoveryFunction);
}
Also used : BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Inject(com.google.inject.Inject) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) Single(io.reactivex.Single) BulkheadOperator(io.github.resilience4j.bulkhead.operator.BulkheadOperator) RecoveryFunction(io.github.resilience4j.ratpack.recovery.RecoveryFunction) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CompletionStage(java.util.concurrent.CompletionStage) Flowable(io.reactivex.Flowable) Exceptions(ratpack.util.Exceptions) Observable(io.reactivex.Observable) BulkheadOperator(io.github.resilience4j.bulkhead.operator.BulkheadOperator) Observable(io.reactivex.Observable) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) Single(io.reactivex.Single) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) CompletionStage(java.util.concurrent.CompletionStage) Flowable(io.reactivex.Flowable)

Aggregations

BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)3 Inject (com.google.inject.Inject)1 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)1 BulkheadOperator (io.github.resilience4j.bulkhead.operator.BulkheadOperator)1 RecoveryFunction (io.github.resilience4j.ratpack.recovery.RecoveryFunction)1 Flowable (io.reactivex.Flowable)1 Observable (io.reactivex.Observable)1 Single (io.reactivex.Single)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 Downstream (ratpack.exec.Downstream)1 Promise (ratpack.exec.Promise)1 Upstream (ratpack.exec.Upstream)1 Function (ratpack.func.Function)1 Exceptions (ratpack.util.Exceptions)1