Search in sources :

Example 1 with ThrottlingException

use of com.weddini.throttling.ThrottlingException in project spring-boot-throttling by weddini.

the class ThrottlingInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    if (HandlerMethod.class.isInstance(handler)) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Throttling annotation = handlerMethod.getMethod().getAnnotation(Throttling.class);
        if (annotation != null) {
            String evaluatedValue = throttlingEvaluator.evaluate(annotation, handlerMethod.getBean(), handlerMethod.getBeanType(), handlerMethod.getMethod(), handlerMethod.getMethodParameters());
            ThrottlingKey key = ThrottlingKey.builder().method(handlerMethod.getMethod()).annotation(annotation).evaluatedValue(evaluatedValue).build();
            boolean isHandlingAllowed = throttlingService.throttle(key, evaluatedValue);
            if (!isHandlingAllowed) {
                if (logger.isDebugEnabled()) {
                    logger.debug("cannot proceed with a handling http request [" + request.getRequestURI() + "] due to @Throttling configuration, type=" + annotation.type() + ", value=" + evaluatedValue);
                }
                throw new ThrottlingException();
            }
        }
    }
    return true;
}
Also used : Throttling(com.weddini.throttling.Throttling) ThrottlingKey(com.weddini.throttling.ThrottlingKey) ThrottlingException(com.weddini.throttling.ThrottlingException) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 2 with ThrottlingException

use of com.weddini.throttling.ThrottlingException in project spring-boot-throttling by weddini.

the class ThrottlingBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> clazz = beanNamesToOriginalClasses.get(beanName);
    if (clazz == null) {
        return bean;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("replacing bean '" + beanName + "' with a proxy");
    }
    return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), (proxy, method, args) -> {
        Throttling annotation = findAnnotation(clazz.getMethod(method.getName(), method.getParameterTypes()), Throttling.class);
        if (annotation != null) {
            final String evaluatedValue = throttlingEvaluator.evaluate(annotation, bean, clazz, method, args);
            ThrottlingKey key = ThrottlingKey.builder().method(method).annotation(annotation).evaluatedValue(evaluatedValue).build();
            boolean isAllowed = throttlingService.throttle(key, evaluatedValue);
            if (!isAllowed) {
                if (logger.isDebugEnabled()) {
                    logger.debug("cannot proceed with a method call due to @Throttling configuration, type=" + annotation.type() + ", value=" + evaluatedValue);
                }
                throw new ThrottlingException();
            }
        }
        // call original method
        return ReflectionUtils.invokeMethod(method, bean, args);
    });
}
Also used : Throttling(com.weddini.throttling.Throttling) ThrottlingKey(com.weddini.throttling.ThrottlingKey) ThrottlingException(com.weddini.throttling.ThrottlingException)

Aggregations

Throttling (com.weddini.throttling.Throttling)2 ThrottlingException (com.weddini.throttling.ThrottlingException)2 ThrottlingKey (com.weddini.throttling.ThrottlingKey)2 HandlerMethod (org.springframework.web.method.HandlerMethod)1