Skip to content

Commit bb07eec

Browse files
committed
Merge pull request #32051 from puppylpg
* pr/32051: Polish "Add socketKeepAlive configuration property for Elasticsearch" Add socketKeepAlive configuration property for Elasticsearch Closes gh-32051
2 parents f580123 + d6f6bcb commit bb07eec

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public class ElasticsearchProperties {
5757
*/
5858
private Duration socketTimeout = Duration.ofSeconds(30);
5959

60+
/**
61+
* Whether to enable socket keep alive between client and Elasticsearch.
62+
*/
63+
private boolean socketKeepAlive = false;
64+
6065
/**
6166
* Prefix added to the path of every request sent to Elasticsearch.
6267
*/
@@ -104,6 +109,14 @@ public void setSocketTimeout(Duration socketTimeout) {
104109
this.socketTimeout = socketTimeout;
105110
}
106111

112+
public boolean isSocketKeepAlive() {
113+
return this.socketKeepAlive;
114+
}
115+
116+
public void setSocketKeepAlive(boolean socketKeepAlive) {
117+
this.socketKeepAlive = socketKeepAlive;
118+
}
119+
107120
public String getPathPrefix() {
108121
return this.pathPrefix;
109122
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.http.client.config.RequestConfig;
2828
import org.apache.http.impl.client.BasicCredentialsProvider;
2929
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
30+
import org.apache.http.impl.nio.reactor.IOReactorConfig;
3031
import org.elasticsearch.client.RestClient;
3132
import org.elasticsearch.client.RestClientBuilder;
3233
import org.elasticsearch.client.sniff.Sniffer;
@@ -155,6 +156,8 @@ public void customize(RestClientBuilder builder) {
155156
@Override
156157
public void customize(HttpAsyncClientBuilder builder) {
157158
builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties));
159+
map.from(this.properties::isSocketKeepAlive).to((keepAlive) -> builder
160+
.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepAlive).build()));
158161
}
159162

160163
@Override

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ void configureWithCustomPathPrefix() {
183183
});
184184
}
185185

186+
@Test
187+
void configureWithNoSocketKeepAliveApplyDefault() {
188+
RestClient client = RestClient.builder(new HttpHost("localhost", 9201, "http")).build();
189+
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE);
190+
}
191+
192+
@Test
193+
void configureWithCustomSocketKeepAlive() {
194+
this.contextRunner.withPropertyValues("spring.elasticsearch.socket-keep-alive=true").run((context) -> {
195+
assertThat(context).hasSingleBean(RestClient.class);
196+
RestClient client = context.getBean(RestClient.class);
197+
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive")
198+
.isEqualTo(Boolean.TRUE);
199+
});
200+
}
201+
186202
@Test
187203
void configureWithoutSnifferLibraryShouldNotCreateSniffer() {
188204
this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))

0 commit comments

Comments
 (0)