get next free port instead of waiting for ports to close

This commit is contained in:
Eric 2023-11-29 10:30:34 +11:00
parent 428f6d68fb
commit 075e89de2b
No known key found for this signature in database
2 changed files with 20 additions and 3 deletions

View File

@ -104,6 +104,20 @@ proc withLogFile*[T: Config](
startConfig.logFile = logToFile
return startConfig
proc nextFreePort(startPort: int): Future[int] {.async.} =
let cmd = when defined(windows):
"netstat -ano | findstr :"
else:
"lsof -ti:"
var port = startPort
while true:
let portInUse = await execCommandEx(cmd & $port)
if portInUse.stdOutput == "":
echo "port ", port, " is free"
return port
else:
inc port
template multinodesuite*(name: string, body: untyped) =
ethersuite name:
@ -185,12 +199,12 @@ template multinodesuite*(name: string, body: untyped) =
var options = config.cliOptions.map(o => $o)
.concat(@[
"--api-port=" & $(8080 + nodeIdx),
"--api-port=" & $ await nextFreePort(8080 + nodeIdx),
"--data-dir=" & datadir,
"--nat=127.0.0.1",
"--listen-addrs=/ip4/127.0.0.1/tcp/0",
"--disc-ip=127.0.0.1",
"--disc-port=" & $(8090 + nodeIdx),
"--disc-port=" & $ await nextFreePort(8090 + nodeIdx),
"--eth-account=" & $accounts[nodeIdx]])
let node = await startNode(options, config.debugEnabled)

View File

@ -120,9 +120,12 @@ method stop*(node: NodeProcess) {.base, async.} =
if err =? node.process.terminate().errorOption:
echo "ERROR terminating node process, error code: ", err
echo "stopping codex client"
await node.closeAndWaitClient().wait(5.seconds)
discard await node.process.waitForExit(timeout=5.seconds)
await node.process.closeWait()
if client =? node.client:
client.close()
node.client = none CodexClient
# await node.closeAndWaitClient().wait(5.seconds)
node.process = nil
echo "code node and client stopped"