szetszwo commented on code in PR #996:
URL: https://github.com/apache/ratis/pull/996#discussion_r1431963346


##########
ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java:
##########
@@ -167,4 +122,33 @@ static String address2String(InetSocketAddress address) {
     }
     return b.append(':').append(address.getPort()).toString();
   }
+
+  /**
+   * Helper class to get free port avoiding randomness.
+   */
+  final class PortAllocator {
+
+    public static final String HOSTNAME = "localhost";
+    public static final int MIN_PORT = 15000;
+    public static final int MAX_PORT = 32000;
+    public static final AtomicInteger NEXT_PORT = new AtomicInteger(MIN_PORT);
+
+    private PortAllocator() {
+      // no instances
+    }
+
+    public static synchronized int getFreePort() {
+      int port = NEXT_PORT.getAndIncrement();
+      if (port > MAX_PORT) {
+        NEXT_PORT.set(MIN_PORT);
+        port = NEXT_PORT.getAndIncrement();
+      }
+      return port;

Review Comment:
   @adoroszlai , Should we try binding it before returning?  Otherwise, the 
port may be already used.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to