Selaa lähdekoodia

。。。。。

WANG 5 vuotta sitten
vanhempi
commit
ae19aa2d9d

+ 7 - 9
src/main/java/cn/com/qmth/examcloud/web/interceptor/ApiFlowLimitedInterceptor.java

@@ -2,7 +2,6 @@ package cn.com.qmth.examcloud.web.interceptor;
 
 import java.util.Map;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -46,6 +45,8 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
 
 	private static int allowedRate = 0;
 
+	private static int minCallRate = 0;
+
 	private static Properties props = new Properties();
 
 	static {
@@ -55,6 +56,7 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
 
 		enable = PropertyHolder.getBoolean("examcloud.api.flowLimited.enable", true);
 		allowedRate = PropertyHolder.getInt("examcloud.api.flowLimited.allowedRate", 5);
+		minCallRate = PropertyHolder.getInt("examcloud.api.flowLimited.minCallRate", 10);
 
 		new Thread(new Runnable() {
 			@Override
@@ -138,18 +140,14 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
 		}
 		if (limited) {
 
-			// 限流后放行率
 			int random = RandomUtils.nextInt(100);
 			if (random <= allowedRate) {
-				return true;
+				double oneMinuteRate = apiStatusInfo.getOneMinuteRate();
+				if (oneMinuteRate < minCallRate) {
+					return true;
+				}
 			}
 
-			// 我也不知道这是啥玩意儿 by wangwei
-			Double mean = apiStatusInfo.getMean();
-			int sleep = mean > 1000d ? 1000 : mean.intValue();
-			sleep = sleep < 100 ? 100 : sleep;
-			Util.sleep(TimeUnit.MILLISECONDS, sleep);
-
 			response.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
 			ServletUtil.returnJson(new StatusResponse("503", "limited"), response);
 			return false;