diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RateLimiter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RateLimiter.java index 9a5644ed..cd85b1d8 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RateLimiter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RateLimiter.java @@ -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; diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java index 03b83949..27bb2b32 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java @@ -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 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); diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java index b3512d67..d58717ba 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java @@ -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(); }