Adds RedisRateLimiter.args convenience method

This commit is contained in:
Spencer Gibb
2017-11-01 11:01:28 -04:00
parent 53d7d3526c
commit 593aac8f40
3 changed files with 9 additions and 8 deletions

View File

@@ -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

View File

@@ -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)));

View File

@@ -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++) {