You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/03 12:26:12 UTC
[shardingsphere-elasticjob] branch master updated: Add cache for
getHostName (#1327) (#1328)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 553165a Add cache for getHostName (#1327) (#1328)
553165a is described below
commit 553165abecbde37b0d017adac0c3834bd4946e5a
Author: Tboy <gu...@immomo.com>
AuthorDate: Mon Aug 3 20:26:01 2020 +0800
Add cache for getHostName (#1327) (#1328)
---
.../apache/shardingsphere/elasticjob/infra/env/IpUtils.java | 10 ++++++++--
.../shardingsphere/elasticjob/infra/env/IpUtilsTest.java | 10 ++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java b/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
index f27a7f6..68af52c 100644
--- a/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
+++ b/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
@@ -43,6 +43,8 @@ public final class IpUtils {
private static volatile String cachedIpAddress;
+ private static volatile String cachedHostName;
+
/**
* Get IP address for localhost.
*
@@ -147,10 +149,14 @@ public final class IpUtils {
* @return host name for localhost
*/
public static String getHostName() {
+ if (null != cachedHostName) {
+ return cachedHostName;
+ }
try {
- return InetAddress.getLocalHost().getHostName();
+ cachedHostName = InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException ex) {
- return "unknown";
+ cachedHostName = "unknown";
}
+ return cachedHostName;
}
}
diff --git a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
index 06a3a25..e0ef10e 100644
--- a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
+++ b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
@@ -17,9 +17,14 @@
package org.apache.shardingsphere.elasticjob.infra.env;
+import lombok.SneakyThrows;
import org.junit.Test;
+import java.lang.reflect.Field;
+
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
public final class IpUtilsTest {
@@ -29,7 +34,12 @@ public final class IpUtilsTest {
}
@Test
+ @SneakyThrows
public void assertGetHostName() {
assertNotNull(IpUtils.getHostName());
+ Field field = IpUtils.class.getDeclaredField("cachedHostName");
+ field.setAccessible(true);
+ String hostName = (String) field.get(null);
+ assertThat(hostName, is(IpUtils.getHostName()));
}
}