diff options
Diffstat (limited to 'src/main/java')
3 files changed, 21 insertions, 9 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java index bb77abc020..16f882fd11 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java @@ -282,7 +282,6 @@ public class BazelWorkspaceStatusModule extends BlazeModule { } private class BazelStatusActionFactory implements WorkspaceStatusAction.Factory { - private String hostname; @Override public Map<String, String> createDummyWorkspaceStatus() { @@ -312,11 +311,7 @@ public class BazelWorkspaceStatusModule extends BlazeModule { * changes during bazel server lifetime, bazel will not see the change. */ private String getHostname() { - if (hostname == null) { - hostname = NetUtil.findShortHostName(); - } - - return hostname; + return NetUtil.getCachedShortHostName(); } } diff --git a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java index 57d757691d..9aed79b08d 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java +++ b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java @@ -90,7 +90,7 @@ public final class LocalSpawnRunner implements SpawnRunner { this.execRoot = execRoot; this.processWrapper = getProcessWrapper(execRoot, localOs).getPathString(); this.localExecutionOptions = Preconditions.checkNotNull(localExecutionOptions); - this.hostName = NetUtil.findShortHostName(); + this.hostName = NetUtil.getCachedShortHostName(); this.resourceManager = resourceManager; this.useProcessWrapper = useProcessWrapper; this.productName = productName; diff --git a/src/main/java/com/google/devtools/build/lib/util/NetUtil.java b/src/main/java/com/google/devtools/build/lib/util/NetUtil.java index f22146a71d..dc2ce8867e 100644 --- a/src/main/java/com/google/devtools/build/lib/util/NetUtil.java +++ b/src/main/java/com/google/devtools/build/lib/util/NetUtil.java @@ -21,14 +21,31 @@ import java.net.UnknownHostException; */ public final class NetUtil { + private static String hostname; + private NetUtil() { } /** + * Returns the *cached* short hostname (computed at most once per the lifetime of a server). Can + * take seconds to complete when the cache is cold. + */ + public static String getCachedShortHostName() { + if (hostname == null) { + synchronized (NetUtil.class) { + if (hostname == null) { + hostname = computeShortHostName(); + } + } + } + return computeShortHostName(); + } + + /** * Returns the short hostname or <code>unknown</code> if the host name could not be determined. - * Performs reverse DNS lookup and can take seconds to complete! + * Performs reverse DNS lookup and can take seconds to complete. */ - public static String findShortHostName() { + private static String computeShortHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { |