Browse Source

Avoid IpStringToBytes Called Twice (#97115)

Co-authored-by: Iraklis Psaroudakis <kingherc@gmail.com>
GrandFisher 2 năm trước cách đây
mục cha
commit
5fbdf7954d

+ 1 - 1
server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java

@@ -76,7 +76,7 @@ public class DiscoveryNodeFilters {
 
     private static boolean matchByIP(String[] values, @Nullable String hostIp, @Nullable String publishIp) {
         for (String ipOrHost : values) {
-            String value = InetAddresses.isInetAddress(ipOrHost) ? NetworkAddress.format(InetAddresses.forString(ipOrHost)) : ipOrHost;
+            String value = InetAddresses.getIpOrHost(ipOrHost);
             boolean matchIp = Regex.simpleMatch(value, hostIp) || Regex.simpleMatch(value, publishIp);
             if (matchIp) {
                 return matchIp;

+ 8 - 0
server/src/main/java/org/elasticsearch/common/network/InetAddresses.java

@@ -35,6 +35,14 @@ public class InetAddresses {
         return ipStringToBytes(ipString) != null;
     }
 
+    public static String getIpOrHost(String ipString) {
+        byte[] bytes = ipStringToBytes(ipString);
+        if (bytes == null) { // is not InetAddress
+            return ipString;
+        }
+        return NetworkAddress.format(bytesToInetAddress(bytes));
+    }
+
     private static byte[] ipStringToBytes(String ipString) {
         // Make a first pass to categorize the characters in this string.
         boolean hasColon = false;