Skip to content

Commit 8ceb69c

Browse files
committed
流量溢出后添加自定义返回格式
1 parent b8e8f0f commit 8ceb69c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

api-boot-project/api-boot-plugins/api-boot-plugin-rate-limiter/src/main/java/org/minbox/framework/api/boot/plugin/rate/limiter/aop/interceptor/ApiBootRateLimiterMethodInterceptor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import org.aopalliance.intercept.MethodInvocation;
2222
import org.minbox.framework.api.boot.plugin.rate.limiter.ApiBootRateLimiter;
2323
import org.minbox.framework.api.boot.plugin.rate.limiter.annotation.RateLimiter;
24+
import org.minbox.framework.api.boot.plugin.rate.limiter.result.RateLimiterOverFlowRequest;
2425
import org.minbox.framework.api.boot.plugin.tools.AopTools;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728
import org.springframework.aop.support.AopUtils;
2829
import org.springframework.util.Assert;
30+
import org.springframework.util.ObjectUtils;
2931

3032
import java.lang.reflect.Method;
3133

@@ -50,9 +52,14 @@ public class ApiBootRateLimiterMethodInterceptor implements MethodInterceptor {
5052
* ApiBoot RateLimiter
5153
*/
5254
private ApiBootRateLimiter apiBootRateLimiter;
55+
/**
56+
* Response results after flow exceeding
57+
*/
58+
private RateLimiterOverFlowRequest overFlowRequest;
5359

54-
public ApiBootRateLimiterMethodInterceptor(ApiBootRateLimiter apiBootRateLimiter) {
60+
public ApiBootRateLimiterMethodInterceptor(ApiBootRateLimiter apiBootRateLimiter, RateLimiterOverFlowRequest overFlowRequest) {
5561
this.apiBootRateLimiter = apiBootRateLimiter;
62+
this.overFlowRequest = overFlowRequest;
5663
Assert.notNull(apiBootRateLimiter, "No ApiBootRateLimiter implementation class instance.");
5764
logger.info("ApiBootDefaultRateLimiterInterceptorHandler load complete.");
5865
}
@@ -82,6 +89,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
8289
} catch (Exception e) {
8390
logger.error("Current Limiting Request Encountered Exception.", e);
8491
}
92+
// If an instance is created
93+
if (!ObjectUtils.isEmpty(overFlowRequest)) {
94+
return overFlowRequest.overflow(invocation.getArguments());
95+
}
8596
return null;
8697
}
8798

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
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+
18+
package org.minbox.framework.api.boot.plugin.rate.limiter.result;
19+
20+
/**
21+
* Response results after flow exceeding
22+
*
23+
* @author:恒宇少年 - 于起宇
24+
* <p>
25+
* DateTime:2019-05-24 17:53
26+
* Blog:http://blog.yuqiyu.com
27+
* WebSite:http://www.jianshu.com/u/092df3f77bca
28+
* Gitee:https://gitee.com/hengboy
29+
* GitHub:https://github.com/hengboy
30+
*/
31+
@FunctionalInterface
32+
public interface RateLimiterOverFlowRequest {
33+
/**
34+
* Response results after flow exceeding
35+
*
36+
* @param methodArgs request method args
37+
* @return
38+
*/
39+
Object overflow(Object[] methodArgs);
40+
}

0 commit comments

Comments
 (0)