From 593aac8f4019bdb2a2633967080fb68a94b3a709 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 1 Nov 2017 11:01:28 -0400 Subject: [PATCH] Adds RedisRateLimiter.args convenience method --- .../cloud/gateway/filter/ratelimit/RedisRateLimiter.java | 8 +++++++- .../RequestRateLimiterGatewayFilterFactoryTests.java | 4 +--- .../gateway/filter/ratelimit/RedisRateLimiterTests.java | 5 +---- 3 files changed, 9 insertions(+), 8 deletions(-) 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 62d13714..299ba2b2 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 @@ -9,8 +9,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.script.RedisScript; - import org.springframework.tuple.Tuple; + +import static org.springframework.tuple.TupleBuilder.tuple; + import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -35,6 +37,10 @@ public class RedisRateLimiter implements RateLimiter { this.script = script; } + public static Tuple args(int replenishRate, int burstCapacity) { + return tuple().of(REPLENISH_RATE_KEY, replenishRate, BURST_CAPACITY_KEY, burstCapacity); + } + /** * 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 diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactoryTests.java index 949703bd..f9dd4871 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactoryTests.java @@ -68,10 +68,8 @@ public class RequestRateLimiterGatewayFilterFactoryTests extends BaseWebClientTe } private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) { - int replenishRate = 10; - int burstCapacity = 2 * replenishRate; - Tuple args = tuple().of(REPLENISH_RATE_KEY, replenishRate, BURST_CAPACITY_KEY, burstCapacity); + Tuple args = tuple().build(); when(rateLimiter.isAllowed(key, args)) .thenReturn(Mono.just(new Response(allowed, 1))); 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 9de79910..8a8c4732 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 @@ -17,9 +17,6 @@ import org.springframework.tuple.Tuple; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -import static org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter.BURST_CAPACITY_KEY; -import static org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter.REPLENISH_RATE_KEY; -import static org.springframework.tuple.TupleBuilder.tuple; /** * see https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L36-L62 @@ -40,7 +37,7 @@ public class RedisRateLimiterTests extends BaseWebClientTests { int replenishRate = 10; int burstCapacity = 2 * replenishRate; - Tuple args = tuple().of(REPLENISH_RATE_KEY, replenishRate, BURST_CAPACITY_KEY, burstCapacity); + Tuple args = RedisRateLimiter.args(replenishRate, burstCapacity); // Bursts work for (int i = 0; i < burstCapacity; i++) {