Search in sources :

Example 1 with RequestNotPermitted

use of io.github.resilience4j.ratelimiter.RequestNotPermitted in project resilience4j by resilience4j.

the class RateLimiterHandler method handle.

@Override
public void handle(Context ctx) throws Exception {
    Duration timeoutDuration = rateLimiter.getRateLimiterConfig().getTimeoutDuration();
    boolean permission = rateLimiter.getPermission(timeoutDuration);
    if (Thread.interrupted()) {
        throw new IllegalStateException("Thread was interrupted during permission wait");
    }
    if (!permission) {
        Throwable t = new RequestNotPermitted("Request not permitted for limiter: " + rateLimiter.getName());
        ctx.error(t);
    } else {
        ctx.next();
    }
}
Also used : RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) Duration(java.time.Duration)

Example 2 with RequestNotPermitted

use of io.github.resilience4j.ratelimiter.RequestNotPermitted in project resilience4j by resilience4j.

the class RateLimiterMethodInterceptor method handleProceedWithException.

private Object handleProceedWithException(MethodInvocation invocation, io.github.resilience4j.ratelimiter.RateLimiter rateLimiter, RecoveryFunction<?> recoveryFunction) throws Throwable {
    RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    Duration timeoutDuration = rateLimiterConfig.getTimeoutDuration();
    boolean permission = rateLimiter.getPermission(timeoutDuration);
    if (Thread.interrupted()) {
        throw new IllegalStateException("Thread was interrupted during permission wait");
    }
    if (!permission) {
        Throwable t = new RequestNotPermitted("Request not permitted for limiter: " + rateLimiter.getName());
        return recoveryFunction.apply(t);
    }
    return invocation.proceed();
}
Also used : RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) Duration(java.time.Duration)

Example 3 with RequestNotPermitted

use of io.github.resilience4j.ratelimiter.RequestNotPermitted 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

RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)3 Duration (java.time.Duration)3 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)2 Inject (com.google.inject.Inject)1 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)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 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