mirror of
https://github.com/spring-cloud/spring-cloud-netflix.git
synced 2026-09-17 07:43:44 +00:00
Simplify Eureka HTTP client synchronization
Signed-off-by: Prahlad Bhakat <prahladbhakat05@gmail.com>
This commit is contained in:
+21
-27
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user