Skip to content

Commit f7d876f

Browse files
committed
todos and timeouts fix
1 parent 08bd305 commit f7d876f

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected int capacityIncrement() {
5555

5656
@Override
5757
protected void data(final ByteBuffer src, final boolean endOfStream) throws ContentTooLongException {
58-
if (src.capacity() > limit) { //TODO why though? the server already has a 100MB limit. also, this method is called multiple times. check if should be src.capacity + buffer.length
58+
if (src.capacity() > limit) {
5959
throw new ContentTooLongException(
6060
"entity content is too long [" + src.capacity() + "] for the configured buffer limit [" + limit + "]"
6161
);

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,15 @@ public HttpAsyncClient getHttpClient() {
219219
/**
220220
* Replaces the nodes with which the client communicates.
221221
*/
222-
public synchronized void setNodes(Collection<Node> nodes) { // TODO why the whole method synchronized?
222+
public synchronized void setNodes(Collection<Node> nodes) {
223223
if (nodes == null || nodes.isEmpty()) {
224224
throw new IllegalArgumentException("node list must not be null or empty");
225225
}
226226

227227
Map<HttpHost, Node> nodesByHost = new LinkedHashMap<>();
228228
for (Node node : nodes) {
229229
Objects.requireNonNull(node, "node cannot be null");
230-
nodesByHost.put(node.getHost(), node); // TODO same host twice will be overwritten. should
231-
// throw exception?
230+
nodesByHost.put(node.getHost(), node);
232231
}
233232
this.nodes = new ArrayList<>(nodesByHost.values());
234233
this.blacklist.clear();

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5ClientBuilder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package co.elastic.clients.transport.rest5_client.low_level;
2121

2222

23+
import org.apache.hc.client5.http.config.ConnectionConfig;
2324
import org.apache.hc.client5.http.config.RequestConfig;
2425
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
2526
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
@@ -55,6 +56,7 @@
5556
public final class Rest5ClientBuilder {
5657
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 1000;
5758
public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 30000;
59+
public static final int DEFAULT_RESPONSE_TIMEOUT_MILLIS = 0; // meaning infinite
5860
public static final int DEFAULT_MAX_CONN_PER_ROUTE = 10;
5961
public static final int DEFAULT_MAX_CONN_TOTAL = 30;
6062

@@ -343,24 +345,30 @@ private CloseableHttpAsyncClient createHttpClient() {
343345
return this.httpClient;
344346
}
345347
// otherwise, creating a default instance of CloseableHttpAsyncClient
346-
// default timeouts are all infinite
347-
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
348+
// default timeouts are all 3 mins
349+
RequestConfig requestConfigBuilder = RequestConfig.custom()
348350
.setConnectionRequestTimeout(Timeout.of(DEFAULT_SOCKET_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
349-
.setConnectTimeout(Timeout.of(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); //TODO deprecated need to change
351+
.setResponseTimeout(Timeout.of(DEFAULT_RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
352+
.build();
350353

351354
try {
352355

353356
SSLContext sslContext = this.sslContext != null ? this.sslContext : SSLContext.getDefault();
354357

358+
ConnectionConfig connectionConfig = ConnectionConfig.custom()
359+
.setConnectTimeout(Timeout.of(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
360+
.build();
361+
355362
PoolingAsyncClientConnectionManager defaultConnectionManager =
356363
PoolingAsyncClientConnectionManagerBuilder.create()
364+
.setDefaultConnectionConfig(connectionConfig)
357365
.setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE)
358366
.setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)
359367
.setTlsStrategy(new BasicClientTlsStrategy(sslContext))
360368
.build();
361369

362370
HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create()
363-
.setDefaultRequestConfig(requestConfigBuilder.build())
371+
.setDefaultRequestConfig(requestConfigBuilder)
364372
.setConnectionManager(defaultConnectionManager)
365373
.setUserAgent(USER_AGENT_HEADER_VALUE)
366374
.setTargetAuthenticationStrategy(new DefaultAuthenticationStrategy())

0 commit comments

Comments
 (0)