Browse Source

LLRC RequestOptions add RequestConfig (#57972)

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.
weizijun 5 years ago
parent
commit
974f6e66b6

+ 28 - 3
client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.client;
 
 import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
 import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
@@ -38,23 +39,25 @@ public final class RequestOptions {
      * Default request options.
      */
     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 HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
     private final WarningsHandler warningsHandler;
+    private final RequestConfig requestConfig;
 
     private RequestOptions(Builder builder) {
         this.headers = Collections.unmodifiableList(new ArrayList<>(builder.headers));
         this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory;
         this.warningsHandler = builder.warningsHandler;
+        this.requestConfig = builder.requestConfig;
     }
 
     /**
      * Create a builder that contains these options but can be modified.
      */
     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;
     }
 
+    /**
+     * get RequestConfig, which can set socketTimeout, connectTimeout
+     * and so on by request
+     * @return RequestConfig
+     */
+    public RequestConfig getRequestConfig() {
+        return requestConfig;
+    }
+
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder();
@@ -152,12 +164,14 @@ public final class RequestOptions {
         private final List<Header> headers;
         private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
         private WarningsHandler warningsHandler;
+        private RequestConfig requestConfig;
 
         private Builder(List<Header> headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
-                WarningsHandler warningsHandler) {
+                WarningsHandler warningsHandler, RequestConfig requestConfig) {
             this.headers = new ArrayList<>(headers);
             this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory;
             this.warningsHandler = warningsHandler;
+            this.requestConfig = requestConfig;
         }
 
         /**
@@ -210,6 +224,17 @@ public final class RequestOptions {
             this.warningsHandler = warningsHandler;
             return this;
         }
+
+        /**
+         * 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;
+        }
     }
 
     /**

+ 8 - 0
client/rest/src/main/java/org/elasticsearch/client/RestClient.java

@@ -29,6 +29,7 @@ import org.apache.http.HttpResponse;
 import org.apache.http.client.AuthCache;
 import org.apache.http.client.ClientProtocolException;
 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.HttpHead;
 import org.apache.http.client.methods.HttpOptions;
@@ -718,6 +719,7 @@ public class RestClient implements Closeable {
             this.httpRequest = createHttpRequest(request.getMethod(), uri, request.getEntity());
             this.cancellable = Cancellable.fromRequest(httpRequest);
             setHeaders(httpRequest, request.getOptions().getHeaders());
+            setRequestConfig(httpRequest, request.getOptions().getRequestConfig());
             this.warningsHandler = request.getOptions().getWarningsHandler() == null ?
                 RestClient.this.warningsHandler : request.getOptions().getWarningsHandler();
         }
@@ -736,6 +738,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) {
             this.httpRequest.reset();
             return new RequestContext(this, node, authCache);

+ 21 - 0
client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.client;
 
 import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
 import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
 
 import java.util.ArrayList;
@@ -90,6 +91,22 @@ public class RequestOptionsTests extends RestClientTestCase {
         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() {
         RequestOptions request = randomBuilder().build();
         assertEquals(request, request);
@@ -122,6 +139,10 @@ public class RequestOptionsTests extends RestClientTestCase {
             builder.setWarningsHandler(randomBoolean() ? WarningsHandler.STRICT : WarningsHandler.PERMISSIVE);
         }
 
+        if (randomBoolean()) {
+            builder.setRequestConfig(RequestConfig.custom().build());
+        }
+
         return builder;
     }
 

+ 11 - 0
client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java

@@ -327,6 +327,17 @@ public class RestClientDocumentation {
                     });
             //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
             RestClientBuilder builder = RestClient.builder(

+ 7 - 0
docs/java-rest/low-level/configuration.asciidoc

@@ -25,6 +25,13 @@ seconds).
 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
 
 The Apache Http Async Client starts by default one dispatcher thread, and a