From 122d40a1102ae0fb7b46905266eafad39b6c8e6f Mon Sep 17 00:00:00 2001 From: puppylpg Date: Wed, 10 Aug 2022 16:31:26 +0800 Subject: [PATCH] Add socketKeepAlive configuration property for Elasticsearch See gh-32051 --- .../elasticsearch/ElasticsearchProperties.java | 13 +++++++++++++ .../ElasticsearchRestClientConfigurations.java | 3 +++ ...csearchRestClientAutoConfigurationTests.java | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index 38b0b0e4a2..da805fe3ca 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -62,6 +62,11 @@ public class ElasticsearchProperties { */ private String pathPrefix; + /** + * Whether to enable socket keep alive between client and Elasticsearch. + */ + private boolean socketKeepAlive = false; + private final Restclient restclient = new Restclient(); public List getUris() { @@ -112,6 +117,14 @@ public class ElasticsearchProperties { this.pathPrefix = pathPrefix; } + public boolean isSocketKeepAlive() { + return this.socketKeepAlive; + } + + public void setSocketKeepAlive(boolean socketKeepAlive) { + this.socketKeepAlive = socketKeepAlive; + } + public Restclient getRestclient() { return this.restclient; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java index c7b64d07c0..9cf91cfb0d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java @@ -27,6 +27,7 @@ import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; +import org.apache.http.impl.nio.reactor.IOReactorConfig; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.sniff.Sniffer; @@ -155,6 +156,8 @@ class ElasticsearchRestClientConfigurations { @Override public void customize(HttpAsyncClientBuilder builder) { builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties)); + map.from(this.properties::isSocketKeepAlive).whenTrue() + .to(keepalive -> builder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepalive).build())); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index 5343e11953..fa67f021ea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -183,6 +183,23 @@ class ElasticsearchRestClientAutoConfigurationTests { }); } + @Test + void socketKeepAliveDefaults() { + RestClient client = RestClient.builder(new HttpHost("localhost", 9201, "http")).build(); + assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE); + } + + @Test + void configureWithCustomSocketKeepAlive() { + this.contextRunner.withPropertyValues("spring.elasticsearch.socket-keep-alive=true").run( + context -> { + assertThat(context).hasSingleBean(RestClient.class); + RestClient client = context.getBean(RestClient.class); + assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE); + } + ); + } + @Test void configureWithoutSnifferLibraryShouldNotCreateSniffer() { this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))