Rename capacity param to burstCapacity
This commit is contained in:
@@ -4,7 +4,7 @@ package org.springframework.cloud.gateway.filter.ratelimit;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface RateLimiter {
|
||||
Response isAllowed(String id, int replenishRate, int capacity);
|
||||
Response isAllowed(String id, int replenishRate, int burstCapacity);
|
||||
|
||||
class Response {
|
||||
private final boolean allowed;
|
||||
|
||||
@@ -30,13 +30,13 @@ public class RedisRateLimiter implements RateLimiter {
|
||||
* This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically.
|
||||
* No other operations can run between fetching the count and writing the new count.
|
||||
* @param replenishRate
|
||||
* @param capacity
|
||||
* @param burstCapacity
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
//TODO: signature? params (tuple?). Return type, tokens left?
|
||||
public Response isAllowed(String id, int replenishRate, int capacity) {
|
||||
public Response isAllowed(String id, int replenishRate, int burstCapacity) {
|
||||
|
||||
try {
|
||||
// Make a unique key per user.
|
||||
@@ -46,7 +46,7 @@ public class RedisRateLimiter implements RateLimiter {
|
||||
List<String> keys = Arrays.asList(prefix + ".tokens", prefix + ".timestamp");
|
||||
|
||||
// The arguments to the LUA script. time() returns unixtime in seconds.
|
||||
String[] args = new String[]{ replenishRate+"", capacity+"", Instant.now().getEpochSecond()+"", "1"};
|
||||
String[] args = new String[]{ replenishRate+"", burstCapacity +"", Instant.now().getEpochSecond()+"", "1"};
|
||||
// allowed, tokens_left = redis.eval(SCRIPT, keys, args)
|
||||
List results = this.redisTemplate.execute(this.script, keys, args);
|
||||
|
||||
|
||||
@@ -34,26 +34,26 @@ public class RedisRateLimiterTests extends BaseWebClientTests {
|
||||
String id = UUID.randomUUID().toString();
|
||||
|
||||
int replenishRate = 10;
|
||||
int capacity = 2 * replenishRate;
|
||||
int burstCapacity = 2 * replenishRate;
|
||||
|
||||
// Bursts work
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
Response response = rateLimiter.isAllowed(id, replenishRate, capacity);
|
||||
for (int i = 0; i < burstCapacity; i++) {
|
||||
Response response = rateLimiter.isAllowed(id, replenishRate, burstCapacity);
|
||||
assertThat(response.isAllowed()).as("Burst # %s is allowed", i).isTrue();
|
||||
}
|
||||
|
||||
Response response = rateLimiter.isAllowed(id, replenishRate, capacity);
|
||||
assertThat(response.isAllowed()).as("Burst # %s is not allowed", capacity).isFalse();
|
||||
Response response = rateLimiter.isAllowed(id, replenishRate, burstCapacity);
|
||||
assertThat(response.isAllowed()).as("Burst # %s is not allowed", burstCapacity).isFalse();
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
// # After the burst is done, check the steady state
|
||||
for (int i = 0; i < replenishRate; i++) {
|
||||
response = rateLimiter.isAllowed(id, replenishRate, capacity);
|
||||
response = rateLimiter.isAllowed(id, replenishRate, burstCapacity);
|
||||
assertThat(response.isAllowed()).as("steady state # %s is allowed", i).isTrue();
|
||||
}
|
||||
|
||||
response = rateLimiter.isAllowed(id, replenishRate, capacity);
|
||||
response = rateLimiter.isAllowed(id, replenishRate, burstCapacity);
|
||||
assertThat(response.isAllowed()).as("steady state # %s is allowed", replenishRate).isFalse();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user