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

Enable Connection Evictor in HttpClient (#4103) #4258

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
11 changes: 11 additions & 0 deletions docs/modules/ROOT/pages/spring-cloud-netflix.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ eureka:
socket-timeout: 10000
----

When using the default Apache HTTP Client Request Factory, an additional parameter can be used to configure timeout for evicting idle connections. Default value is 30 seconds. Value must be specified in milliseconds.

.application.yml
[source,yaml]
----
eureka:
client:
rest-template-timeout:
idle-timeout: 30000
----

=== Status Page and Health Indicator

The status page and health indicators for a Eureka instance default to `/info` and `/health` respectively, which are the default locations of useful endpoints in a Spring Boot Actuator application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 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 @@ -30,6 +30,7 @@
* {@link RestTemplateEurekaHttpClient}.
*
* @author Jiwon Jeon
* @author Armin Krezovic
* @since 3.1.6
*/
@ConfigurationProperties("eureka.client.rest-template-timeout")
Expand All @@ -45,6 +46,12 @@ public class RestTemplateTimeoutProperties {

private int socketTimeout = 3 * 60 * 1000;

/**
* Indicates how long a connection can be idle before it is evicted from the
* connection pool.
*/
private long idleTimeout = 30 * 60 * 1000L;
ZIRAKrezovic marked this conversation as resolved.
Show resolved Hide resolved

public int getConnectTimeout() {
return connectTimeout;
}
Expand All @@ -69,6 +76,14 @@ public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}

public long getIdleTimeout() {
return idleTimeout;
}

public void setIdleTimeout(long idleTimeout) {
this.idleTimeout = idleTimeout;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -81,18 +96,18 @@ public boolean equals(Object o) {
RestTemplateTimeoutProperties that = (RestTemplateTimeoutProperties) o;

return connectTimeout == that.connectTimeout && connectRequestTimeout == that.connectRequestTimeout
&& socketTimeout == that.socketTimeout;
&& socketTimeout == that.socketTimeout && idleTimeout == that.idleTimeout;
}

@Override
public int hashCode() {
return Objects.hash(connectTimeout, connectRequestTimeout, socketTimeout);
return Objects.hash(connectTimeout, connectRequestTimeout, socketTimeout, idleTimeout);
}

@Override
public String toString() {
return "RestTemplateTimeoutProperties{" + ", connectTimeout=" + connectTimeout + ", connectRequestTimeout="
+ connectRequestTimeout + ", socketTimeout=" + socketTimeout + '}';
+ connectRequestTimeout + ", socketTimeout=" + socketTimeout + ", idleTimeout=" + idleTimeout + '}';
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 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 @@ -17,6 +17,7 @@
package org.springframework.cloud.netflix.eureka.http;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
Expand All @@ -29,8 +30,10 @@
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.cloud.netflix.eureka.RestTemplateTimeoutProperties;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Expand All @@ -43,9 +46,13 @@
* @author Marcin Grzejszczak
* @author Olga Maciaszek-Sharma
* @author Jiwon Jeon
* @author Armin Krezovic
* @since 3.0.0
*/
public class DefaultEurekaClientHttpRequestFactorySupplier implements EurekaClientHttpRequestFactorySupplier {
public class DefaultEurekaClientHttpRequestFactorySupplier
ZIRAKrezovic marked this conversation as resolved.
Show resolved Hide resolved
implements EurekaClientHttpRequestFactorySupplier, DisposableBean {

private final AtomicReference<CloseableHttpClient> ref = new AtomicReference<>();

private final RestTemplateTimeoutProperties restTemplateTimeoutProperties;

Expand All @@ -64,7 +71,18 @@ public DefaultEurekaClientHttpRequestFactorySupplier(RestTemplateTimeoutProperti

@Override
public ClientHttpRequestFactory get(SSLContext sslContext, @Nullable HostnameVerifier hostnameVerifier) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
TimeValue timeValue;

if (restTemplateTimeoutProperties != null) {
timeValue = TimeValue.ofMilliseconds(restTemplateTimeoutProperties.getIdleTimeout());
}
else {
timeValue = TimeValue.of(30, TimeUnit.SECONDS);
}

HttpClientBuilder httpClientBuilder = HttpClients.custom().evictExpiredConnections()
.evictIdleConnections(timeValue);

if (sslContext != null || hostnameVerifier != null || restTemplateTimeoutProperties != null) {
httpClientBuilder.setConnectionManager(
buildConnectionManager(sslContext, hostnameVerifier, restTemplateTimeoutProperties));
Expand All @@ -73,7 +91,11 @@ public ClientHttpRequestFactory get(SSLContext sslContext, @Nullable HostnameVer
httpClientBuilder.setDefaultRequestConfig(buildRequestConfig());
}

CloseableHttpClient httpClient = httpClientBuilder.build();
if (ref.get() == null) {
ref.compareAndSet(null, httpClientBuilder.build());
}

CloseableHttpClient httpClient = ref.get();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return requestFactory;
Expand Down Expand Up @@ -108,4 +130,13 @@ private RequestConfig buildRequestConfig() {
.build();
}

@Override
public void destroy() throws Exception {
CloseableHttpClient httpClient = ref.get();

if (httpClient != null) {
httpClient.close();
}
}

}