|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.handler.advice; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | + |
| 21 | +import org.springframework.messaging.Message; |
| 22 | +import org.springframework.messaging.MessagingException; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +import io.github.resilience4j.ratelimiter.RateLimiter; |
| 26 | +import io.github.resilience4j.ratelimiter.RateLimiterConfig; |
| 27 | +import io.github.resilience4j.ratelimiter.RequestNotPermitted; |
| 28 | +import io.vavr.CheckedFunction0; |
| 29 | +import io.vavr.control.Try; |
| 30 | + |
| 31 | +/** |
| 32 | + * An {@link AbstractRequestHandlerAdvice} extension for a rate limiting to service method calls. |
| 33 | + * The implementation is based on the |
| 34 | + * <a href="https://github.com/resilience4j/resilience4j#ratelimiter">Resilience4j</a>. |
| 35 | + * |
| 36 | + * @author Artem Bilan |
| 37 | + * |
| 38 | + * @since 5.2 |
| 39 | + */ |
| 40 | +public class RateLimiterRequestHandlerAdvice extends AbstractRequestHandlerAdvice { |
| 41 | + |
| 42 | + public static final String DEFAULT_NAME = "RateLimiterRequestHandlerAdvice"; |
| 43 | + |
| 44 | + private final RateLimiter rateLimiter; |
| 45 | + |
| 46 | + /** |
| 47 | + * Construct an instance based on default rate limiter options |
| 48 | + * and {@value #DEFAULT_NAME} as a rate limiter name. |
| 49 | + * @see RateLimiter#ofDefaults |
| 50 | + */ |
| 51 | + public RateLimiterRequestHandlerAdvice() { |
| 52 | + this(RateLimiter.ofDefaults(DEFAULT_NAME)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Construct an instance based on default rate limiter options and provided name. |
| 57 | + * @param name the name for the rate limiter. |
| 58 | + */ |
| 59 | + public RateLimiterRequestHandlerAdvice(String name) { |
| 60 | + this(RateLimiter.ofDefaults(name)); |
| 61 | + Assert.hasText(name, "'name' must not be empty"); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Construct an instance based on the provided {@link RateLimiter}. |
| 66 | + * @param rateLimiter the {@link RateLimiter} to use. |
| 67 | + */ |
| 68 | + public RateLimiterRequestHandlerAdvice(RateLimiter rateLimiter) { |
| 69 | + Assert.notNull(rateLimiter, "'rateLimiter' must not be null"); |
| 70 | + this.rateLimiter = rateLimiter; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Construct an instance based on the provided {@link RateLimiterConfig} |
| 75 | + * and {@value #DEFAULT_NAME} as a rate limiter name. |
| 76 | + * @param rateLimiterConfig the {@link RateLimiterConfig} to use. |
| 77 | + */ |
| 78 | + public RateLimiterRequestHandlerAdvice(RateLimiterConfig rateLimiterConfig) { |
| 79 | + this(rateLimiterConfig, DEFAULT_NAME); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Construct an instance based on the provided {@link RateLimiterConfig} and name. |
| 84 | + * @param rateLimiterConfig the {@link RateLimiterConfig} to use. |
| 85 | + * @param name the name for the rate limiter. |
| 86 | + */ |
| 87 | + public RateLimiterRequestHandlerAdvice(RateLimiterConfig rateLimiterConfig, String name) { |
| 88 | + Assert.notNull(rateLimiterConfig, "'rateLimiterConfig' must not be null"); |
| 89 | + Assert.hasText(name, "'name' must not be empty"); |
| 90 | + this.rateLimiter = RateLimiter.of(name, rateLimiterConfig); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Change the {@code limitForPeriod} option of the {@link #rateLimiter}. |
| 95 | + * @param limitForPeriod the {@code limitForPeriod} to use. |
| 96 | + * @see RateLimiter#changeLimitForPeriod(int) |
| 97 | + */ |
| 98 | + public void setLimitForPeriod(int limitForPeriod) { |
| 99 | + this.rateLimiter.changeLimitForPeriod(limitForPeriod); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Change the {@code timeoutDuration} option of the {@link #rateLimiter}. |
| 104 | + * @param timeoutDuration the {@code timeoutDuration} to use. |
| 105 | + * @see RateLimiter#changeTimeoutDuration(Duration) |
| 106 | + */ |
| 107 | + public void setTimeoutDuration(Duration timeoutDuration) { |
| 108 | + this.rateLimiter.changeTimeoutDuration(timeoutDuration); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Obtain the metrics from the rate limiter. |
| 113 | + * @return the {@link RateLimiter.Metrics} from rate limiter. |
| 114 | + * @see RateLimiter#getMetrics() |
| 115 | + */ |
| 116 | + public RateLimiter.Metrics getMetrics() { |
| 117 | + return this.rateLimiter.getMetrics(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { |
| 122 | + CheckedFunction0<Object> restrictedCall = |
| 123 | + RateLimiter.decorateCheckedSupplier(this.rateLimiter, callback::execute); |
| 124 | + try { |
| 125 | + return Try.of(restrictedCall).get(); |
| 126 | + } |
| 127 | + catch (RequestNotPermitted ex) { |
| 128 | + throw new RateLimitExceededException(message, "Rate limit exceeded for: " + target, ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * A {@link MessagingException} wrapper for the {@link RequestNotPermitted} |
| 135 | + * with the {@code requestMessage} and {@code target} context. |
| 136 | + */ |
| 137 | + public static class RateLimitExceededException extends MessagingException { |
| 138 | + |
| 139 | + private static final long serialVersionUID = 1L; |
| 140 | + |
| 141 | + RateLimitExceededException(Message<?> message, String description, RequestNotPermitted cause) { |
| 142 | + super(message, description, cause); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments