From 5cf466cd2adf3c31e2921bb07feac6552f6ba045 Mon Sep 17 00:00:00 2001 From: Tony Clarke <36965832+tony-clarke-amdocs@users.noreply.github.com> Date: Fri, 24 Aug 2018 16:12:08 -0400 Subject: [PATCH 1/3] responseMono timeout is a noop since the return Mono is ignored (#515) --- .../cloud/gateway/filter/NettyRoutingFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java index 27be9346..5f3d52d1 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java @@ -117,7 +117,7 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered { }); if (properties.getResponseTimeout() != null) { - responseMono.timeout(properties.getResponseTimeout(), + responseMono = responseMono.timeout(properties.getResponseTimeout(), Mono.error(new TimeoutException("Response took longer than timeout: " + properties.getResponseTimeout()))); } From 6e54ed23afa778fd3ccf6bd2a861d94af1cd44f5 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 24 Aug 2018 16:41:23 -0400 Subject: [PATCH 2/3] Moves Evaluation Context as field and changes if predicate is true to not use spel. --- ...DiscoveryClientRouteDefinitionLocator.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java index 2879c5ab..7c72e644 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java @@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.discovery; import java.net.URI; import java.util.Map; +import java.util.function.Predicate; import reactor.core.publisher.Flux; @@ -35,7 +36,6 @@ import org.springframework.expression.spel.support.SimpleEvaluationContext; import org.springframework.util.StringUtils; /** - * TODO: developer configuration, in zuul, this was opt out, should be opt in * TODO: change to RouteLocator? use java dsl * @author Spencer Gibb */ @@ -44,6 +44,7 @@ public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLoc private final DiscoveryClient discoveryClient; private final DiscoveryLocatorProperties properties; private final String routeIdPrefix; + private final SimpleEvaluationContext evalCtxt; public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { this.discoveryClient = discoveryClient; @@ -53,30 +54,37 @@ public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLoc } else { this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_"; } + evalCtxt = SimpleEvaluationContext + .forReadOnlyDataBinding() + .withInstanceMethods() + .build(); } @Override public Flux getRouteDefinitions() { - SimpleEvaluationContext evalCtxt = SimpleEvaluationContext - .forReadOnlyDataBinding() - .withInstanceMethods() - .build(); SpelExpressionParser parser = new SpelExpressionParser(); Expression includeExpr = parser.parseExpression(properties.getIncludeExpression()); Expression urlExpr = parser.parseExpression(properties.getUrlExpression()); + Predicate includePredicate; + if (properties.getIncludeExpression() == null || "true".equalsIgnoreCase(properties.getIncludeExpression())) { + includePredicate = instance -> true; + } else { + includePredicate = instance -> { + Boolean include = includeExpr.getValue(evalCtxt, instance, Boolean.class); + if (include == null) { + return false; + } + return include; + }; + } + return Flux.fromIterable(discoveryClient.getServices()) .map(discoveryClient::getInstances) .filter(instances -> !instances.isEmpty()) .map(instances -> instances.get(0)) - .filter(instance -> { - Boolean include = includeExpr.getValue(evalCtxt, instance, Boolean.class); - if (include == null) { - return false; - } - return include; - }) + .filter(includePredicate) .map(instance -> { String serviceId = instance.getServiceId(); From cba999a8b6296e2d914ccb6ee59c3c84a6c9ccf4 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 24 Aug 2018 16:41:58 -0400 Subject: [PATCH 3/3] Adds response timeout test fixes gh-518 --- .../NettyRoutingFilterIntegrationTests.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterIntegrationTests.java diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterIntegrationTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterIntegrationTests.java new file mode 100644 index 00000000..aefd24ae --- /dev/null +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterIntegrationTests.java @@ -0,0 +1,61 @@ +/* + * 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; + +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.gateway.test.BaseWebClientTests; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "spring.cloud.gateway.httpclient.response-timeout=3s", webEnvironment = RANDOM_PORT) +@DirtiesContext +@SuppressWarnings("unchecked") +public class NettyRoutingFilterIntegrationTests extends BaseWebClientTests { + + @Test + public void responseTimeoutWorks() { + testClient.get() + .uri("/delay/5") + .exchange() + .expectStatus().is5xxServerError() + .expectBody(Map.class) + .consumeWith(result -> { + Map body = result.getResponseBody(); + assertThat(body).containsEntry("message", "Response took longer than timeout: PT3S"); + }); + } + + @EnableAutoConfiguration + @SpringBootConfiguration + @Import(DefaultTestConfig.class) + public static class TestConfig { + } + +}