初始化时吧所有的都用了

This commit is contained in:
2025-04-05 17:43:22 +08:00
parent cecc7aa181
commit c8874064f0
3 changed files with 32 additions and 3 deletions

View File

@@ -12,6 +12,9 @@ public class SlidingWindowRateLimiter implements IRateLimiter {
public SlidingWindowRateLimiter(int maxRequestsPerSecond) {
this.semaphore = new Semaphore(maxRequestsPerSecond);
for (int i = 0; i < maxRequestsPerSecond; i++) {
semaphore.tryAcquire();
}
scheduler.scheduleAtFixedRate(() -> {
if (semaphore.availablePermits() < maxRequestsPerSecond) {
semaphore.release(maxRequestsPerSecond - semaphore.availablePermits());
@@ -21,6 +24,9 @@ public class SlidingWindowRateLimiter implements IRateLimiter {
public SlidingWindowRateLimiter(int maxRequests, int perSecond) {
this.semaphore = new Semaphore(maxRequests);
for (int i = 0; i < maxRequests; i++) {
semaphore.tryAcquire();
}
scheduler.scheduleAtFixedRate(() -> {
if (semaphore.availablePermits() < maxRequests) {
semaphore.release(maxRequests - semaphore.availablePermits());