ip 请求次数限制14

This commit is contained in:
GYJ 2025-03-24 11:05:48 +08:00
parent 562f23759e
commit 9c8a990843
1 changed files with 43 additions and 41 deletions

View File

@ -23,13 +23,46 @@ public class IpAccessCounter {
// 十分钟的毫秒数 // 十分钟的毫秒数
private static final long TEN_MINUTES = 10 * ONE_MINUTE; private static final long TEN_MINUTES = 10 * ONE_MINUTE;
private static AtomicInteger removeCount = new AtomicInteger(0); // private static AtomicInteger removeCount = new AtomicInteger(0);
// static { static {
// // 定时任务每分钟清理一次访问记录 // 定时任务每分钟清理一次访问记录
// ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
// executor.scheduleAtFixedRate(() -> { executor.scheduleAtFixedRate(() -> {
synchronized (IpAccessCounter.class) {
try {
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;
});
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
} catch (Exception e) {
log.error("定时任务异常", e);
}
}
}, ONE_MINUTE, 20000, TimeUnit.MINUTES);
}
// public static void removeOldInfo() {
// synchronized (IpAccessCounter.class) {
// try { // try {
// removeCount.set(0);
// long currentTime = System.currentTimeMillis(); // long currentTime = System.currentTimeMillis();
// Set<String> removedIps = new HashSet<>(); // Set<String> removedIps = new HashSet<>();
// IP_ACCESS_TIMES.forEach((ip, accessTimes) -> { // IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
@ -54,46 +87,15 @@ public class IpAccessCounter {
// } catch (Exception e) { // } catch (Exception e) {
// log.error("定时任务异常", e); // log.error("定时任务异常", e);
// } // }
// }, ONE_MINUTE, 20000, TimeUnit.MINUTES); // }
// } // }
public static void removeOldInfo() {
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;
});
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
} catch (Exception e) {
log.error("定时任务异常", e);
}
}
}
// 检查 IP 是否可以访问 // 检查 IP 是否可以访问
public static boolean canAccess(String ip, Integer maxAccessCount) { public static boolean canAccess(String ip, Integer maxAccessCount) {
removeCount.getAndIncrement(); // removeCount.getAndIncrement();
if (removeCount.get() > 400) { // if (removeCount.get() > 400) {
removeOldInfo(); // removeOldInfo();
} // }
if (isBlacklisted(ip)) { if (isBlacklisted(ip)) {
return false; return false;
} }