Skip to content

Add socketKeepAlive configuration property for Elasticsearch #32051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getUris() {
Expand Down Expand Up @@ -112,6 +117,14 @@ public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}

public boolean isSocketKeepAlive() {
return this.socketKeepAlive;
}

public void setSocketKeepAlive(boolean socketKeepAlive) {
this.socketKeepAlive = socketKeepAlive;
}

public Restclient getRestclient() {
return this.restclient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -155,6 +156,8 @@ public void customize(RestClientBuilder builder) {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ void configureWithCustomPathPrefix() {
});
}

@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"))
Expand Down