feat: 增加获取令牌等待超时方法
This commit is contained in:
parent
d5b3af3a3b
commit
7edef26ec9
@ -18,6 +18,8 @@ public interface RateLimiter {
|
||||
*/
|
||||
boolean tryAcquire();
|
||||
|
||||
boolean tryAcquire(long timeoutMillis);
|
||||
|
||||
/**
|
||||
* 获取窗口类型
|
||||
*
|
||||
|
||||
@ -12,6 +12,7 @@ import org.redisson.api.RateType;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class RedisRateLimiterImpl implements RateLimiter {
|
||||
@ -26,6 +27,12 @@ public class RedisRateLimiterImpl implements RateLimiter {
|
||||
|
||||
private RRateLimiter rateLimiter;
|
||||
|
||||
/**
|
||||
* 默认超时时间(毫秒)
|
||||
*/
|
||||
private static final long DEFAULT_TIME_OUT_MILLIS = 5 * 1000;
|
||||
|
||||
|
||||
@Builder
|
||||
RedisRateLimiterImpl(RedissonClient redissonClient,
|
||||
WindowType windowType,
|
||||
@ -56,6 +63,11 @@ public class RedisRateLimiterImpl implements RateLimiter {
|
||||
return rateLimiterWorker.tryAcquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire(long timeoutMillis) {
|
||||
return rateLimiterWorker.tryAcquire(timeoutMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WindowType getWindowType() {
|
||||
return windowType;
|
||||
@ -89,6 +101,11 @@ public class RedisRateLimiterImpl implements RateLimiter {
|
||||
public boolean tryAcquire() {
|
||||
return rateLimiter.tryAcquire(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire(long timeoutMillis) {
|
||||
return rateLimiter.tryAcquire(1, timeoutMillis, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
interface RateLimiterWorker {
|
||||
@ -98,5 +115,7 @@ public class RedisRateLimiterImpl implements RateLimiter {
|
||||
* @return 如果获取成功则返回true, 失败则为false
|
||||
*/
|
||||
boolean tryAcquire();
|
||||
|
||||
boolean tryAcquire(long timeoutMillis);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user