use of org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response in project spring-cloud-gateway by spring-cloud.
the class RedisRateLimiterTests method redisRateLimiterWorks.
@Test
public void redisRateLimiterWorks() throws Exception {
assumeThat("Ignore on Circle", System.getenv("CIRCLECI"), is(nullValue()));
String id = UUID.randomUUID().toString();
int replenishRate = 10;
int burstCapacity = 2 * replenishRate;
String routeId = "myroute";
rateLimiter.getConfig().put(routeId, new RedisRateLimiter.Config().setBurstCapacity(burstCapacity).setReplenishRate(replenishRate));
// Bursts work
for (int i = 0; i < burstCapacity; i++) {
Response response = rateLimiter.isAllowed(routeId, id).block();
assertThat(response.isAllowed()).as("Burst # %s is allowed", i).isTrue();
}
Response response = rateLimiter.isAllowed(routeId, id).block();
if (response.isAllowed()) {
// TODO: sometimes there is an off by one error
response = rateLimiter.isAllowed(routeId, id).block();
}
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(routeId, id).block();
assertThat(response.isAllowed()).as("steady state # %s is allowed", i).isTrue();
}
response = rateLimiter.isAllowed(routeId, id).block();
assertThat(response.isAllowed()).as("steady state # %s is allowed", replenishRate).isFalse();
}
use of org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response in project spring-cloud-gateway by spring-cloud.
the class RequestRateLimiterGatewayFilterFactoryTests method assertFilterFactory.
private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) {
Tuple args = tuple().build();
when(rateLimiter.isAllowed("myroute", key)).thenReturn(Mono.just(new Response(allowed, 1)));
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
exchange.getResponse().setStatusCode(HttpStatus.OK);
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, Route.builder().id("myroute").predicate(ex -> true).uri("http://localhost").build());
when(this.filterChain.filter(exchange)).thenReturn(Mono.empty());
RequestRateLimiterGatewayFilterFactory factory = this.context.getBean(RequestRateLimiterGatewayFilterFactory.class);
GatewayFilter filter = factory.apply(config -> config.setKeyResolver(keyResolver));
Mono<Void> response = filter.filter(exchange, this.filterChain);
response.subscribe(aVoid -> assertThat(exchange.getResponse().getStatusCode()).isEqualTo(expectedStatus));
}
Aggregations