Skip to content

Commit

Permalink
Merge branch 'wangyuheng-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Jan 16, 2019
2 parents d9649dc + f8faee2 commit 88a5d5f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -138,11 +139,7 @@ public Mono<Response> isAllowed(String routeId, String id) {
throw new IllegalStateException("RedisRateLimiter is not initialized");
}

Config routeConfig = getConfig().getOrDefault(routeId, defaultConfig);

if (routeConfig == null) {
throw new IllegalArgumentException("No Configuration found for route " + routeId);
}
Config routeConfig = loadConfiguration(routeId);

// How many requests per second do you want a user to be allowed to do?
int replenishRate = routeConfig.getReplenishRate();
Expand Down Expand Up @@ -187,6 +184,19 @@ public Mono<Response> isAllowed(String routeId, String id) {
return Mono.just(new Response(true, getHeaders(routeConfig, -1L)));
}

/* for testing */ Config loadConfiguration(String routeId) {
Config routeConfig = getConfig().getOrDefault(routeId, defaultConfig);

if (routeConfig == null) {
routeConfig = getConfig().get(RouteDefinitionRouteLocator.DEFAULT_FILTERS);
}

if (routeConfig == null) {
throw new IllegalArgumentException("No Configuration found for route " + routeId +" or defaultFilters");
}
return routeConfig;
}

@NotNull
public HashMap<String, String> getHeaders(Config config, Long tokensLeft) {
HashMap<String, String> headers = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
public class RouteDefinitionRouteLocator implements RouteLocator, BeanFactoryAware, ApplicationEventPublisherAware {
protected final Log logger = LogFactory.getLog(getClass());

public static final String DEFAULT_FILTERS = "defaultFilters";
private final RouteDefinitionLocator routeDefinitionLocator;
private final ConversionService conversionService;
private final Map<String, RoutePredicateFactory> predicates = new LinkedHashMap<>();
Expand Down Expand Up @@ -184,7 +185,7 @@ private List<GatewayFilter> getFilters(RouteDefinition routeDefinition) {

//TODO: support option to apply defaults after route specific filters?
if (!this.gatewayProperties.getDefaultFilters().isEmpty()) {
filters.addAll(loadGatewayFilters("defaultFilters",
filters.addAll(loadGatewayFilters(DEFAULT_FILTERS,
this.gatewayProperties.getDefaultFilters()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.springframework.cloud.gateway.filter.ratelimit;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Spencer Gibb
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
@ActiveProfiles("redis-rate-limiter-default-config")
public class RedisRateLimiterDefaultFilterConfigTests {

@Autowired
private RedisRateLimiter rateLimiter;

@Autowired
private RouteLocator routeLocator;

@Before
public void init() {
routeLocator.getRoutes().collectList().block(); // prime routes since getRoutes() no longer blocks
}

@Test
public void redisRateConfiguredFromEnvironmentDefaultFilters() {
String routeId = "redis_rate_limiter_config_default_test";
RedisRateLimiter.Config config = rateLimiter.loadConfiguration(routeId);
assertConfigAndRoute(routeId, 70, 80, config);
}

private void assertConfigAndRoute(String key, int replenishRate, int burstCapacity, RedisRateLimiter.Config config) {
assertThat(config).isNotNull();
assertThat(config.getReplenishRate()).isEqualTo(replenishRate);
assertThat(config.getBurstCapacity()).isEqualTo(burstCapacity);

Route route = routeLocator.getRoutes().filter(r -> r.getId().equals(key)).next().block();
assertThat(route).isNotNull();
assertThat(route.getFilters()).isNotEmpty();
}

@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response;
import org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.cloud.gateway.test.support.redis.RedisRule;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void redisRateLimiterWorks() throws Exception {

Thread.sleep(1000);

// # After the burst is done, check the steady state
// # After the burst is done, check the steady state
for (int i = 0; i < replenishRate; i++) {
response = rateLimiter.isAllowed(routeId, id).block();
assertThat(response.isAllowed()).as("steady state # %s is allowed", i).isTrue();
Expand All @@ -80,7 +81,7 @@ public void redisRateLimiterWorks() throws Exception {
response = rateLimiter.isAllowed(routeId, id).block();
assertThat(response.isAllowed()).as("steady state # %s is allowed", replenishRate).isFalse();
}

@Test
public void keysUseRedisKeyHashTags() {
assertThat(RedisRateLimiter.getKeys("1"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test:
uri: lb://myservice

spring:
cloud:
gateway:
default-filters:
- name: RequestRateLimiter
args:
redis-rate-limiter:
replenish-rate: 70
burst-capacity: 80
routes:
# =====================================
- id: redis_rate_limiter_config_default_test
uri: ${test.uri}
predicates:
- Path=/default

0 comments on commit 88a5d5f

Please sign in to comment.