mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-06-11 07:57:28 +00:00
fix: RateLimiter 移除 synchronized 并添加 volatile,修复事件循环阻塞
审查发现 synchronized 在 Vert.x 事件循环中会严重阻塞并发。 ConcurrentHashMap 本身已线程安全,移除 synchronized 锁。 RequestInfo 字段添加 volatile 保证多线程内存可见性。
This commit is contained in:
@@ -28,7 +28,7 @@ public class RateLimiter {
|
|||||||
MAX_REQUESTS, TIME_WINDOW, PATH_REG);
|
MAX_REQUESTS, TIME_WINDOW, PATH_REG);
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public static Future<Void> checkRateLimit(HttpServerRequest request) {
|
public static Future<Void> checkRateLimit(HttpServerRequest request) {
|
||||||
Promise<Void> promise = Promise.promise();
|
Promise<Void> promise = Promise.promise();
|
||||||
if (!request.path().matches(PATH_REG)) {
|
if (!request.path().matches(PATH_REG)) {
|
||||||
// 如果请求路径不匹配正则,则不进行限流
|
// 如果请求路径不匹配正则,则不进行限流
|
||||||
@@ -71,8 +71,8 @@ public class RateLimiter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class RequestInfo {
|
private static class RequestInfo {
|
||||||
int count;
|
volatile int count;
|
||||||
long timestamp;
|
volatile long timestamp;
|
||||||
|
|
||||||
RequestInfo(int count, long time) {
|
RequestInfo(int count, long time) {
|
||||||
this.count = count;
|
this.count = count;
|
||||||
|
|||||||
Reference in New Issue
Block a user