Search in sources :

Example 1 with RateLimiterOperator

use of io.github.resilience4j.ratelimiter.operator.RateLimiterOperator 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)

Aggregations

Inject (com.google.inject.Inject)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 RecoveryFunction (io.github.resilience4j.ratpack.recovery.RecoveryFunction)1 Flowable (io.reactivex.Flowable)1 Observable (io.reactivex.Observable)1 Single (io.reactivex.Single)1 Duration (java.time.Duration)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 Promise (ratpack.exec.Promise)1