Browse Source

Merge remote-tracking branch 'origin/2.0.x'

pull/824/head
Ryan Baxter 6 years ago
parent
commit
fec80f3f2b
  1. 16
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java
  2. 20
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java

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

@ -21,6 +21,7 @@ import java.util.ArrayList; @@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.validation.constraints.Min;
@ -46,6 +47,7 @@ import org.springframework.validation.annotation.Validated; @@ -46,6 +47,7 @@ import org.springframework.validation.annotation.Validated;
* https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L11-L34.
*
* @author Spencer Gibb
* @author Ronny Bräunlich
*/
@ConfigurationProperties("spring.cloud.gateway.redis-rate-limiter")
public class RedisRateLimiter extends AbstractRateLimiter<RedisRateLimiter.Config>
@ -266,11 +268,15 @@ public class RedisRateLimiter extends AbstractRateLimiter<RedisRateLimiter.Confi @@ -266,11 +268,15 @@ public class RedisRateLimiter extends AbstractRateLimiter<RedisRateLimiter.Confi
}
@NotNull
public HashMap<String, String> getHeaders(Config config, Long tokensLeft) {
HashMap<String, String> headers = new HashMap<>();
headers.put(this.remainingHeader, tokensLeft.toString());
headers.put(this.replenishRateHeader, String.valueOf(config.getReplenishRate()));
headers.put(this.burstCapacityHeader, String.valueOf(config.getBurstCapacity()));
public Map<String, String> getHeaders(Config config, Long tokensLeft) {
Map<String, String> headers = new HashMap<>();
if (isIncludeHeaders()) {
headers.put(this.remainingHeader, tokensLeft.toString());
headers.put(this.replenishRateHeader,
String.valueOf(config.getReplenishRate()));
headers.put(this.burstCapacityHeader,
String.valueOf(config.getBurstCapacity()));
}
return headers;
}

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

@ -44,6 +44,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @@ -44,6 +44,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L36-L62
*
* @author Spencer Gibb
* @author Ronny Bräunlich
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ -110,6 +111,25 @@ public class RedisRateLimiterTests extends BaseWebClientTests { @@ -110,6 +111,25 @@ public class RedisRateLimiterTests extends BaseWebClientTests {
"request_rate_limiter.{1}.tokens", "request_rate_limiter.{1}.timestamp");
}
@Test
public void redisRateLimiterDoesNotSendHeadersIfDeactivated() throws Exception {
assumeThat("Ignore on Circle", System.getenv("CIRCLECI"), is(nullValue()));
String id = UUID.randomUUID().toString();
String routeId = "myroute";
rateLimiter.setIncludeHeaders(false);
Response response = rateLimiter.isAllowed(routeId, id).block();
assertThat(response.isAllowed()).isTrue();
assertThat(response.getHeaders())
.doesNotContainKey(RedisRateLimiter.REMAINING_HEADER);
assertThat(response.getHeaders())
.doesNotContainKey(RedisRateLimiter.REPLENISH_RATE_HEADER);
assertThat(response.getHeaders())
.doesNotContainKey(RedisRateLimiter.BURST_CAPACITY_HEADER);
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(BaseWebClientTests.DefaultTestConfig.class)

Loading…
Cancel
Save