diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java index 9afc49cfcf1..86de1116312 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java @@ -56,7 +56,7 @@ public class ConfigHttpClientManager implements Closeable { private static final int CON_TIME_OUT_MILLIS = ParamUtil.getConnectTimeout(); - private static final int READ_TIME_OUT_MILLIS = 3000; + private static final int READ_TIME_OUT_MILLIS = ParamUtil.getReadTimeout(); private final LimiterHttpClientRequestInterceptor limiterHttpClientRequestInterceptor = new LimiterHttpClientRequestInterceptor(); diff --git a/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java b/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java index 60d82f44e05..593407911bf 100644 --- a/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java @@ -58,6 +58,8 @@ public class ParamUtil { private static int connectTimeout; + private static int readTimeout; + private static double perTaskConfigSize = 3000; private static final String NACOS_CLIENT_APP_KEY = "nacos.client.appKey"; @@ -72,8 +74,12 @@ public class ParamUtil { private static final String NACOS_CONNECT_TIMEOUT_KEY = "NACOS.CONNECT.TIMEOUT"; + private static final String NACOS_READ_TIMEOUT_KEY = "NACOS.READ.TIMEOUT"; + private static final String DEFAULT_NACOS_CONNECT_TIMEOUT = "1000"; + private static final String DEFAULT_NACOS_READ_TIMEOUT = "3000"; + private static final String PER_TASK_CONFIG_SIZE_KEY = "PER_TASK_CONFIG_SIZE"; private static final String DEFAULT_PER_TASK_CONFIG_SIZE_KEY = "3000"; @@ -97,6 +103,9 @@ public class ParamUtil { connectTimeout = initConnectionTimeout(); LOGGER.info("[settings] [http-client] connect timeout:{}", connectTimeout); + readTimeout = initReadTimeout(); + LOGGER.info("[settings] [http-client] read timeout:{}", readTimeout); + clientVersion = VersionUtils.version; perTaskConfigSize = initPerTaskConfigSize(); @@ -115,6 +124,18 @@ private static int initConnectionTimeout() { } } + private static int initReadTimeout() { + String tmp = DEFAULT_NACOS_READ_TIMEOUT; + try { + tmp = NacosClientProperties.PROTOTYPE.getProperty(NACOS_READ_TIMEOUT_KEY, DEFAULT_NACOS_READ_TIMEOUT); + return Integer.parseInt(tmp); + } catch (NumberFormatException e) { + final String msg = "[http-client] invalid read timeout:" + tmp; + LOGGER.error("[settings] " + msg, e); + throw new IllegalArgumentException(msg, e); + } + } + private static double initPerTaskConfigSize() { try { return Double.parseDouble(NacosClientProperties.PROTOTYPE.getProperty(PER_TASK_CONFIG_SIZE_KEY, @@ -165,6 +186,14 @@ public static void setConnectTimeout(int connectTimeout) { ParamUtil.connectTimeout = connectTimeout; } + public static int getReadTimeout() { + return readTimeout; + } + + public static void setReadTimeout(int readTimeout) { + ParamUtil.readTimeout = readTimeout; + } + public static double getPerTaskConfigSize() { return perTaskConfigSize; } diff --git a/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java b/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java index 008acdcbf0c..796f7f07348 100644 --- a/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java @@ -47,6 +47,8 @@ class ParamUtilTest { private int defaultConnectTimeout; + private int defaultReadTimeout; + private double defaultPerTaskConfigSize; private String defaultNodesPath; @@ -58,6 +60,7 @@ void before() { defaultContextPath = "nacos"; defaultVersion = VersionUtils.version; defaultConnectTimeout = 1000; + defaultReadTimeout = 3000; defaultPerTaskConfigSize = 3000.0; defaultNodesPath = "serverlist"; } @@ -69,9 +72,11 @@ void after() { ParamUtil.setDefaultContextPath(defaultContextPath); ParamUtil.setClientVersion(defaultVersion); ParamUtil.setConnectTimeout(defaultConnectTimeout); + ParamUtil.setReadTimeout(defaultReadTimeout); ParamUtil.setPerTaskConfigSize(defaultPerTaskConfigSize); ParamUtil.setDefaultNodesPath(defaultNodesPath); System.clearProperty("NACOS.CONNECT.TIMEOUT"); + System.clearProperty("NACOS_READ_TIMEOUT"); System.clearProperty("PER_TASK_CONFIG_SIZE"); System.clearProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL); } @@ -126,6 +131,16 @@ void testSetConnectTimeout() { assertEquals(expect, ParamUtil.getConnectTimeout()); } + @Test + void testSetReadTimeout() { + int defaultVal = ParamUtil.getReadTimeout(); + assertEquals(defaultReadTimeout, defaultVal); + + int expect = 3000; + ParamUtil.setReadTimeout(expect); + assertEquals(expect, ParamUtil.getReadTimeout()); + } + @Test void testGetPerTaskConfigSize() { double defaultVal = ParamUtil.getPerTaskConfigSize(); @@ -184,6 +199,20 @@ void testInitConnectionTimeoutWithException() throws Throwable { }); } + @Test + void testInitReadTimeoutWithException() throws Throwable { + assertThrows(IllegalArgumentException.class, () -> { + Method method = ParamUtil.class.getDeclaredMethod("initReadTimeout"); + method.setAccessible(true); + System.setProperty("NACOS.READ.TIMEOUT", "test"); + try { + method.invoke(null); + } catch (InvocationTargetException e) { + throw e.getCause(); + } + }); + } + @Test void testInitPerTaskConfigSizeWithException() throws Throwable { assertThrows(IllegalArgumentException.class, () -> {