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();
}
}
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();
}
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);
}
Aggregations