Skip to content

Commit

Permalink
[ISSUE#12748] Support custom client configuration timeout. (#12764)
Browse files Browse the repository at this point in the history
* Support custom client configuration timeout.(#12748)

* Add UT.(#12748
  • Loading branch information
MatthewAden authored Oct 28, 2024
1 parent b04d226 commit 4334cd1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
29 changes: 29 additions & 0 deletions client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ParamUtilTest {

private int defaultConnectTimeout;

private int defaultReadTimeout;

private double defaultPerTaskConfigSize;

private String defaultNodesPath;
Expand All @@ -58,6 +60,7 @@ void before() {
defaultContextPath = "nacos";
defaultVersion = VersionUtils.version;
defaultConnectTimeout = 1000;
defaultReadTimeout = 3000;
defaultPerTaskConfigSize = 3000.0;
defaultNodesPath = "serverlist";
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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, () -> {
Expand Down

0 comments on commit 4334cd1

Please sign in to comment.