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


##########
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:
   Thanks for the suggestion, updated the patch, and added test case for this.
   
   Also restored the two original methods as `@Deprecated` (but now they use 
the new port assignment).



-- 
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