提交 9a2903e4 编写于 作者: A Abhishek Chandrasekaran

update rate limit

Signed-off-by: NAbhishek Chandrasekaran <abchandrasekar@expediagroup.com>
上级 a394e061
......@@ -242,6 +242,21 @@ public class RedissonRateLimiter extends RedissonExpirable implements RRateLimit
+ "return redis.call('hsetnx', KEYS[1], 'type', ARGV[3]);",
Collections.singletonList(getName()), rate, unit.toMillis(rateInterval), type.ordinal());
}
@Override
public boolean updateRate(RateType type, long rate, long rateInterval, RateIntervalUnit unit) {
return get(updateRateAsync(type, rate, rateInterval, unit));
}
@Override
public RFuture<Boolean> updateRateAsync(RateType type, long rate, long rateInterval, RateIntervalUnit unit) {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"redis.call('hset', KEYS[1], 'rate', ARGV[1]);"
+ "redis.call('hset', KEYS[1], 'interval', ARGV[2]);"
+ "redis.call('hset', KEYS[1], 'type', ARGV[3]);"
+ "return redis.call('del', ARGV[4], ARGV[5]);",
Collections.<Object>singletonList(getName()), rate, unit.toMillis(rateInterval), type.ordinal(), getValueName(), getPermitsName());
}
private static final RedisCommand HGETALL = new RedisCommand("HGETALL", new MultiDecoder<RateLimiterConfig>() {
@Override
......
......@@ -36,6 +36,18 @@ public interface RRateLimiter extends RRateLimiterAsync, RExpirable {
* otherwise
*/
boolean trySetRate(RateType mode, long rate, long rateInterval, RateIntervalUnit rateIntervalUnit);
/**
* Updates RateLimiter's state and stores config to Redis server.
*
* @param mode - rate mode
* @param rate - rate
* @param rateInterval - rate time interval
* @param rateIntervalUnit - rate time interval unit
* @return {@code true} if rate was set and {@code false}
* otherwise
*/
boolean updateRate(RateType mode, long rate, long rateInterval, RateIntervalUnit rateIntervalUnit);
/**
* Acquires a permit only if one is available at the
......
......@@ -142,7 +142,21 @@ public interface RRateLimiterAsync extends RExpirableAsync {
* if the waiting time elapsed before a permit was acquired
*/
RFuture<Boolean> tryAcquireAsync(long permits, long timeout, TimeUnit unit);
/**
* Updates RateLimiter's state and stores config to Redis server.
*
*
* @param mode - rate mode
* @param rate - rate
* @param rateInterval - rate time interval
* @param rateIntervalUnit - rate time interval unit
* @return {@code true} if rate was set and {@code false}
* otherwise
*/
RFuture<Boolean> updateRateAsync(RateType mode, long rate, long rateInterval, RateIntervalUnit rateIntervalUnit);
/**
* Returns current configuration of this RateLimiter object.
*
......
......@@ -56,6 +56,17 @@ public class RedissonRateLimiterTest extends BaseTest {
assertThat(rr.getConfig().getRateInterval()).isEqualTo(5000);
assertThat(rr.getConfig().getRateType()).isEqualTo(RateType.OVERALL);
}
@Test
public void testUpdateRateConfig() {
RRateLimiter rr = redisson.getRateLimiter("acquire");
assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
assertThat(rr.updateRate(RateType.OVERALL, 2, 5, RateIntervalUnit.SECONDS)).isTrue();
assertThat(rr.getConfig().getRate()).isEqualTo(2);
assertThat(rr.getConfig().getRateInterval()).isEqualTo(5000);
assertThat(rr.getConfig().getRateType()).isEqualTo(RateType.OVERALL);
}
@Test
public void testPermitsExceeding() throws InterruptedException {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册