This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 09ddd3f add property. (#2736)
09ddd3f is described below
commit 09ddd3fbc7d47537729e388f282d25fe7b8f934e
Author: Kevin Clair <[email protected]>
AuthorDate: Sun Jan 9 21:46:01 2022 +0800
add property. (#2736)
---
.../shenyu/common/constant/HttpConstants.java | 9 ++++
.../shenyu/sync/data/http/HttpSyncDataService.java | 21 ++++-----
.../shenyu/sync/data/http/config/HttpConfig.java | 50 +++++++++++++++++++++-
.../sync/data/http/config/HttpConfigTest.java | 2 +-
4 files changed, 66 insertions(+), 16 deletions(-)
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/HttpConstants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/HttpConstants.java
index 3f3ec1e..7d29fd8 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/HttpConstants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/HttpConstants.java
@@ -36,4 +36,13 @@ public final class HttpConstants {
*/
public static final long SERVER_MAX_HOLD_TIMEOUT =
TimeUnit.SECONDS.toMillis(60);
+ /**
+ * Default connection timeout is 10s.
+ */
+ public static final long CLIENT_POLLING_CONNECT_TIMEOUT =
TimeUnit.SECONDS.toMillis(10);
+
+ /**
+ * Default write timeout is 90s.
+ */
+ public static final long CLIENT_POLLING_WRITE_TIMEOUT =
TimeUnit.SECONDS.toMillis(90);
}
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/HttpSyncDataService.java
b/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/HttpSyncDataService.java
index 8942a31..ab71830 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/HttpSyncDataService.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/HttpSyncDataService.java
@@ -52,14 +52,13 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
-import java.time.Duration;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
-import java.util.Objects;
import java.util.Map;
-import java.util.HashMap;
+import java.util.Objects;
import java.util.Optional;
-import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
@@ -81,11 +80,6 @@ public class HttpSyncDataService implements SyncDataService,
AutoCloseable {
private static final AtomicBoolean RUNNING = new AtomicBoolean(false);
/**
- * default: 10s.
- */
- private final Duration connectionTimeout = Duration.ofSeconds(10);
-
- /**
* only use for http long polling.
*/
private final RestTemplate httpClient;
@@ -105,15 +99,16 @@ public class HttpSyncDataService implements
SyncDataService, AutoCloseable {
this.httpConfig = httpConfig;
this.factory = new DataRefreshFactory(pluginDataSubscriber,
metaDataSubscribers, authDataSubscribers);
this.serverList =
Lists.newArrayList(Splitter.on(",").split(httpConfig.getUrl()));
- this.httpClient = createRestTemplate();
+ this.httpClient = createRestTemplate(httpConfig);
this.accessToken = new FreshBeanHolder<>(this::doLogin);
this.start();
}
- private RestTemplate createRestTemplate() {
+ private RestTemplate createRestTemplate(final HttpConfig httpConfig) {
OkHttp3ClientHttpRequestFactory factory = new
OkHttp3ClientHttpRequestFactory();
- factory.setConnectTimeout((int) this.connectionTimeout.toMillis());
- factory.setReadTimeout((int)
HttpConstants.CLIENT_POLLING_READ_TIMEOUT);
+
factory.setConnectTimeout(Objects.isNull(httpConfig.getConnectionTimeout()) ?
(int) HttpConstants.CLIENT_POLLING_CONNECT_TIMEOUT :
httpConfig.getConnectionTimeout());
+ factory.setReadTimeout(Objects.isNull(httpConfig.getReadTimeout()) ?
(int) HttpConstants.CLIENT_POLLING_READ_TIMEOUT : httpConfig.getReadTimeout());
+ factory.setWriteTimeout(Objects.isNull(httpConfig.getWriteTimeout()) ?
(int) HttpConstants.CLIENT_POLLING_WRITE_TIMEOUT :
httpConfig.getWriteTimeout());
return new RestTemplate(factory);
}
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/config/HttpConfig.java
b/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/config/HttpConfig.java
index c4ede92..d0a410c 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/config/HttpConfig.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-http/src/main/java/org/apache/shenyu/sync/data/http/config/HttpConfig.java
@@ -30,6 +30,10 @@ public class HttpConfig {
private Integer connectionTimeout;
+ private Integer readTimeout;
+
+ private Integer writeTimeout;
+
private String username;
private String password;
@@ -124,6 +128,42 @@ public class HttpConfig {
this.connectionTimeout = connectionTimeout;
}
+ /**
+ * Gets the value of readTimeout.
+ *
+ * @return the value of readTimeout
+ */
+ public Integer getReadTimeout() {
+ return readTimeout;
+ }
+
+ /**
+ * Sets the readTimeout.
+ *
+ * @param readTimeout readTimeout
+ */
+ public void setReadTimeout(final Integer readTimeout) {
+ this.readTimeout = readTimeout;
+ }
+
+ /**
+ * Gets the value of writeTimeout.
+ *
+ * @return the value of writeTimeout
+ */
+ public Integer getWriteTimeout() {
+ return writeTimeout;
+ }
+
+ /**
+ * Sets the writeTimeout.
+ *
+ * @param writeTimeout writeTimeout
+ */
+ public void setWriteTimeout(final Integer writeTimeout) {
+ this.writeTimeout = writeTimeout;
+ }
+
@Override
public boolean equals(final Object o) {
if (this == o) {
@@ -135,12 +175,14 @@ public class HttpConfig {
HttpConfig that = (HttpConfig) o;
return Objects.equals(url, that.url)
&& Objects.equals(delayTime, that.delayTime)
- && Objects.equals(connectionTimeout, that.connectionTimeout);
+ && Objects.equals(connectionTimeout, that.connectionTimeout)
+ && Objects.equals(readTimeout, that.readTimeout)
+ && Objects.equals(writeTimeout, that.writeTimeout);
}
@Override
public int hashCode() {
- return Objects.hash(url, delayTime, connectionTimeout);
+ return Objects.hash(url, delayTime, connectionTimeout, readTimeout,
writeTimeout);
}
@Override
@@ -153,6 +195,10 @@ public class HttpConfig {
+ delayTime
+ ", connectionTimeout="
+ connectionTimeout
+ + ", readTimeout="
+ + readTimeout
+ + ", writeTimeout="
+ + writeTimeout
+ '}';
}
}
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-http/src/test/java/org/apache/shenyu/sync/data/http/config/HttpConfigTest.java
b/shenyu-sync-data-center/shenyu-sync-data-http/src/test/java/org/apache/shenyu/sync/data/http/config/HttpConfigTest.java
index 8231e5a..2b12fe8 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-http/src/test/java/org/apache/shenyu/sync/data/http/config/HttpConfigTest.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-http/src/test/java/org/apache/shenyu/sync/data/http/config/HttpConfigTest.java
@@ -70,7 +70,7 @@ public class HttpConfigTest {
@Test
public void testHashCode() {
assertEquals(Objects.hash(httpConfig.getUrl(),
httpConfig.getDelayTime(),
- httpConfig.getConnectionTimeout()),
+ httpConfig.getConnectionTimeout(),
httpConfig.getReadTimeout(), httpConfig.getWriteTimeout()),
httpConfig.hashCode());
}
}