Changesets · Branches · Tags · Compare
| Changeset | 21e3aa7eaedaa6d41dd5b4b2c7338e72f8d19104d03b366c0bb8374aab7cd59d |
|---|---|
| Author | ndr_brt <andrea.bertagnolli@gmail.com> · 2024-07-17 17:04 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-17 17:04 +02:00 |
| Parents | 2f3034f0fbdf |
2 files changed
modified core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java +23 −27
| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | |
| 17 | 17 | import java.io.IOException; |
| 18 | 18 | import java.net.ServerSocket; |
| 19 | +import java.util.HashSet; | |
| 19 | 20 | import java.util.Random; |
| 20 | - | |
| 21 | -import static java.lang.String.format; | |
| 21 | +import java.util.Set; | |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Utilities for assigning ports. |
| @@ -26,22 +26,22 @@ | ||
| 26 | 26 | public final class Ports { |
| 27 | 27 | public static final int MAX_TCP_PORT = 65_535; |
| 28 | 28 | private static final Random RANDOM = new Random(); |
| 29 | + private static final Set<Integer> ALREADY_RETURNED = new HashSet<>(); | |
| 29 | 30 | |
| 30 | 31 | /** |
| 31 | - * Gets a free port in the range 1024 - 65535 by trying them in ascending order. | |
| 32 | + * Gets a free port in the range 1024 - 65535 by trying them randomly. | |
| 32 | 33 | * |
| 33 | - * @return the first free port | |
| 34 | + * @return the lower bound. | |
| 34 | 35 | * @throws IllegalArgumentException if no free port is available |
| 35 | 36 | */ |
| 36 | 37 | public static int getFreePort() { |
| 37 | - var rnd = 1024 + RANDOM.nextInt(MAX_TCP_PORT - 1024); | |
| 38 | - return getFreePort(rnd); | |
| 38 | + return getFreePort(1024); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | - * Gets a free port in the range lowerBound - 65535 by trying them in ascending order. | |
| 42 | + * Gets a free port in the range lowerBound - 65535 by trying them randomly. | |
| 43 | 43 | * |
| 44 | - * @return the first free port | |
| 44 | + * @return the lower bound. | |
| 45 | 45 | * @throws IllegalArgumentException if no free port is available |
| 46 | 46 | */ |
| 47 | 47 | public static int getFreePort(int lowerBound) { |
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | - * Gets a free port in the range lowerBound - upperBound by trying them in ascending order. | |
| 55 | + * Gets a free port in the range lowerBound - upperBound randomly. Will not return ports already returned. | |
| 56 | 56 | * |
| 57 | 57 | * @return the first free port |
| 58 | 58 | * @throws IllegalArgumentException if no free port is available or if the bounds are invalid. |
| @@ -65,25 +65,21 @@ | ||
| 65 | 65 | if (upperBound > MAX_TCP_PORT) { |
| 66 | 66 | throw new IllegalArgumentException("Upper bound must be < " + MAX_TCP_PORT); |
| 67 | 67 | } |
| 68 | - var port = lowerBound; | |
| 69 | - boolean found = false; | |
| 70 | - | |
| 71 | - while (!found && port <= upperBound) { | |
| 72 | - try (ServerSocket serverSocket = new ServerSocket(port)) { | |
| 73 | - serverSocket.setReuseAddress(true); | |
| 74 | - port = serverSocket.getLocalPort(); | |
| 75 | - | |
| 76 | - found = true; | |
| 77 | - } catch (IOException e) { | |
| 78 | - found = false; | |
| 79 | - port++; | |
| 80 | - } | |
| 81 | - } | |
| 82 | 68 | |
| 83 | - if (!found) { | |
| 84 | - throw new IllegalArgumentException(format("No free ports in the range [%d - %d]", lowerBound, upperBound)); | |
| 85 | - } | |
| 86 | - return port; | |
| 69 | + do { | |
| 70 | + var tryPort = lowerBound + RANDOM.nextInt(upperBound - lowerBound); | |
| 71 | + if (!ALREADY_RETURNED.contains(tryPort)) { | |
| 72 | + ALREADY_RETURNED.add(tryPort); | |
| 73 | + | |
| 74 | + try (var serverSocket = new ServerSocket(tryPort)) { | |
| 75 | + serverSocket.setReuseAddress(true); | |
| 76 | + | |
| 77 | + return tryPort; | |
| 78 | + } catch (IOException ignored) { | |
| 79 | + // port already used by external service, try another one | |
| 80 | + } | |
| 81 | + } | |
| 82 | + } while (true); | |
| 87 | 83 | } |
| 88 | 84 | |
| 89 | 85 | private Ports() { |
modified core/common/lib/util-lib/src/test/java/org/eclipse/edc/util/io/PortsTest.java +14 −2
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import java.io.IOException; |
| 20 | 20 | import java.net.ServerSocket; |
| 21 | 21 | |
| 22 | +import static java.util.stream.IntStream.range; | |
| 22 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 23 | 24 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 24 | 25 | |
| @@ -51,12 +52,23 @@ | ||
| 51 | 52 | assertThatThrownBy(() -> Ports.getFreePort(5000, 0)).isInstanceOf(IllegalArgumentException.class); |
| 52 | 53 | } |
| 53 | 54 | |
| 55 | + @Test | |
| 56 | + void getFreePortAvoidDuplicated() { | |
| 57 | + var lowerBound = 6000; | |
| 58 | + var portsToBeGenerated = 10; | |
| 59 | + var ports = range(0, portsToBeGenerated) | |
| 60 | + .mapToObj(i -> Ports.getFreePort(lowerBound, lowerBound + portsToBeGenerated)) | |
| 61 | + .distinct().toList(); | |
| 62 | + | |
| 63 | + assertThat(ports.size()).isEqualTo(portsToBeGenerated); | |
| 64 | + } | |
| 65 | + | |
| 54 | 66 | @Test |
| 55 | 67 | void getFreePort_whenOccupied() throws IOException { |
| 56 | 68 | var port = Ports.getFreePort(MIN_PORT); |
| 57 | 69 | |
| 58 | 70 | try (var socket = new ServerSocket(port)) { |
| 59 | - assertThat(Ports.getFreePort(MIN_PORT)).describedAs("Next free port").isGreaterThan(socket.getLocalPort()); | |
| 71 | + assertThat(Ports.getFreePort(MIN_PORT)).describedAs("Next free port").isNotEqualTo(socket.getLocalPort()); | |
| 60 | 72 | } |
| 61 | 73 | } |
| 62 | -} | |
| No newline at the end of the file | ||
| 74 | +} |