You've already forked FrameTour-BE
规整下代码
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.ycwl.basic.utils.ratelimiter;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FixedRateLimiter implements IRateLimiter {
|
||||
private final Semaphore semaphore = new Semaphore(1);
|
||||
private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
|
||||
|
||||
public FixedRateLimiter(float maxRequestsPerSecond) {
|
||||
int rate = Float.valueOf(1000000 / maxRequestsPerSecond).intValue();
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
if (semaphore.availablePermits() < 1) {
|
||||
semaphore.release(1);
|
||||
}
|
||||
}, rate, rate, TimeUnit.NANOSECONDS);
|
||||
}
|
||||
|
||||
|
||||
public FixedRateLimiter(int rate, TimeUnit timeUnit) {
|
||||
// 启动一个线程每0.5秒释放一个许可
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
if (semaphore.availablePermits() < 1) {
|
||||
semaphore.release(1);
|
||||
}
|
||||
}, rate, rate, timeUnit);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package com.ycwl.basic.utils.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;
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package com.ycwl.basic.utils.ratelimiter;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
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);
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
if (semaphore.availablePermits() < maxRequestsPerSecond) {
|
||||
semaphore.release(maxRequestsPerSecond - semaphore.availablePermits());
|
||||
}
|
||||
}, 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