You've already forked FrameTour-BE
指定设备提前预约
This commit is contained in:
@ -5,28 +5,36 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FixedRateLimiter {
|
||||
public class FixedRateLimiter implements IRateLimiter {
|
||||
private final Semaphore semaphore = new Semaphore(1);
|
||||
private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
|
||||
|
||||
public FixedRateLimiter(int rate, TimeUnit timeUnit) {
|
||||
// 启动一个线程每0.5秒释放一个许可
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
synchronized (semaphore) {
|
||||
if (semaphore.availablePermits() < 1) {
|
||||
semaphore.release(1);
|
||||
}
|
||||
if (semaphore.availablePermits() < 1) {
|
||||
semaphore.release(1);
|
||||
}
|
||||
}, rate, rate, timeUnit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acquire() throws InterruptedException {
|
||||
synchronized (semaphore) {
|
||||
semaphore.acquire();
|
||||
}
|
||||
semaphore.acquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
scheduler.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire() {
|
||||
return semaphore.tryAcquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
return semaphore.tryAcquire(timeout, unit);
|
||||
}
|
||||
}
|
||||
|
10
src/main/java/com/ycwl/basic/ratelimiter/IRateLimiter.java
Normal file
10
src/main/java/com/ycwl/basic/ratelimiter/IRateLimiter.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.ycwl.basic.ratelimiter;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public interface IRateLimiter {
|
||||
void acquire() throws InterruptedException;
|
||||
void shutdown();
|
||||
boolean tryAcquire();
|
||||
boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException;
|
||||
}
|
@ -6,25 +6,45 @@ import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
public class SlidingWindowRateLimiter {
|
||||
public class SlidingWindowRateLimiter implements IRateLimiter {
|
||||
private final Semaphore semaphore;
|
||||
private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
|
||||
|
||||
public SlidingWindowRateLimiter(int maxRequestsPerSecond) {
|
||||
this.semaphore = new Semaphore(maxRequestsPerSecond);
|
||||
int rate = 1000000 / maxRequestsPerSecond;
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
if (semaphore.availablePermits() < maxRequestsPerSecond) {
|
||||
semaphore.release(1);
|
||||
semaphore.release(maxRequestsPerSecond - semaphore.availablePermits());
|
||||
}
|
||||
}, rate, rate, TimeUnit.MICROSECONDS);
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public SlidingWindowRateLimiter(int maxRequests, int perSecond) {
|
||||
this.semaphore = new Semaphore(maxRequests);
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
if (semaphore.availablePermits() < maxRequests) {
|
||||
semaphore.release(maxRequests - semaphore.availablePermits());
|
||||
}
|
||||
}, perSecond, perSecond, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acquire() throws InterruptedException {
|
||||
semaphore.acquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
scheduler.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire() {
|
||||
return semaphore.tryAcquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
return semaphore.tryAcquire(timeout, unit);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user