Simplify Eureka HTTP client synchronization

Signed-off-by: Prahlad Bhakat <prahladbhakat05@gmail.com>
This commit is contained in:
Prahlad Bhakat
2026-09-09 15:25:05 +05:30
parent b2d3f8225b
commit 8a1569162b
@@ -55,9 +55,7 @@ public class DefaultEurekaClientHttpRequestFactorySupplier implements EurekaClie
private final Set<RequestConfigCustomizer> requestConfigCustomizers;
private final Object lock = new Object();
private volatile CloseableHttpClient sharedHttpClient;
private CloseableHttpClient sharedHttpClient;
public DefaultEurekaClientHttpRequestFactorySupplier(TimeoutProperties timeoutProperties,
Set<RequestConfigCustomizer> requestConfigCustomizers) {
@@ -66,20 +64,18 @@ public class DefaultEurekaClientHttpRequestFactorySupplier implements EurekaClie
}
@Override
public ClientHttpRequestFactory get(SSLContext sslContext, @Nullable HostnameVerifier hostnameVerifier) {
CloseableHttpClient httpClient;
synchronized (this.lock) {
httpClient = this.sharedHttpClient;
if (httpClient == null) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (sslContext != null || hostnameVerifier != null || timeoutProperties != null) {
httpClientBuilder
.setConnectionManager(buildConnectionManager(sslContext, hostnameVerifier, timeoutProperties));
}
httpClientBuilder.setDefaultRequestConfig(buildRequestConfig());
httpClient = httpClientBuilder.build();
this.sharedHttpClient = httpClient;
public synchronized ClientHttpRequestFactory get(SSLContext sslContext,
@Nullable HostnameVerifier hostnameVerifier) {
CloseableHttpClient httpClient = this.sharedHttpClient;
if (httpClient == null) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (sslContext != null || hostnameVerifier != null || timeoutProperties != null) {
httpClientBuilder
.setConnectionManager(buildConnectionManager(sslContext, hostnameVerifier, timeoutProperties));
}
httpClientBuilder.setDefaultRequestConfig(buildRequestConfig());
httpClient = httpClientBuilder.build();
this.sharedHttpClient = httpClient;
}
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
@@ -87,17 +83,15 @@ public class DefaultEurekaClientHttpRequestFactorySupplier implements EurekaClie
}
@Override
public void close() {
synchronized (this.lock) {
CloseableHttpClient httpClient = this.sharedHttpClient;
this.sharedHttpClient = null;
if (httpClient != null) {
try {
httpClient.close();
}
catch (IOException ex) {
// best-effort close during shutdown; nothing actionable if it fails
}
public synchronized void close() {
CloseableHttpClient httpClient = this.sharedHttpClient;
this.sharedHttpClient = null;
if (httpClient != null) {
try {
httpClient.close();
}
catch (IOException ex) {
// best-effort close during shutdown; nothing actionable if it fails
}
}
}