Search in sources :

Example 1 with RecoveryFunction

use of io.github.resilience4j.ratpack.recovery.RecoveryFunction in project resilience4j by resilience4j.

the class RateLimiterMethodInterceptor method invoke.

@SuppressWarnings("unchecked")
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    RateLimiter annotation = invocation.getMethod().getAnnotation(RateLimiter.class);
    RecoveryFunction<?> recoveryFunction = annotation.recovery().newInstance();
    if (registry == null) {
        registry = RateLimiterRegistry.ofDefaults();
    }
    io.github.resilience4j.ratelimiter.RateLimiter rateLimiter = registry.rateLimiter(annotation.name());
    if (rateLimiter == null) {
        return invocation.proceed();
    }
    Class<?> returnType = invocation.getMethod().getReturnType();
    if (Promise.class.isAssignableFrom(returnType)) {
        Promise<?> result = (Promise<?>) proceed(invocation, rateLimiter, recoveryFunction);
        if (result != null) {
            RateLimiterTransformer transformer = RateLimiterTransformer.of(rateLimiter).recover(recoveryFunction);
            result = result.transform(transformer);
        }
        return result;
    } else if (Observable.class.isAssignableFrom(returnType)) {
        Observable<?> result = (Observable<?>) proceed(invocation, rateLimiter, recoveryFunction);
        if (result != null) {
            RateLimiterOperator operator = RateLimiterOperator.of(rateLimiter);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Flowable.class.isAssignableFrom(returnType)) {
        Flowable<?> result = (Flowable<?>) proceed(invocation, rateLimiter, recoveryFunction);
        if (result != null) {
            RateLimiterOperator operator = RateLimiterOperator.of(rateLimiter);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Single.class.isAssignableFrom(returnType)) {
        Single<?> result = (Single<?>) proceed(invocation, rateLimiter, recoveryFunction);
        if (result != null) {
            RateLimiterOperator operator = RateLimiterOperator.of(rateLimiter);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (CompletionStage.class.isAssignableFrom(returnType)) {
        RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
        Duration timeoutDuration = rateLimiterConfig.getTimeoutDuration();
        if (rateLimiter.getPermission(timeoutDuration)) {
            return proceed(invocation, rateLimiter, recoveryFunction);
        } else {
            final CompletableFuture promise = new CompletableFuture<>();
            Throwable t = new RequestNotPermitted("Request not permitted for limiter: " + rateLimiter.getName());
            try {
                promise.complete(recoveryFunction.apply(t));
            } catch (Throwable t2) {
                promise.completeExceptionally(t2);
            }
            return promise;
        }
    }
    return handleProceedWithException(invocation, rateLimiter, recoveryFunction);
}
Also used : Inject(com.google.inject.Inject) RateLimiterOperator(io.github.resilience4j.ratelimiter.operator.RateLimiterOperator) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) Single(io.reactivex.Single) 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) Duration(java.time.Duration) Observable(io.reactivex.Observable) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) Duration(java.time.Duration) Observable(io.reactivex.Observable) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) RateLimiterOperator(io.github.resilience4j.ratelimiter.operator.RateLimiterOperator) Single(io.reactivex.Single) Flowable(io.reactivex.Flowable)

Example 2 with RecoveryFunction

use of io.github.resilience4j.ratpack.recovery.RecoveryFunction in project resilience4j by resilience4j.

the class RetryMethodInterceptor method invoke.

@SuppressWarnings("unchecked")
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Retry annotation = invocation.getMethod().getAnnotation(Retry.class);
    io.github.resilience4j.retry.Retry retry = registry.retry(annotation.name());
    if (retry == null) {
        return invocation.proceed();
    }
    RecoveryFunction<?> recoveryFunction = annotation.recovery().newInstance();
    if (registry == null) {
        registry = RetryRegistry.ofDefaults();
    }
    Class<?> returnType = invocation.getMethod().getReturnType();
    if (Promise.class.isAssignableFrom(returnType)) {
        Promise<?> result = (Promise<?>) proceed(invocation, retry, recoveryFunction);
        if (result != null) {
            RetryTransformer transformer = RetryTransformer.of(retry).recover(recoveryFunction);
            result = result.transform(transformer);
        }
        return result;
    } else if (Observable.class.isAssignableFrom(returnType)) {
        Observable<?> result = (Observable<?>) proceed(invocation, retry, recoveryFunction);
        if (result != null) {
            io.github.resilience4j.retry.transformer.RetryTransformer transformer = io.github.resilience4j.retry.transformer.RetryTransformer.of(retry);
            result = result.compose(transformer).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Flowable.class.isAssignableFrom(returnType)) {
        Flowable<?> result = (Flowable<?>) proceed(invocation, retry, recoveryFunction);
        if (result != null) {
            io.github.resilience4j.retry.transformer.RetryTransformer transformer = io.github.resilience4j.retry.transformer.RetryTransformer.of(retry);
            result = result.compose(transformer).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Single.class.isAssignableFrom(returnType)) {
        Single<?> result = (Single<?>) proceed(invocation, retry, recoveryFunction);
        if (result != null) {
            io.github.resilience4j.retry.transformer.RetryTransformer transformer = io.github.resilience4j.retry.transformer.RetryTransformer.of(retry);
            result = result.compose(transformer).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (CompletionStage.class.isAssignableFrom(returnType)) {
        CompletionStage stage = (CompletionStage) proceed(invocation, retry, recoveryFunction);
        return executeCompletionStage(invocation, stage, retry.context(), recoveryFunction);
    }
    return proceed(invocation, retry, recoveryFunction);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CompletionStage(java.util.concurrent.CompletionStage) Flowable(io.reactivex.Flowable) Inject(com.google.inject.Inject) Observable(io.reactivex.Observable) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Single(io.reactivex.Single) RecoveryFunction(io.github.resilience4j.ratpack.recovery.RecoveryFunction) Observable(io.reactivex.Observable) Promise(ratpack.exec.Promise) Single(io.reactivex.Single) CompletionStage(java.util.concurrent.CompletionStage) Flowable(io.reactivex.Flowable)

Example 3 with RecoveryFunction

use of io.github.resilience4j.ratpack.recovery.RecoveryFunction in project resilience4j by resilience4j.

the class CircuitBreakerMethodInterceptor method invoke.

@SuppressWarnings("unchecked")
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    CircuitBreaker annotation = invocation.getMethod().getAnnotation(CircuitBreaker.class);
    RecoveryFunction<?> recoveryFunction = annotation.recovery().newInstance();
    if (registry == null) {
        registry = CircuitBreakerRegistry.ofDefaults();
    }
    io.github.resilience4j.circuitbreaker.CircuitBreaker breaker = registry.circuitBreaker(annotation.name());
    if (breaker == null) {
        return invocation.proceed();
    }
    Class<?> returnType = invocation.getMethod().getReturnType();
    if (Promise.class.isAssignableFrom(returnType)) {
        Promise<?> result = (Promise<?>) proceed(invocation, breaker, recoveryFunction);
        if (result != null) {
            CircuitBreakerTransformer transformer = CircuitBreakerTransformer.of(breaker).recover(recoveryFunction);
            result = result.transform(transformer);
        }
        return result;
    } else if (Observable.class.isAssignableFrom(returnType)) {
        Observable<?> result = (Observable<?>) proceed(invocation, breaker, recoveryFunction);
        if (result != null) {
            CircuitBreakerOperator operator = CircuitBreakerOperator.of(breaker);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Flowable.class.isAssignableFrom(returnType)) {
        Flowable<?> result = (Flowable<?>) proceed(invocation, breaker, recoveryFunction);
        if (result != null) {
            CircuitBreakerOperator operator = CircuitBreakerOperator.of(breaker);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (Single.class.isAssignableFrom(returnType)) {
        Single<?> result = (Single<?>) proceed(invocation, breaker, recoveryFunction);
        if (result != null) {
            CircuitBreakerOperator operator = CircuitBreakerOperator.of(breaker);
            result = result.lift(operator).onErrorReturn(t -> recoveryFunction.apply((Throwable) t));
        }
        return result;
    } else if (CompletionStage.class.isAssignableFrom(returnType)) {
        final CompletableFuture promise = new CompletableFuture<>();
        if (breaker.isCallPermitted()) {
            CompletionStage<?> result = (CompletionStage<?>) proceed(invocation, breaker, recoveryFunction);
            if (result != null) {
                long start = System.nanoTime();
                result.whenComplete((v, t) -> {
                    long durationInNanos = System.nanoTime() - start;
                    if (t != null) {
                        breaker.onError(durationInNanos, t);
                        try {
                            promise.complete(recoveryFunction.apply((Throwable) t));
                        } catch (Exception e) {
                            promise.completeExceptionally(e);
                        }
                    } else {
                        breaker.onSuccess(durationInNanos);
                        promise.complete(v);
                    }
                });
            }
        } else {
            Throwable t = new CircuitBreakerOpenException(String.format("CircuitBreaker '%s' is open", breaker.getName()));
            try {
                promise.complete(recoveryFunction.apply((Throwable) t));
            } catch (Throwable t2) {
                promise.completeExceptionally(t2);
            }
        }
        return promise;
    }
    return proceed(invocation, breaker, recoveryFunction);
}
Also used : CircuitBreakerOperator(io.github.resilience4j.circuitbreaker.operator.CircuitBreakerOperator) Inject(com.google.inject.Inject) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) CircuitBreakerOperator(io.github.resilience4j.circuitbreaker.operator.CircuitBreakerOperator) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Single(io.reactivex.Single) 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) Observable(io.reactivex.Observable) Observable(io.reactivex.Observable) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) Promise(ratpack.exec.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) Single(io.reactivex.Single) CompletionStage(java.util.concurrent.CompletionStage) Flowable(io.reactivex.Flowable)

Example 4 with RecoveryFunction

use of io.github.resilience4j.ratpack.recovery.RecoveryFunction 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

Inject (com.google.inject.Inject)4 RecoveryFunction (io.github.resilience4j.ratpack.recovery.RecoveryFunction)4 Flowable (io.reactivex.Flowable)4 Observable (io.reactivex.Observable)4 Single (io.reactivex.Single)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CompletionStage (java.util.concurrent.CompletionStage)4 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)4 MethodInvocation (org.aopalliance.intercept.MethodInvocation)4 Promise (ratpack.exec.Promise)4 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)1 BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)1 BulkheadOperator (io.github.resilience4j.bulkhead.operator.BulkheadOperator)1 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)1 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)1 CircuitBreakerOperator (io.github.resilience4j.circuitbreaker.operator.CircuitBreakerOperator)1 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)1 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)1 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)1 RateLimiterOperator (io.github.resilience4j.ratelimiter.operator.RateLimiterOperator)1