From 0cfd8daed32cb2644502d6330b9d1ad01387722c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=AE=87?= <940643974@qq.com> Date: Wed, 13 Mar 2024 20:13:01 +0800 Subject: [PATCH] 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 --- .editorconfig | 31 +++++++++++++++++++ .../hugegraph/rest/AbstractRestClient.java | 11 +++++++ .../hugegraph/rest/RestClientConfig.java | 13 +++++++- .../hugegraph/version/CommonVersion.java | 2 +- .../hugegraph/unit/rest/RestClientTest.java | 28 +++++++++++++++++ .../apache/hugegraph/version/RpcVersion.java | 2 +- pom.xml | 10 +++++- 7 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..5c47926694 --- /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 973f177da5..2b08e69b38 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 @@ private OkHttpClient buildOkHttpClient(RestClientConfig config) { 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 @@ private OkHttpClient buildOkHttpClient(RestClientConfig config) { 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 fc63613bb2..c8e766ba7e 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 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 a049ff44fe..8ae89bd0e2 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 f7b998df5c..712aea7ab2 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.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 void testAuthContext() { 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 MockRestClientImpl(String url, int timeout) { 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 c7ab4052a0..ac359822d1 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 2b8a486621..157da40bea 100644 --- a/pom.xml +++ b/pom.xml @@ -90,7 +90,7 @@ - 1.2.0 + 1.3.0 UTF-8 ${project.basedir}/.. 1.8 @@ -270,6 +270,14 @@ clean + + + remove-flattened-pom + install + + clean + +