Merge branch 'master' of https://github.com/dushitaoyuan/javaweb_security_handle
This commit is contained in:
@@ -61,29 +61,24 @@ public class RateLimitAspect {
|
|||||||
RateLimitType type = rateLimit.type();
|
RateLimitType type = rateLimit.type();
|
||||||
String key = rateLimit.key();
|
String key = rateLimit.key();
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
key = methodName;
|
type = RateLimitType.METHOD;
|
||||||
if (LOG.isDebugEnabled()) {
|
}
|
||||||
LOG.debug("{}限流策略未定义,采用[{}]限流策略", methodName, RateLimitType.METHOD);
|
switch (type) {
|
||||||
}
|
case IP:
|
||||||
} else {
|
if (key == null || key.isEmpty()) {
|
||||||
switch (type) {
|
key = RequestUtil.getRemoteIp() + "_" + methodName;
|
||||||
case IP:
|
} else {
|
||||||
String serviceKey = rateLimit.key();
|
key = RequestUtil.getRemoteIp() + "_" + key;
|
||||||
if (serviceKey == null || key.isEmpty()) {
|
}
|
||||||
key = RequestUtil.getRemoteIp() + "_" + methodName;
|
break;
|
||||||
} else {
|
case METHOD:
|
||||||
key = RequestUtil.getRemoteIp() + "_" + serviceKey;
|
key = methodName;
|
||||||
}
|
break;
|
||||||
break;
|
case SERVICE_KEY:
|
||||||
case METHOD:
|
break;
|
||||||
key = methodName;
|
case GLOBAL:
|
||||||
break;
|
key = "global";
|
||||||
case SERVICE_KEY:
|
break;
|
||||||
break;
|
|
||||||
case GLOBAL:
|
|
||||||
key = "global";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (LOG.isDebugEnabled()) {
|
if (LOG.isDebugEnabled()) {
|
||||||
LOG.debug("采用[{}]限流策略,限流key:{}", type, key);
|
LOG.debug("采用[{}]限流策略,限流key:{}", type, key);
|
||||||
@@ -102,4 +97,4 @@ public class RateLimitAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,10 @@ public class GuavaRateLimiter extends AbstractRateLimiter {
|
|||||||
rateHolder.clear();
|
rateHolder.clear();
|
||||||
}
|
}
|
||||||
RateLimiter rateLimiter = null;
|
RateLimiter rateLimiter = null;
|
||||||
if (rateHolder.containsKey(key)) {
|
if (!rateHolder.containsKey(key)) {
|
||||||
rateLimiter = rateHolder.get(key);
|
rateHolder.putIfAbsent(key, RateLimiter.create(limit));
|
||||||
} else {
|
|
||||||
rateLimiter = RateLimiter.create(limit);
|
|
||||||
}
|
}
|
||||||
rateHolder.putIfAbsent(key, rateLimiter);
|
rateLimiter = rateHolder.get(key);
|
||||||
return rateLimiter.tryAcquire(permits);
|
return rateLimiter.tryAcquire(permits);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,20 +52,16 @@ public class GuavaRateLimiter extends AbstractRateLimiter {
|
|||||||
countHolder.clear();
|
countHolder.clear();
|
||||||
}
|
}
|
||||||
LongAdder longAdder = null;
|
LongAdder longAdder = null;
|
||||||
if (countHolder.containsKey(key)) {
|
if (!countHolder.containsKey(key)) {
|
||||||
longAdder = countHolder.get(key);
|
countHolder.putIfAbsent(key, new LongAdder());
|
||||||
if (longAdder.longValue() >=totalCount) {
|
|
||||||
TOTAL_LIMIT_ZERO_FLAG.put(key);
|
|
||||||
countHolder.remove(key);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
longAdder.add(count);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
longAdder = countHolder.get(key);
|
||||||
longAdder = new LongAdder();
|
if (longAdder.longValue() >= totalCount) {
|
||||||
countHolder.putIfAbsent(key, longAdder);
|
TOTAL_LIMIT_ZERO_FLAG.put(key);
|
||||||
countHolder.get(key).add(count);
|
countHolder.remove(key);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
longAdder.add(count);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user