ip 请求次数限制13
This commit is contained in:
parent
de8385b8ca
commit
562f23759e
|
|
@ -58,31 +58,33 @@ public class IpAccessCounter {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public static void removeOldInfo() {
|
public static void removeOldInfo() {
|
||||||
try {
|
synchronized (IpAccessCounter.class) {
|
||||||
removeCount.set(0);
|
try {
|
||||||
long currentTime = System.currentTimeMillis();
|
removeCount.set(0);
|
||||||
Set<String> removedIps = new HashSet<>();
|
long currentTime = System.currentTimeMillis();
|
||||||
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
|
Set<String> removedIps = new HashSet<>();
|
||||||
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
|
||||||
if (accessTimes.isEmpty()) {
|
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
||||||
removedIps.add(ip);
|
if (accessTimes.isEmpty()) {
|
||||||
}
|
removedIps.add(ip);
|
||||||
});
|
}
|
||||||
removedIps.forEach(IP_ACCESS_TIMES::remove);
|
});
|
||||||
|
removedIps.forEach(IP_ACCESS_TIMES::remove);
|
||||||
|
|
||||||
Set<String> removedBlacklistIps = new HashSet<>();
|
Set<String> removedBlacklistIps = new HashSet<>();
|
||||||
BLACKLIST.entrySet().removeIf(entry -> {
|
BLACKLIST.entrySet().removeIf(entry -> {
|
||||||
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
|
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
|
||||||
if (shouldRemove) {
|
if (shouldRemove) {
|
||||||
removedBlacklistIps.add(entry.getKey());
|
removedBlacklistIps.add(entry.getKey());
|
||||||
}
|
}
|
||||||
return shouldRemove;
|
return shouldRemove;
|
||||||
});
|
});
|
||||||
|
|
||||||
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
|
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
|
||||||
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
|
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("定时任务异常", e);
|
log.error("定时任务异常", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue