mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-06-11 16:07:27 +00:00
fix: RateLimiter 添加过期条目清理,修复 ipRequestMap 无限增长
当 Map 超过 1000 条目时触发惰性清理,移除所有已过期的 IP 条目。 原实现中过期条目只重置计数不删除 key,长期运行后 Map 持续膨胀。 同时消除多余的 ipRequestMap.get(ip) 调用,直接使用 compute() 返回值。
This commit is contained in:
@@ -38,7 +38,13 @@ public class RateLimiter {
|
||||
|
||||
String ip = request.remoteAddress().host();
|
||||
|
||||
ipRequestMap.compute(ip, (key, requestInfo) -> {
|
||||
// 定期清理过期条目,防止 Map 无限增长
|
||||
if (ipRequestMap.size() > 1000) {
|
||||
long now = System.currentTimeMillis();
|
||||
ipRequestMap.entrySet().removeIf(entry -> now - entry.getValue().timestamp > TIME_WINDOW);
|
||||
}
|
||||
|
||||
RequestInfo info = ipRequestMap.compute(ip, (key, requestInfo) -> {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (requestInfo == null || currentTime - requestInfo.timestamp > TIME_WINDOW) {
|
||||
// 初始化或重置计数器
|
||||
@@ -50,7 +56,6 @@ public class RateLimiter {
|
||||
}
|
||||
});
|
||||
|
||||
RequestInfo info = ipRequestMap.get(ip);
|
||||
if (info.count > MAX_REQUESTS) {
|
||||
// 超过限制
|
||||
// 计算剩余时间
|
||||
|
||||
Reference in New Issue
Block a user