You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/06/24 08:04:07 UTC

[dubbo] branch 3.0 updated: Optimize NetUtils#getAvailablePort to avoid port occupation for 3.0 (#8119)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new ca3b173  Optimize NetUtils#getAvailablePort to avoid port occupation for 3.0 (#8119)
ca3b173 is described below

commit ca3b1730432dec2874e0136830d34e40d1cb085e
Author: xiaoheng1 <20...@qq.com>
AuthorDate: Thu Jun 24 16:03:19 2021 +0800

    Optimize NetUtils#getAvailablePort to avoid port occupation for 3.0 (#8119)
---
 .../org/apache/dubbo/common/utils/NetUtils.java    | 31 +++++++++++++---------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index af08299..ec32eb9 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -33,6 +33,7 @@ import java.net.NetworkInterface;
 import java.net.ServerSocket;
 import java.net.SocketException;
 import java.net.UnknownHostException;
+import java.util.BitSet;
 import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
@@ -69,7 +70,7 @@ public class NetUtils {
     private static final int RND_PORT_RANGE = 10000;
 
     // valid port range is (0, 65535]
-    private static final int MIN_PORT = 0;
+    private static final int MIN_PORT = 1;
     private static final int MAX_PORT = 65535;
 
     private static final Pattern ADDRESS_PATTERN = Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$");
@@ -82,25 +83,31 @@ public class NetUtils {
     private static final String SPLIT_IPV4_CHARACTER = "\\.";
     private static final String SPLIT_IPV6_CHARACTER = ":";
 
+    /**
+     * store the used port.
+     * the set used only on the synchronized method.
+     */
+    private static BitSet USED_PORT = new BitSet(65536);
+
     public static int getRandomPort() {
         return RND_PORT_START + ThreadLocalRandom.current().nextInt(RND_PORT_RANGE);
     }
 
-    public static int getAvailablePort() {
-        try (ServerSocket ss = new ServerSocket()) {
-            ss.bind(null);
-            return ss.getLocalPort();
-        } catch (IOException e) {
-            return getRandomPort();
-        }
+    public synchronized static int getAvailablePort() {
+        int randomPort = getRandomPort();
+        return getAvailablePort(randomPort);
     }
 
-    public static int getAvailablePort(int port) {
-        if (port <= 0) {
-            return getAvailablePort();
+    public synchronized static int getAvailablePort(int port) {
+        if (port < MIN_PORT) {
+            return port = MIN_PORT;
         }
         for (int i = port; i < MAX_PORT; i++) {
+            if (USED_PORT.get(i)) {
+                continue;
+            }
             try (ServerSocket ignored = new ServerSocket(i)) {
+                USED_PORT.set(i);
                 return i;
             } catch (IOException e) {
                 // continue
@@ -110,7 +117,7 @@ public class NetUtils {
     }
 
     public static boolean isInvalidPort(int port) {
-        return port <= MIN_PORT || port > MAX_PORT;
+        return port < MIN_PORT || port > MAX_PORT;
     }
 
     public static boolean isValidAddress(String address) {