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() {
try {
removeCount.set(0);
long currentTime = System.currentTimeMillis();
Set<String> removedIps = new HashSet<>();
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
if (accessTimes.isEmpty()) {
removedIps.add(ip);
}
});
removedIps.forEach(IP_ACCESS_TIMES::remove);
synchronized (IpAccessCounter.class) {
try {
removeCount.set(0);
long currentTime = System.currentTimeMillis();
Set<String> removedIps = new HashSet<>();
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
if (accessTimes.isEmpty()) {
removedIps.add(ip);
}
});
removedIps.forEach(IP_ACCESS_TIMES::remove);
Set<String> removedBlacklistIps = new HashSet<>();
BLACKLIST.entrySet().removeIf(entry -> {
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
if (shouldRemove) {
removedBlacklistIps.add(entry.getKey());
}
return shouldRemove;
});
Set<String> removedBlacklistIps = new HashSet<>();
BLACKLIST.entrySet().removeIf(entry -> {
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
if (shouldRemove) {
removedBlacklistIps.add(entry.getKey());
}
return shouldRemove;
});
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
} catch (Exception e) {
log.error("定时任务异常", e);
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
} catch (Exception e) {
log.error("定时任务异常", e);
}
}
}