This commit is contained in:
2025-01-26 02:21:27 +08:00
parent 7bd9a7507f
commit 1b11342e5d
32 changed files with 310 additions and 133 deletions

View File

@ -12,11 +12,14 @@ public class SlidingWindowRateLimiter {
public SlidingWindowRateLimiter(int maxRequestsPerSecond) {
this.semaphore = new Semaphore(maxRequestsPerSecond);
// Schedule a task to release all permits every second
scheduler.scheduleAtFixedRate(() -> semaphore.release(maxRequestsPerSecond - semaphore.availablePermits()), 1, 1, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(() -> {
if (semaphore.availablePermits() < maxRequestsPerSecond) {
semaphore.release(1);
}
}, 0, (1000 / maxRequestsPerSecond), TimeUnit.MILLISECONDS);
}
public void allowRequest() throws InterruptedException {
public void aquire() throws InterruptedException {
semaphore.acquire();
}