Skip to content

Commit

Permalink
Revert "Enable Connection Evictor in HttpClient (#4103)"
Browse files Browse the repository at this point in the history
This reverts commit c071e6b.
  • Loading branch information
OlgaMaciaszek committed Apr 15, 2024
1 parent dbb36fb commit 79a2eb8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 65 deletions.
11 changes: 0 additions & 11 deletions docs/modules/ROOT/pages/spring-cloud-netflix.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,6 @@ 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-2024 the original author or authors.
* Copyright 2013-2022 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,7 +30,6 @@
* {@link RestTemplateEurekaHttpClient}.
*
* @author Jiwon Jeon
* @author Armin Krezovic
* @since 3.1.6
*/
@ConfigurationProperties("eureka.client.rest-template-timeout")
Expand All @@ -46,12 +45,6 @@ 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;

public int getConnectTimeout() {
return connectTimeout;
}
Expand All @@ -76,14 +69,6 @@ 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 @@ -96,18 +81,18 @@ public boolean equals(Object o) {
RestTemplateTimeoutProperties that = (RestTemplateTimeoutProperties) o;

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

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

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2022 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,7 +17,6 @@
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 @@ -30,10 +29,8 @@
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 @@ -46,13 +43,9 @@
* @author Marcin Grzejszczak
* @author Olga Maciaszek-Sharma
* @author Jiwon Jeon
* @author Armin Krezovic
* @since 3.0.0
*/
public class DefaultEurekaClientHttpRequestFactorySupplier
implements EurekaClientHttpRequestFactorySupplier, DisposableBean {

private final AtomicReference<CloseableHttpClient> ref = new AtomicReference<>();
public class DefaultEurekaClientHttpRequestFactorySupplier implements EurekaClientHttpRequestFactorySupplier {

private final RestTemplateTimeoutProperties restTemplateTimeoutProperties;

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

@Override
public ClientHttpRequestFactory get(SSLContext sslContext, @Nullable HostnameVerifier hostnameVerifier) {
TimeValue timeValue;

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

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

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

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

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

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

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

}

0 comments on commit 79a2eb8

Please sign in to comment.