Skip to content

Commit

Permalink
[java] Making non-loopback ipv4 address caching a bit smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Mar 26, 2019
1 parent 5c2dcce commit 1bd9f5f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions java/client/src/org/openqa/selenium/net/NetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class NetworkUtils {

private static String cachedNonLoopbackAddressOfThisMachine;
private static InetAddress cachedIp4NonLoopbackAddressOfThisMachine;
private static String cachedIp4NonLoopbackAddressHostName;

private final NetworkInterfaceProvider networkInterfaceProvider;
private volatile String hostname;
Expand Down Expand Up @@ -86,10 +88,12 @@ public String getPrivateLocalAddress() {
* @return A String representing the host name or non-loopback IP4 address of this machine.
*/
public String getNonLoopbackAddressOfThisMachine() {
if (cachedNonLoopbackAddressOfThisMachine == null) {
cachedNonLoopbackAddressOfThisMachine = getIp4NonLoopbackAddressOfThisMachine().getHostName();
InetAddress ip4NonLoopbackAddressOfThisMachine = getIp4NonLoopbackAddressOfThisMachine();
if (! Objects.equals(cachedIp4NonLoopbackAddressOfThisMachine, ip4NonLoopbackAddressOfThisMachine)) {
cachedIp4NonLoopbackAddressOfThisMachine = ip4NonLoopbackAddressOfThisMachine;
cachedIp4NonLoopbackAddressHostName = ip4NonLoopbackAddressOfThisMachine.getHostName();
}
return cachedNonLoopbackAddressOfThisMachine;
return cachedIp4NonLoopbackAddressHostName;
}

/**
Expand Down

0 comments on commit 1bd9f5f

Please sign in to comment.