Skip to content

Commit 9705445

Browse files
committed
Requires a port name for $availableXxxPort() config functions
1 parent cf30727 commit 9705445

File tree

24 files changed

+54
-45
lines changed

24 files changed

+54
-45
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Version 3.10.0 (2019-08-06)
1+
# Version 3.11.0 (2020-08-06)
2+
3+
* [brk] Config functions that find available ports now take a port name as argument so they can return the same port for each evaluation (example: `$availableTcpPort()` becomes `$availableTcpPort('web')`).
4+
5+
# Version 3.10.0 (2020-08-06)
26

37
* [brk] Generic session options moved from `web.server.sessions` to `web.sessions`.
48
* [brk] Static resources options moved from `web.staticResources` to `web.static`.

cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.10.1-SNAPSHOT</version>
17+
<version>3.11.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-cli</artifactId>

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.10.1-SNAPSHOT</version>
17+
<version>3.11.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-core</artifactId>

core/src/main/java/org/seedstack/seed/core/internal/configuration/AvailablePortFunctionHolder.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,49 @@
77
*/
88
package org.seedstack.seed.core.internal.configuration;
99

10+
import org.seedstack.coffig.spi.ConfigFunction;
11+
import org.seedstack.coffig.spi.ConfigFunctionHolder;
12+
13+
import javax.net.ServerSocketFactory;
1014
import java.io.IOException;
1115
import java.net.DatagramSocket;
1216
import java.net.InetAddress;
1317
import java.net.ServerSocket;
14-
import javax.net.ServerSocketFactory;
15-
import org.seedstack.coffig.spi.ConfigFunction;
16-
import org.seedstack.coffig.spi.ConfigFunctionHolder;
18+
import java.util.concurrent.ConcurrentHashMap;
19+
import java.util.concurrent.ConcurrentMap;
1720

1821
public class AvailablePortFunctionHolder implements ConfigFunctionHolder {
22+
private static final ConcurrentMap<String, Integer> TCP_PORTS = new ConcurrentHashMap<>();
23+
private static final ConcurrentMap<String, Integer> UDP_PORTS = new ConcurrentHashMap<>();
1924
private static final int PORT_RANGE_MIN = 49152;
2025
private static final int PORT_RANGE_MAX = 65535;
21-
private static final Object TCP_SYNC = new Object();
22-
private static final Object UDP_SYNC = new Object();
2326

2427
@ConfigFunction
25-
int availableTcpPort() {
26-
synchronized (TCP_SYNC) {
27-
for (int i = PORT_RANGE_MIN; i <= PORT_RANGE_MAX; i++) {
28-
if (isTcpPortAvailable(i)) {
29-
return i;
28+
int availableTcpPort(String name) {
29+
return TCP_PORTS.computeIfAbsent(name, n -> {
30+
synchronized (TCP_PORTS) {
31+
for (int i = PORT_RANGE_MIN; i <= PORT_RANGE_MAX; i++) {
32+
if (isTcpPortAvailable(i)) {
33+
return i;
34+
}
3035
}
36+
throw new IllegalStateException("Unable to find an available TCP port in range " + PORT_RANGE_MIN + "-" + PORT_RANGE_MAX);
3137
}
32-
}
33-
throw new IllegalStateException("Unable to find an available TCP port in range " + PORT_RANGE_MIN + "-" +
34-
PORT_RANGE_MAX);
38+
});
3539
}
3640

3741
@ConfigFunction
38-
int availableUdpPort() {
39-
synchronized (UDP_SYNC) {
40-
for (int i = PORT_RANGE_MIN; i <= PORT_RANGE_MAX; i++) {
41-
if (isUdpPortAvailable(i)) {
42-
return i;
42+
int availableUdpPort(String name) {
43+
return UDP_PORTS.computeIfAbsent(name, n -> {
44+
synchronized (UDP_PORTS) {
45+
for (int i = PORT_RANGE_MIN; i <= PORT_RANGE_MAX; i++) {
46+
if (isUdpPortAvailable(i)) {
47+
return i;
48+
}
4349
}
4450
}
45-
}
46-
throw new IllegalStateException("Unable to find an available UDP port in range " + PORT_RANGE_MIN + "-" +
47-
PORT_RANGE_MAX);
51+
throw new IllegalStateException("Unable to find an available UDP port in range " + PORT_RANGE_MIN + "-" + PORT_RANGE_MAX);
52+
});
4853
}
4954

5055
private boolean isTcpPortAvailable(int port) {

core/src/test/resources/application.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ crypto:
7575
keystore: ssl
7676
keyPassword: changeMe
7777
functions:
78-
availableTcpPort: $availableTcpPort()
79-
availableUdpPort: $availableUdpPort()
78+
availableTcpPort: $availableTcpPort('port1')
79+
availableUdpPort: $availableUdpPort('port2')
8080
randomUuid: $randomUuid()

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>org.seedstack.seed</groupId>
2121
<artifactId>seed</artifactId>
22-
<version>3.10.1-SNAPSHOT</version>
22+
<version>3.11.0-SNAPSHOT</version>
2323
<packaging>pom</packaging>
2424

2525
<properties>

rest/core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.10.1-SNAPSHOT</version>
17+
<version>3.11.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-core</artifactId>

rest/jersey2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.10.1-SNAPSHOT</version>
17+
<version>3.11.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-jersey2</artifactId>

rest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parent>
1414
<groupId>org.seedstack.seed</groupId>
1515
<artifactId>seed</artifactId>
16-
<version>3.10.1-SNAPSHOT</version>
16+
<version>3.11.0-SNAPSHOT</version>
1717
</parent>
1818

1919
<artifactId>seed-rest</artifactId>

rest/specs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parent>
1414
<groupId>org.seedstack.seed</groupId>
1515
<artifactId>seed-rest</artifactId>
16-
<version>3.10.1-SNAPSHOT</version>
16+
<version>3.11.0-SNAPSHOT</version>
1717
</parent>
1818

1919
<artifactId>seed-rest-specs</artifactId>

0 commit comments

Comments
 (0)