Skip to content

Commit 9ca19b2

Browse files
author
user
committed
Refactored to use ports 11000 to 14000 instead of random ports
1 parent fc62b82 commit 9ca19b2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

main/src/mill/main/PortManager.scala

+14-9
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,31 @@ object PortManager extends ExternalModule {
99
var portsAllocated = Set.empty[Int]
1010
var portsByName : Map[String, Set[Int]] = Map.empty[String, Set[Int]]
1111

12+
val startingPort = 11000
13+
val endingPort = 14000
14+
1215
def getPorts(tartgetNumberOfPorts : Int): Option[Set[Int]] = {
1316
var i = 0
1417
var ports = Set.empty[Int]
1518

1619
this.synchronized {
17-
for (z <- 1 to 100) {
20+
for (port <- startingPort to endingPort) {
1821

1922
if (i >= tartgetNumberOfPorts) {
2023
return Some(ports)
2124
}
22-
val socket = new ServerSocket(0)
23-
try {
24-
val port = socket.getLocalPort
25-
if (!ports.contains(port) && !portsAllocated.contains(port)) {
26-
ports = ports + port
27-
i += 1
25+
var portGood = false
26+
if (!ports.contains(port) && !portsAllocated.contains(port)) {
27+
try {
28+
val socket = new ServerSocket(port)
29+
portGood = true
30+
socket.close()
2831
}
2932

30-
} finally {
31-
socket.close()
33+
if (portGood){
34+
i = i+1
35+
ports = ports + port
36+
}
3237
}
3338
}
3439
portsAllocated = ports ++ portsAllocated

0 commit comments

Comments
 (0)