Skip to content

Commit 122d40a

Browse files
puppylpgsnicoll
authored andcommitted
Add socketKeepAlive configuration property for Elasticsearch
See gh-32051
1 parent f580123 commit 122d40a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-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
@@ -62,6 +62,11 @@ public class ElasticsearchProperties {
6262
*/
6363
private String pathPrefix;
6464

65+
/**
66+
* Whether to enable socket keep alive between client and Elasticsearch.
67+
*/
68+
private boolean socketKeepAlive = false;
69+
6570
private final Restclient restclient = new Restclient();
6671

6772
public List<String> getUris() {
@@ -112,6 +117,14 @@ public void setPathPrefix(String pathPrefix) {
112117
this.pathPrefix = pathPrefix;
113118
}
114119

120+
public boolean isSocketKeepAlive() {
121+
return this.socketKeepAlive;
122+
}
123+
124+
public void setSocketKeepAlive(boolean socketKeepAlive) {
125+
this.socketKeepAlive = socketKeepAlive;
126+
}
127+
115128
public Restclient getRestclient() {
116129
return this.restclient;
117130
}

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).whenTrue()
160+
.to(keepalive -> builder.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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ void configureWithCustomPathPrefix() {
183183
});
184184
}
185185

186+
@Test
187+
void socketKeepAliveDefaults() {
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(
195+
context -> {
196+
assertThat(context).hasSingleBean(RestClient.class);
197+
RestClient client = context.getBean(RestClient.class);
198+
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE);
199+
}
200+
);
201+
}
202+
186203
@Test
187204
void configureWithoutSnifferLibraryShouldNotCreateSniffer() {
188205
this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))

0 commit comments

Comments
 (0)