Search in sources :

Example 1 with RateLimiter

use of io.github.resilience4j.ratelimiter.annotation.RateLimiter in project resilience4j by resilience4j.

the class RateLimiterAspect method rateLimiterAroundAdvice.

@Around(value = "matchAnnotatedClassOrMethod(limitedService)", argNames = "proceedingJoinPoint, limitedService")
public Object rateLimiterAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, RateLimiter limitedService) throws Throwable {
    RateLimiter targetService = limitedService;
    Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();
    String methodName = method.getDeclaringClass().getName() + "#" + method.getName();
    if (targetService == null) {
        targetService = getRateLimiterAnnotation(proceedingJoinPoint);
    }
    String name = targetService.name();
    io.github.resilience4j.ratelimiter.RateLimiter rateLimiter = getOrCreateRateLimiter(methodName, name);
    return handleJoinPoint(proceedingJoinPoint, rateLimiter, methodName);
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) RateLimiter(io.github.resilience4j.ratelimiter.annotation.RateLimiter) Around(org.aspectj.lang.annotation.Around)

Example 2 with RateLimiter

use of io.github.resilience4j.ratelimiter.annotation.RateLimiter in project resilience4j by resilience4j.

the class RateLimiterAspect method getRateLimiterAnnotation.

private RateLimiter getRateLimiterAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
    RateLimiter rateLimiter = null;
    Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
    if (targetClass.isAnnotationPresent(RateLimiter.class)) {
        rateLimiter = targetClass.getAnnotation(RateLimiter.class);
        if (rateLimiter == null) {
            rateLimiter = targetClass.getDeclaredAnnotation(RateLimiter.class);
        }
        if (rateLimiter == null) {
            logger.debug("TargetClass has no declared annotation 'RateLimiter'");
        }
    }
    return rateLimiter;
}
Also used : RateLimiter(io.github.resilience4j.ratelimiter.annotation.RateLimiter)

Aggregations

RateLimiter (io.github.resilience4j.ratelimiter.annotation.RateLimiter)2 Method (java.lang.reflect.Method)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1