Polish "Add socketKeepAlive configuration property for Elasticsearch"

See gh-32051
This commit is contained in:
Stephane Nicoll 2022-08-17 08:22:24 +02:00
parent 122d40a110
commit d6f6bcb770
3 changed files with 22 additions and 23 deletions

View File

@ -57,16 +57,16 @@ public class ElasticsearchProperties {
*/
private Duration socketTimeout = Duration.ofSeconds(30);
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
private String pathPrefix;
/**
* Whether to enable socket keep alive between client and Elasticsearch.
*/
private boolean socketKeepAlive = false;
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
private String pathPrefix;
private final Restclient restclient = new Restclient();
public List<String> getUris() {
@ -109,14 +109,6 @@ public class ElasticsearchProperties {
this.socketTimeout = socketTimeout;
}
public String getPathPrefix() {
return this.pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
public boolean isSocketKeepAlive() {
return this.socketKeepAlive;
}
@ -125,6 +117,14 @@ public class ElasticsearchProperties {
this.socketKeepAlive = socketKeepAlive;
}
public String getPathPrefix() {
return this.pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
public Restclient getRestclient() {
return this.restclient;
}

View File

@ -156,8 +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()));
map.from(this.properties::isSocketKeepAlive).to((keepAlive) -> builder
.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepAlive).build()));
}
@Override

View File

@ -184,20 +184,19 @@ class ElasticsearchRestClientAutoConfigurationTests {
}
@Test
void socketKeepAliveDefaults() {
void configureWithNoSocketKeepAliveApplyDefault() {
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);
}
);
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