Skip to content

Commit

Permalink
feat: support user defined RestClientConfig/HTTPClient params (#140)
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
zhenyuT and imbajin authored Mar 13, 2024
1 parent 33fa9ed commit 0cfd8da
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 4 deletions.
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,13 +33,21 @@ 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
private Integer idleTime = 30;
private Integer maxIdleConns = 5;
private String trustStoreFile;
private String trustStorePassword;
private Consumer<OkHttpClient.Builder> builderCallback;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 0cfd8da

Please sign in to comment.