ip 请求次数限制13

This commit is contained in:
GYJ 2025-03-24 10:03:56 +08:00
parent de8385b8ca
commit 562f23759e
1 changed files with 25 additions and 23 deletions

View File

@ -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);
}
} }
} }