Skip to content

Commit

Permalink
JGRP-2739 Cache addresses on first access
Browse files Browse the repository at this point in the history
  • Loading branch information
pruivo committed Jan 26, 2024
1 parent e64f3e6 commit a38fddc
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/org/jgroups/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4679,26 +4679,31 @@ public static void resetCacheAddresses(boolean reset_interfaces, boolean reset_a

/** Returns all addresses of all interfaces (that are up) that satisfy a given filter (ignored if null) */
public static Collection<InetAddress> getAllAvailableAddresses(Predicate<InetAddress> filter) {
Collection<InetAddress> cached_addresses=CACHED_ADDRESSES;
if(cached_addresses != null && filter == null)
return cached_addresses;
Set<InetAddress> retval=new HashSet<>();
Collection<InetAddress> cached_addresses=getAllAvailableAddresses();
assert cached_addresses != null;
return filter == null ?
cached_addresses :
cached_addresses.stream().filter(filter).collect(Collectors.toList());
}

private static synchronized Collection<InetAddress> getAllAvailableAddresses() {
if (CACHED_ADDRESSES != null) {
return CACHED_ADDRESSES;
}
Set<InetAddress> addresses = new HashSet<>();
try {
List<NetworkInterface> interfaces=getAllAvailableInterfaces();
for(NetworkInterface intf: interfaces) {
if(!isUp(intf) /*!intf.isUp()*/)
continue;
Enumeration<InetAddress> addrs=intf.getInetAddresses();
while(addrs.hasMoreElements()) {
InetAddress addr=addrs.nextElement();
if(filter == null || filter.test(addr))
retval.add(addr);
}
intf.getInetAddresses().asIterator().forEachRemaining(addresses::add);
}
}
catch(SocketException e) {
}
return CACHED_ADDRESSES=retval;
// immutable list
CACHED_ADDRESSES = List.copyOf(addresses);
return CACHED_ADDRESSES;
}

public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception {
Expand Down

0 comments on commit a38fddc

Please sign in to comment.