This is an automated email from the ASF dual-hosted git repository.
jin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git
The following commit(s) were added to refs/heads/master by this push:
new 0cfd8da feat: support user defined RestClientConfig/HTTPClient params
(#140)
0cfd8da is described below
commit 0cfd8daed32cb2644502d6330b9d1ad01387722c
Author: 小宇 <[email protected]>
AuthorDate: Wed Mar 13 20:13:01 2024 +0800
feat: support user defined RestClientConfig/HTTPClient params (#140)
- add builderCallback param for client to add custom config
- add params connectTimeout and readTimeout to instead the param time
- update version to 1.3.0
---------
Co-authored-by: imbajin <[email protected]>
---
.editorconfig | 31 ++++++++++++++++++++++
.../apache/hugegraph/rest/AbstractRestClient.java | 11 ++++++++
.../apache/hugegraph/rest/RestClientConfig.java | 13 ++++++++-
.../apache/hugegraph/version/CommonVersion.java | 2 +-
.../apache/hugegraph/unit/rest/RestClientTest.java | 28 +++++++++++++++++++
.../org/apache/hugegraph/version/RpcVersion.java | 2 +-
pom.xml | 10 ++++++-
7 files changed, 93 insertions(+), 4 deletions(-)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..5c47926
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+
+[*.{java, xml, py}]
+indent_style = space
+indent_size = 4
+
+[*.{java, xml}]
+# Ignore the IDEA unsupported warning & it works well (indeed)
+continuation_indent_size = 8
diff --git
a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
index 973f177..2b08e69 100644
---
a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
+++
b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
@@ -183,6 +183,12 @@ public abstract class AbstractRestClient implements
RestClient {
builder.connectTimeout(config.getTimeout(), TimeUnit.MILLISECONDS)
.readTimeout(config.getTimeout(), TimeUnit.MILLISECONDS);
}
+ if (config.getConnectTimeout() != null) {
+ builder.connectTimeout(config.getConnectTimeout(),
TimeUnit.MILLISECONDS);
+ }
+ if (config.getReadTimeout() != null) {
+ builder.readTimeout(config.getReadTimeout(),
TimeUnit.MILLISECONDS);
+ }
if (config.getMaxIdleConns() != null || config.getIdleTime() != null) {
ConnectionPool connectionPool = new
ConnectionPool(config.getMaxIdleConns(),
@@ -205,6 +211,11 @@ public abstract class AbstractRestClient implements
RestClient {
configSsl(builder, this.baseUrl, config.getTrustStoreFile(),
config.getTrustStorePassword());
+ // Execute builder callback before builder.build() for user configs
+ if (config.getBuilderCallback() != null) {
+ config.getBuilderCallback().accept(builder);
+ }
+
OkHttpClient okHttpClient = builder.build();
if (config.getMaxConns() != null) {
diff --git
a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/RestClientConfig.java
b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/RestClientConfig.java
index fc63613..c8e766b 100644
---
a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/RestClientConfig.java
+++
b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/RestClientConfig.java
@@ -17,9 +17,12 @@
package org.apache.hugegraph.rest;
+import java.util.function.Consumer;
+
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
+import okhttp3.OkHttpClient;
@Builder
@Getter
@@ -30,8 +33,15 @@ public class RestClientConfig {
private String user;
private String password;
private String token;
- // unit in milliseconds
+ /**
+ * @deprecated use connectTimeout and readTimeout instead
+ */
+ @Deprecated
private Integer timeout;
+ /** unit in milliseconds */
+ private Integer connectTimeout;
+ /** unit in milliseconds */
+ private Integer readTimeout;
private Integer maxConns;
private Integer maxConnsPerRoute;
// unit in seconds
@@ -39,4 +49,5 @@ public class RestClientConfig {
private Integer maxIdleConns = 5;
private String trustStoreFile;
private String trustStorePassword;
+ private Consumer<OkHttpClient.Builder> builderCallback;
}
diff --git
a/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java
b/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java
index a049ff4..8ae89bd 100644
---
a/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java
+++
b/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java
@@ -24,5 +24,5 @@ public class CommonVersion {
public static final String NAME = "hugegraph-common";
// The second parameter of Version.of() is for all-in-one JAR
- public static final Version VERSION = Version.of(CommonVersion.class,
"1.2.0");
+ public static final Version VERSION = Version.of(CommonVersion.class,
"1.3.0");
}
diff --git
a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java
b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java
index f7b998d..712aea7 100644
---
a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java
+++
b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java
@@ -21,6 +21,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
+import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import javax.net.ssl.SSLContext;
@@ -320,6 +321,29 @@ public class RestClientTest {
Assert.assertNull(client.getAuthContext());
}
+ @SneakyThrows
+ @Test
+ public void testBuilderCallback() {
+ // default configs
+ MockRestClientImpl restClient = new MockRestClientImpl(TEST_URL,
+
RestClientConfig.builder().build());
+ OkHttpClient okHttpClient = Whitebox.getInternalState(restClient,
"client");
+ Assert.assertEquals(okHttpClient.connectTimeoutMillis(), 10000);
+ Assert.assertEquals(okHttpClient.readTimeoutMillis(), 10000);
+
+ // set config by (user)builderCallback
+ RestClientConfig config = RestClientConfig.builder().builderCallback(
+ builder -> builder.connectTimeout(5, TimeUnit.SECONDS)
+ .readTimeout(30, TimeUnit.SECONDS))
+ .build();
+
+ restClient = new MockRestClientImpl(TEST_URL, config);
+ okHttpClient = Whitebox.getInternalState(restClient, "client");
+
+ Assert.assertEquals(okHttpClient.connectTimeoutMillis(), 5000);
+ Assert.assertEquals(okHttpClient.readTimeoutMillis(), 30000);
+ }
+
@SneakyThrows
@Test
public void testRequest() {
@@ -485,6 +509,10 @@ public class RestClientTest {
super(url, timeout);
}
+ public MockRestClientImpl(String url, RestClientConfig config) {
+ super(url, config);
+ }
+
@Override
protected void checkStatus(Response response, int... statuses) {
// pass
diff --git
a/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java
b/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java
index c7ab405..ac35982 100644
--- a/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java
+++ b/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java
@@ -24,5 +24,5 @@ public class RpcVersion {
public static final String NAME = "hugegraph-rpc";
// The second parameter of Version.of() is for all-in-one JAR
- public static final Version VERSION = Version.of(RpcVersion.class,
"1.2.0");
+ public static final Version VERSION = Version.of(RpcVersion.class,
"1.3.0");
}
diff --git a/pom.xml b/pom.xml
index 2b8a486..157da40 100644
--- a/pom.xml
+++ b/pom.xml
@@ -90,7 +90,7 @@
<properties>
<!-- Note: We need also update the version in CommonVersion.java &
RpcVersion.java now -->
- <revision>1.2.0</revision>
+ <revision>1.3.0</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<top.level.dir>${project.basedir}/..</top.level.dir>
<compiler.source>1.8</compiler.source>
@@ -270,6 +270,14 @@
<goal>clean</goal>
</goals>
</execution>
+ <!-- auto delete .flattened-pom.xml after "install" step
-->
+ <execution>
+ <id>remove-flattened-pom</id>
+ <phase>install</phase>
+ <goals>
+ <goal>clean</goal>
+ </goals>
+ </execution>
</executions>
</plugin>
</plugins>