mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-24 17:09:48 +00:00
Different kinds of requests may need different request options from the client default. Users can optionally set RequestConfig on a single request's RequestOptions to override the default. Without this, socketTimeout can only set at RestClient initialization. Co-authored-by: weizijun <weizijun1989@gmail.com>
This commit is contained in:
parent
f4c46075b4
commit
55c4dec360
@ -20,6 +20,7 @@
|
|||||||
package org.elasticsearch.client;
|
package org.elasticsearch.client;
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
|
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
|
||||||
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
|
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
|
||||||
@ -38,23 +39,25 @@ public final class RequestOptions {
|
|||||||
* Default request options.
|
* Default request options.
|
||||||
*/
|
*/
|
||||||
public static final RequestOptions DEFAULT = new Builder(
|
public static final RequestOptions DEFAULT = new Builder(
|
||||||
Collections.emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT, null).build();
|
Collections.emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT, null, null).build();
|
||||||
|
|
||||||
private final List<Header> headers;
|
private final List<Header> headers;
|
||||||
private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
|
private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
|
||||||
private final WarningsHandler warningsHandler;
|
private final WarningsHandler warningsHandler;
|
||||||
|
private final RequestConfig requestConfig;
|
||||||
|
|
||||||
private RequestOptions(Builder builder) {
|
private RequestOptions(Builder builder) {
|
||||||
this.headers = Collections.unmodifiableList(new ArrayList<>(builder.headers));
|
this.headers = Collections.unmodifiableList(new ArrayList<>(builder.headers));
|
||||||
this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory;
|
this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory;
|
||||||
this.warningsHandler = builder.warningsHandler;
|
this.warningsHandler = builder.warningsHandler;
|
||||||
|
this.requestConfig = builder.requestConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a builder that contains these options but can be modified.
|
* Create a builder that contains these options but can be modified.
|
||||||
*/
|
*/
|
||||||
public Builder toBuilder() {
|
public Builder toBuilder() {
|
||||||
return new Builder(headers, httpAsyncResponseConsumerFactory, warningsHandler);
|
return new Builder(headers, httpAsyncResponseConsumerFactory, warningsHandler, requestConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,6 +98,15 @@ public final class RequestOptions {
|
|||||||
return warningsHandler;
|
return warningsHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get RequestConfig, which can set socketTimeout, connectTimeout
|
||||||
|
* and so on by request
|
||||||
|
* @return RequestConfig
|
||||||
|
*/
|
||||||
|
public RequestConfig getRequestConfig() {
|
||||||
|
return requestConfig;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
@ -152,12 +164,14 @@ public final class RequestOptions {
|
|||||||
private final List<Header> headers;
|
private final List<Header> headers;
|
||||||
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
|
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
|
||||||
private WarningsHandler warningsHandler;
|
private WarningsHandler warningsHandler;
|
||||||
|
private RequestConfig requestConfig;
|
||||||
|
|
||||||
private Builder(List<Header> headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
|
private Builder(List<Header> headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
|
||||||
WarningsHandler warningsHandler) {
|
WarningsHandler warningsHandler, RequestConfig requestConfig) {
|
||||||
this.headers = new ArrayList<>(headers);
|
this.headers = new ArrayList<>(headers);
|
||||||
this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory;
|
this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory;
|
||||||
this.warningsHandler = warningsHandler;
|
this.warningsHandler = warningsHandler;
|
||||||
|
this.requestConfig = requestConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -208,6 +222,17 @@ public final class RequestOptions {
|
|||||||
public void setWarningsHandler(WarningsHandler warningsHandler) {
|
public void setWarningsHandler(WarningsHandler warningsHandler) {
|
||||||
this.warningsHandler = warningsHandler;
|
this.warningsHandler = warningsHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set RequestConfig, which can set socketTimeout, connectTimeout
|
||||||
|
* and so on by request
|
||||||
|
* @param requestConfig http client RequestConfig
|
||||||
|
* @return Builder
|
||||||
|
*/
|
||||||
|
public Builder setRequestConfig(RequestConfig requestConfig) {
|
||||||
|
this.requestConfig = requestConfig;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,6 +29,7 @@ import org.apache.http.HttpResponse;
|
|||||||
import org.apache.http.client.AuthCache;
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.entity.GzipDecompressingEntity;
|
import org.apache.http.client.entity.GzipDecompressingEntity;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||||
import org.apache.http.client.methods.HttpHead;
|
import org.apache.http.client.methods.HttpHead;
|
||||||
import org.apache.http.client.methods.HttpOptions;
|
import org.apache.http.client.methods.HttpOptions;
|
||||||
@ -710,6 +711,7 @@ public class RestClient implements Closeable {
|
|||||||
this.httpRequest = createHttpRequest(request.getMethod(), uri, request.getEntity());
|
this.httpRequest = createHttpRequest(request.getMethod(), uri, request.getEntity());
|
||||||
this.cancellable = Cancellable.fromRequest(httpRequest);
|
this.cancellable = Cancellable.fromRequest(httpRequest);
|
||||||
setHeaders(httpRequest, request.getOptions().getHeaders());
|
setHeaders(httpRequest, request.getOptions().getHeaders());
|
||||||
|
setRequestConfig(httpRequest, request.getOptions().getRequestConfig());
|
||||||
this.warningsHandler = request.getOptions().getWarningsHandler() == null ?
|
this.warningsHandler = request.getOptions().getWarningsHandler() == null ?
|
||||||
RestClient.this.warningsHandler : request.getOptions().getWarningsHandler();
|
RestClient.this.warningsHandler : request.getOptions().getWarningsHandler();
|
||||||
}
|
}
|
||||||
@ -728,6 +730,12 @@ public class RestClient implements Closeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setRequestConfig(HttpRequestBase httpRequest, RequestConfig requestConfig) {
|
||||||
|
if (requestConfig != null) {
|
||||||
|
httpRequest.setConfig(requestConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RequestContext createContextForNextAttempt(Node node, AuthCache authCache) {
|
RequestContext createContextForNextAttempt(Node node, AuthCache authCache) {
|
||||||
this.httpRequest.reset();
|
this.httpRequest.reset();
|
||||||
return new RequestContext(this, node, authCache);
|
return new RequestContext(this, node, authCache);
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
package org.elasticsearch.client;
|
package org.elasticsearch.client;
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
|
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -90,6 +91,22 @@ public class RequestOptionsTests extends RestClientTestCase {
|
|||||||
assertSame(factory, options.getHttpAsyncResponseConsumerFactory());
|
assertSame(factory, options.getHttpAsyncResponseConsumerFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSetRequestBuilder() {
|
||||||
|
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
|
||||||
|
|
||||||
|
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
|
||||||
|
int socketTimeout = 10000;
|
||||||
|
int connectTimeout = 100;
|
||||||
|
requestConfigBuilder.setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout);
|
||||||
|
RequestConfig requestConfig = requestConfigBuilder.build();
|
||||||
|
|
||||||
|
builder.setRequestConfig(requestConfig);
|
||||||
|
RequestOptions options = builder.build();
|
||||||
|
assertSame(options.getRequestConfig(), requestConfig);
|
||||||
|
assertEquals(options.getRequestConfig().getSocketTimeout(), socketTimeout);
|
||||||
|
assertEquals(options.getRequestConfig().getConnectTimeout(), connectTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
public void testEqualsAndHashCode() {
|
public void testEqualsAndHashCode() {
|
||||||
RequestOptions request = randomBuilder().build();
|
RequestOptions request = randomBuilder().build();
|
||||||
assertEquals(request, request);
|
assertEquals(request, request);
|
||||||
@ -122,6 +139,10 @@ public class RequestOptionsTests extends RestClientTestCase {
|
|||||||
builder.setWarningsHandler(randomBoolean() ? WarningsHandler.STRICT : WarningsHandler.PERMISSIVE);
|
builder.setWarningsHandler(randomBoolean() ? WarningsHandler.STRICT : WarningsHandler.PERMISSIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (randomBoolean()) {
|
||||||
|
builder.setRequestConfig(RequestConfig.custom().build());
|
||||||
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,6 +327,17 @@ public class RestClientDocumentation {
|
|||||||
});
|
});
|
||||||
//end::rest-client-config-timeouts
|
//end::rest-client-config-timeouts
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
//tag::rest-client-config-request-options-timeouts
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(5000)
|
||||||
|
.setSocketTimeout(60000)
|
||||||
|
.build();
|
||||||
|
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
|
||||||
|
.setRequestConfig(requestConfig)
|
||||||
|
.build();
|
||||||
|
//end::rest-client-config-request-options-timeouts
|
||||||
|
}
|
||||||
{
|
{
|
||||||
//tag::rest-client-config-threads
|
//tag::rest-client-config-threads
|
||||||
RestClientBuilder builder = RestClient.builder(
|
RestClientBuilder builder = RestClient.builder(
|
||||||
|
@ -25,6 +25,13 @@ seconds).
|
|||||||
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]
|
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
|
timeouts also can set by RequestOptions per request.this will override RestClient customizeRequestConfig.
|
||||||
|
|
||||||
|
["source","java",subs="attributes,callouts,macros"]
|
||||||
|
--------------------------------------------------
|
||||||
|
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-request-options-timeouts]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
=== Number of threads
|
=== Number of threads
|
||||||
|
|
||||||
The Apache Http Async Client starts by default one dispatcher thread, and a
|
The Apache Http Async Client starts by default one dispatcher thread, and a
|
||||||
|
Loading…
x
Reference in New Issue
Block a user