public abstract class SemaphoreRateLimiter extends Object implements RateLimiter
Semaphore 实现的限流器| Modifier and Type | Field and Description |
|---|---|
protected RateLimiterConfig |
config |
protected Semaphore |
semaphore |
| Constructor and Description |
|---|
SemaphoreRateLimiter(RateLimiterConfig config)
构造
|
SemaphoreRateLimiter(RateLimiterConfig config,
Semaphore semaphore)
构造
|
| Modifier and Type | Method and Description |
|---|---|
void |
acquire(int permits)
获取许可,阻塞方法,如果没有足够的许可,则阻塞等待
|
abstract void |
refreshLimit()
刷新限制,用户可重写此方法,改变填充许可方式,如:
填满窗口,一般用于固定窗口(Fixed Window)
固定频率填充,如每个周期只填充1个,配合
RateLimiterConfig.getRefreshPeriod(),可实现令牌桶(Token Bucket)
同样,用户可通过调用此方法手动刷新注意:重写此方法前需判断许可是否已满 |
boolean |
tryAcquire(int permits)
尝试获取许可,非阻塞方法
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitacquire, tryAcquireprotected final RateLimiterConfig config
protected final Semaphore semaphore
public SemaphoreRateLimiter(RateLimiterConfig config)
config - 限流配置public SemaphoreRateLimiter(RateLimiterConfig config, Semaphore semaphore)
config - 限流配置semaphore - Semaphore,默认使用RateLimiterConfig.getCapacity()创建public boolean tryAcquire(int permits)
RateLimitertryAcquire in interface RateLimiterpermits - 需要的许可数true表示成功获取,false表示无足够的许可可获取,此时需要等待给定的时间public void acquire(int permits)
RateLimiteracquire in interface RateLimiterpermits - 需要的许可数public abstract void refreshLimit()
RateLimiterConfig.getRefreshPeriod(),可实现令牌桶(Token Bucket)RateLimiterConfig.getCapacity()Copyright © 2025. All rights reserved.