From 10b55a0a13f2102275f02918af61613ab2cbfb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=AD=8F=2C=E5=B0=8F=E9=AD=8F=2C=E6=88=91?= =?UTF-8?q?=E4=BB=AC=E8=A6=81=E5=8E=BB=E5=93=AA=E9=87=8C=E5=91=80?= <30821411+galaxy-sea@users.noreply.github.com> Date: Tue, 17 Jan 2023 20:18:00 +0800 Subject: [PATCH] connectionRequestTimeout for httpClient Fixes gh-799 (#817) --- .../HttpClient5FeignConfiguration.java | 7 +++- .../support/FeignHttpClientProperties.java | 37 +++++++++++++++++++ .../FeignHttpClientPropertiesTests.java | 15 +++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java index 708cff08..6dc228c5 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java @@ -55,6 +55,7 @@ import org.springframework.context.annotation.Configuration; * Default configuration for {@link CloseableHttpClient}. * * @author Nguyen Ky Thanh + * @author changjin wei(魏昌进) */ @Configuration(proxyBeanMethods = false) @ConditionalOnMissingBean(CloseableHttpClient.class) @@ -90,7 +91,11 @@ public class HttpClient5FeignConfiguration { .setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout( Timeout.of(httpClientProperties.getConnectionTimeout(), TimeUnit.MILLISECONDS)) - .setRedirectsEnabled(httpClientProperties.isFollowRedirects()).build()) + .setRedirectsEnabled(httpClientProperties.isFollowRedirects()) + .setConnectionRequestTimeout( + Timeout.of(httpClientProperties.getHc5().getConnectionRequestTimeout(), + httpClientProperties.getHc5().getConnectionRequestTimeoutUnit())) + .build()) .build(); return httpClient5; } diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java index 2e63e54a..e34929b4 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java @@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Ryan Baxter * @author Nguyen Ky Thanh * @author Olga Maciaszek-Sharma + * @author changjin wei(魏昌进) */ @ConfigurationProperties(prefix = "feign.httpclient") public class FeignHttpClientProperties { @@ -199,6 +200,16 @@ public class FeignHttpClientProperties { */ public static final TimeUnit DEFAULT_SOCKET_TIMEOUT_UNIT = TimeUnit.SECONDS; + /** + * Default value for connection request timeout. + */ + public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 3; + + /** + * Default value for connection request timeout unit. + */ + public static final TimeUnit DEFAULT_CONNECTION_REQUEST_TIMEOUT_UNIT = TimeUnit.MINUTES; + /** * Pool concurrency policies. */ @@ -219,6 +230,16 @@ public class FeignHttpClientProperties { */ private TimeUnit socketTimeoutUnit = DEFAULT_SOCKET_TIMEOUT_UNIT; + /** + * Default value for connection request timeout. + */ + private int connectionRequestTimeout = DEFAULT_CONNECTION_REQUEST_TIMEOUT; + + /** + * Default value for connection request timeout unit. + */ + private TimeUnit connectionRequestTimeoutUnit = DEFAULT_CONNECTION_REQUEST_TIMEOUT_UNIT; + public PoolConcurrencyPolicy getPoolConcurrencyPolicy() { return poolConcurrencyPolicy; } @@ -251,6 +272,22 @@ public class FeignHttpClientProperties { this.socketTimeout = socketTimeout; } + public int getConnectionRequestTimeout() { + return connectionRequestTimeout; + } + + public void setConnectionRequestTimeout(int connectionRequestTimeout) { + this.connectionRequestTimeout = connectionRequestTimeout; + } + + public TimeUnit getConnectionRequestTimeoutUnit() { + return connectionRequestTimeoutUnit; + } + + public void setConnectionRequestTimeoutUnit(TimeUnit connectionRequestTimeoutUnit) { + this.connectionRequestTimeoutUnit = connectionRequestTimeoutUnit; + } + /** * Enumeration of pool concurrency policies. */ diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/FeignHttpClientPropertiesTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/FeignHttpClientPropertiesTests.java index cbff0103..b104254b 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/FeignHttpClientPropertiesTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/FeignHttpClientPropertiesTests.java @@ -32,12 +32,15 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.openfeign.support.FeignHttpClientProperties.Hc5Properties.DEFAULT_CONNECTION_REQUEST_TIMEOUT; +import static org.springframework.cloud.openfeign.support.FeignHttpClientProperties.Hc5Properties.DEFAULT_CONNECTION_REQUEST_TIMEOUT_UNIT; import static org.springframework.cloud.openfeign.support.FeignHttpClientProperties.Hc5Properties.DEFAULT_SOCKET_TIMEOUT; import static org.springframework.cloud.openfeign.support.FeignHttpClientProperties.Hc5Properties.DEFAULT_SOCKET_TIMEOUT_UNIT; /** * @author Ryan Baxter * @author Nguyen Ky Thanh + * @author changjin wei(魏昌进) */ @DirtiesContext class FeignHttpClientPropertiesTests { @@ -67,17 +70,23 @@ class FeignHttpClientPropertiesTests { assertThat(getProperties().getHc5().getPoolReusePolicy()).isEqualTo(PoolReusePolicy.FIFO); assertThat(getProperties().getHc5().getSocketTimeout()).isEqualTo(DEFAULT_SOCKET_TIMEOUT); assertThat(getProperties().getHc5().getSocketTimeoutUnit()).isEqualTo(DEFAULT_SOCKET_TIMEOUT_UNIT); + assertThat(getProperties().getHc5().getConnectionRequestTimeout()) + .isEqualTo(DEFAULT_CONNECTION_REQUEST_TIMEOUT); + assertThat(getProperties().getHc5().getConnectionRequestTimeoutUnit()) + .isEqualTo(DEFAULT_CONNECTION_REQUEST_TIMEOUT_UNIT); } @Test void testCustomization() { TestPropertyValues - .of("feign.httpclient.maxConnections=2", "feign.httpclient.connectionTimeout=2", + .of("feign.httpclient.maxConnections=2", "feign.httpclient.connectionTimeout=2", "feign.httpclient.maxConnectionsPerRoute=2", "feign.httpclient.timeToLive=2", "feign.httpclient.disableSslValidation=true", "feign.httpclient.followRedirects=false", "feign.httpclient.disableSslValidation=true", "feign.httpclient.followRedirects=false", "feign.httpclient.hc5.poolConcurrencyPolicy=lax", "feign.httpclient.hc5.poolReusePolicy=lifo", - "feign.httpclient.hc5.socketTimeout=200", "feign.httpclient.hc5.socketTimeoutUnit=milliseconds") + "feign.httpclient.hc5.socketTimeout=200", "feign.httpclient.hc5.socketTimeoutUnit=milliseconds", + "feign.httpclient.hc5.connectionRequestTimeout=200", + "feign.httpclient.hc5.connectionRequestTimeoutUnit=milliseconds") .applyTo(this.context); setupContext(); assertThat(getProperties().getMaxConnections()).isEqualTo(2); @@ -90,6 +99,8 @@ class FeignHttpClientPropertiesTests { assertThat(getProperties().getHc5().getPoolReusePolicy()).isEqualTo(PoolReusePolicy.LIFO); assertThat(getProperties().getHc5().getSocketTimeout()).isEqualTo(200); assertThat(getProperties().getHc5().getSocketTimeoutUnit()).isEqualTo(TimeUnit.MILLISECONDS); + assertThat(getProperties().getHc5().getConnectionRequestTimeout()).isEqualTo(200); + assertThat(getProperties().getHc5().getConnectionRequestTimeoutUnit()).isEqualTo(TimeUnit.MILLISECONDS); } private void setupContext() {