Skip to content

Commit

Permalink
Using CACHED_ADDRESSES correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Jan 29, 2024
1 parent 8e7deaa commit b43aba9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/org/jgroups/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public enum AddressScope {GLOBAL,SITE_LOCAL,LINK_LOCAL,LOOPBACK,NON_LOOPBACK}

try {
CACHED_INTERFACES=getAllAvailableInterfaces();
CACHED_ADDRESSES=getAllAvailableAddresses(null);
CACHED_ADDRESSES=getAllAvailableAddresses();
}
catch(SocketException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -4644,7 +4644,7 @@ public static StackType getIpStackType() {
* if the type cannot be detected
*/
private static StackType _getIpStackType() {
Collection<InetAddress> all_addresses=getAllAvailableAddresses(null);
Collection<InetAddress> all_addresses=getAllAvailableAddresses();
for(InetAddress addr: all_addresses) {
if(addr instanceof Inet4Address)
ipv4_stack_available=true;
Expand All @@ -4668,7 +4668,7 @@ else if(addr instanceof Inet6Address)


public static boolean isStackAvailable(boolean ipv4) {
Collection<InetAddress> all_addrs=getAllAvailableAddresses(null);
Collection<InetAddress> all_addrs=getAllAvailableAddresses();
for(InetAddress addr : all_addrs)
if(ipv4 && addr instanceof Inet4Address || (!ipv4 && addr instanceof Inet6Address))
return true;
Expand Down Expand Up @@ -4711,10 +4711,9 @@ public static Collection<InetAddress> getAllAvailableAddresses(Predicate<InetAdd
}

private static synchronized Collection<InetAddress> getAllAvailableAddresses() {
if (CACHED_ADDRESSES != null) {
if(CACHED_ADDRESSES != null)
return CACHED_ADDRESSES;
}
Set<InetAddress> addresses = new HashSet<>();
Set<InetAddress> addresses=new HashSet<>();
try {
List<NetworkInterface> interfaces=getAllAvailableInterfaces();
for(NetworkInterface intf: interfaces) {
Expand All @@ -4726,15 +4725,14 @@ private static synchronized Collection<InetAddress> getAllAvailableAddresses() {
catch(SocketException e) {
}
// immutable list
CACHED_ADDRESSES = List.copyOf(addresses);
return CACHED_ADDRESSES;
return CACHED_ADDRESSES=List.copyOf(addresses);
}

public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception {
// N.B. bind_addr.isAnyLocalAddress() is not OK
if (bind_addr.isLoopbackAddress())
return;
Collection<InetAddress> addrs=getAllAvailableAddresses(null);
Collection<InetAddress> addrs=getAllAvailableAddresses();
for(InetAddress addr : addrs) {
if(addr.equals(bind_addr))
return;
Expand Down

0 comments on commit b43aba9

Please sign in to comment.