Merge pull request #3679 from qnnn/refreshable-connectTimeout

Support for reloading httpClient connectTimeout Configuration
This commit is contained in:
Ryan Baxter
2025-04-23 09:35:48 -04:00
committed by GitHub
2 changed files with 45 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2025 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.
@@ -254,7 +254,8 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered {
* @return the configured HttpClient.
*/
protected HttpClient getHttpClient(Route route, ServerWebExchange exchange) {
Object connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR);
Object connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR) != null
? route.getMetadata().get(CONNECT_TIMEOUT_ATTR) : properties.getConnectTimeout();
if (connectTimeoutAttr != null) {
Integer connectTimeout = getInteger(connectTimeoutAttr);
return this.httpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2025 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.
@@ -16,18 +16,26 @@
package org.springframework.cloud.gateway.filter;
import java.util.Collections;
import java.util.HashMap;
import io.netty.channel.ChannelOption;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.netty.DisposableServer;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientConfig;
import reactor.netty.http.server.HttpServer;
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.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
@@ -35,6 +43,8 @@ import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.util.TestSocketUtils;
import org.springframework.test.web.reactive.server.WebTestClient;
@@ -48,6 +58,12 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
@Autowired
private ApplicationContext context;
@Autowired
private NettyRoutingFilter nettyRoutingFilter;
@Autowired
private RouteLocator routeLocator;
@BeforeAll
public static void beforeAll() {
port = TestSocketUtils.findAvailableTcpPort();
@@ -88,6 +104,30 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
}
}
@Test
void testConnectTimeoutConfigurationReloadWorks() {
ConfigurableEnvironment environment = (ConfigurableEnvironment) this.context.getEnvironment();
HashMap<String, Object> map = new HashMap<>();
environment.getPropertySources().addFirst(new MapPropertySource("mock", map));
map.put("spring.cloud.gateway.httpclient.connect-timeout", "1000");
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
Route route = routeLocator.getRoutes()
.filter(r -> r.getId().equals("refreshable_configuration_test")).blockLast();
assertThat(route).isNotNull();
HttpClient httpClient = nettyRoutingFilter.getHttpClient(route, null);
HttpClientConfig configuration = httpClient.configuration();
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(1000);
map.put("spring.cloud.gateway.httpclient.connect-timeout", "5000");
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
httpClient = nettyRoutingFilter.getHttpClient(route, null);
configuration = httpClient.configuration();
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(5000);
environment.getPropertySources().remove("mock");
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(PermitAllSecurityConfiguration.class)
@@ -98,6 +138,7 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
return builder.routes()
.route(p -> p.path("/mockexample").filters(f -> f.prefixPath("/httpbin")).uri("http://example.com"))
.route(p -> p.path("/issue").uri("HTTP://127.0.0.1:" + port))
.route("refreshable_configuration_test", p -> p.path("/refresh").uri("http://example.com"))
.build();
}