Skip to content

Commit a38226b

Browse files
committed
Implementing lazy loopback detection. This provides ~10x speed improvement of selenium server startup.
1 parent 01399ff commit a38226b

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Diff for: java/client/src/org/openqa/selenium/net/NetworkInterface.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,13 @@
3131
public class NetworkInterface {
3232

3333
private final String name;
34+
private java.net.NetworkInterface networkInterface;
3435
private final Iterable<InetAddress> inetAddresses;
35-
private boolean isLoopback;
36+
private Boolean isLoopback;
3637

3738
public NetworkInterface(java.net.NetworkInterface networkInterface) {
3839
this(networkInterface.getName(), list(networkInterface.getInetAddresses()));
39-
try {
40-
// Issue 1181 : determine whether this NetworkInterface instance is loopback
41-
// from java.net.NetworkInterface API
42-
this.isLoopback = networkInterface.isLoopback();
43-
} catch (SocketException ex) {
44-
Logger.getLogger(NetworkInterface.class.getName()).log(Level.WARNING, null, ex);
45-
// If an SocketException is caught, determine whether this NetworkInterface
46-
// instance is loopback from computation from its inetAddresses
47-
this.isLoopback =
48-
isLoopBackFromINetAddresses(list(networkInterface.getInetAddresses()));
49-
}
40+
this.networkInterface = networkInterface;
5041
}
5142

5243
NetworkInterface(String name, Iterable<InetAddress> inetAddresses) {
@@ -64,6 +55,22 @@ public boolean isIp4AddressBindingOnly() {
6455
}
6556

6657
public boolean isLoopBack() {
58+
if (isLoopback == null) {
59+
if (networkInterface != null) {
60+
try {
61+
// Issue 1181 : determine whether this NetworkInterface instance is loopback
62+
// from java.net.NetworkInterface API
63+
isLoopback = networkInterface.isLoopback();
64+
} catch (SocketException ex) {
65+
Logger.getLogger(NetworkInterface.class.getName()).log(Level.WARNING, null, ex);
66+
}
67+
}
68+
// If a SocketException is caught, determine whether this NetworkInterface
69+
// instance is loopback from computation from its inetAddresses
70+
if (isLoopback == null) {
71+
isLoopback = isLoopBackFromINetAddresses(list(networkInterface.getInetAddresses()));
72+
}
73+
}
6774
return isLoopback;
6875
}
6976

@@ -80,11 +87,11 @@ public InetAddress getIp4LoopbackOnly() {
8087
// Most "normal" boxes don't have multiple addresses so we'll just refine this
8188
// algorithm until it works.
8289
// See NetworkUtilsTest#testOpenSuseBoxIssue1181
83-
InetAddress lastFound = null;
8490
// Issue 1181
85-
if (!isLoopback) {
86-
return lastFound;
91+
if (!isLoopBack()) {
92+
return null;
8793
}
94+
InetAddress lastFound = null;
8895
for (InetAddress inetAddress : inetAddresses) {
8996
if (inetAddress.isLoopbackAddress() && !isIpv6(inetAddress)) {
9097
lastFound = inetAddress;

0 commit comments

Comments
 (0)