Skip to content

Commit 15b23a9

Browse files
committed
use ConnectionConfig; other cleanup
1 parent 56e09a1 commit 15b23a9

File tree

1 file changed

+20
-11
lines changed
  • docs/reference/transport/rest5-client/config

1 file changed

+20
-11
lines changed

docs/reference/transport/rest5-client/config/timeouts.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
21
# Timeouts
32

4-
Configuring requests timeouts can be done by using the `setRequestConfigCallback` method while building the `RestClient`. In the following example we increase the connect timeout (defaults to 30 second) and the response timeout (defaults to 0, which is infinite).
3+
You can set timeouts when building the `Rest5Client`:
4+
5+
- The **connect timeout** is the maximum time for establishing a TCP connection, including the TLS handshake. The connect timeout is set on `ConnectionConfig`.
6+
- The **response timeout** is the maximum period to wait for response data. The response timeout is set on `RequestConfig`.
7+
- The **connection request timeout** is the maximum time for leasing a connection from the pool. The connection request timeout is set on `RequestConfig`.
8+
9+
To configure timeouts, use `setHttpClientConfigCallback` and `setRequestConfigCallback` while building the `Rest5Client`. The following example sets a 10-second connect timeout and a 20-second response timeout:
510

611
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-timeouts
712
```java
813
Rest5ClientBuilder builder = Rest5Client
9-
.builder(new HttpHost("localhost", 9200))
14+
.builder(new HttpHost("http", "localhost", 9200)) <1>
15+
.setHttpClientConfigCallback(c -> c.setDefaultConnectionConfig(
16+
ConnectionConfig.custom()
17+
.setConnectTimeout(Timeout.ofSeconds(10))
18+
.build()
19+
))
1020
.setRequestConfigCallback(r -> r
11-
.setConnectTimeout(Timeout.of(5000, TimeUnit.MILLISECONDS))
12-
.setResponseTimeout(Timeout.of(30000, TimeUnit.MILLISECONDS))
13-
.build()
21+
.setResponseTimeout(Timeout.ofSeconds(20))
1422
);
1523
```
1624

17-
Timeouts also can be set per request with RequestOptions, which overrides RestClient's builder. The RequestOptions can then be set in the Rest5ClientTransport constructor.
25+
1. Specify `https` for TLS.
26+
27+
You can also set per-request timeouts using `RequestOptions`, which override the builder defaults. The following example sets a response timeout of 60 seconds, as well as a connection request timeout of 1 second (to limit pooled connection wait time):
1828

1929
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-request-options-timeouts
2030
```java
21-
RequestConfig requestConfig = RequestConfig.custom()
22-
.setConnectTimeout(Timeout.ofMilliseconds(5000))
23-
.setConnectionRequestTimeout(Timeout.ofMilliseconds(60000))
31+
RequestConfig requestConfig = RequestConfig.custom()
32+
.setResponseTimeout(Timeout.ofSeconds(60))
33+
.setConnectionRequestTimeout(Timeout.ofSeconds(1))
2434
.build();
2535

2636
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
@@ -30,4 +40,3 @@ RequestOptions options = RequestOptions.DEFAULT.toBuilder()
3040
ElasticsearchTransport transport = new Rest5ClientTransport(
3141
restClient, new JacksonJsonpMapper(), options);
3242
```
33-

0 commit comments

Comments
 (0)