Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for reloading httpClient connectTimeout Configuration #3679

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -254,7 +254,8 @@ protected Mono<HttpClient> getHttpClientMono(Route route, ServerWebExchange exch
* @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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,25 +16,35 @@

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;
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;

Expand All @@ -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();
Expand Down Expand Up @@ -88,6 +104,30 @@ void testCaseInsensitiveScheme() {
}
}

@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)
Expand All @@ -98,6 +138,7 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
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();
}

Expand Down
Loading