Browse Source

Rename capacity param to burstCapacity

pull/41/head
Spencer Gibb 8 years ago
parent
commit
7672ea3d7d
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 2
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RateLimiter.java
  2. 6
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java
  3. 14
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java

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

6
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java

@ -30,13 +30,13 @@ public class RedisRateLimiter implements RateLimiter { @@ -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 { @@ -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);

14
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java

@ -34,26 +34,26 @@ public class RedisRateLimiterTests extends BaseWebClientTests { @@ -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();
}

Loading…
Cancel
Save