Closed ciderale closed 3 years ago
Hi, thanks for the PR!
Scanning ports to find one open usually has two big drawbacks: it is not atomic (the port may get assigned by the time the registry starts) and it's somewhat slow. I would rather use port 0
and have the registry report the actual port the OS gave it. If you need this ASAP @ciderale I'm happy to merge; otherwise we could look into using port 0?
Thanks for the quick reply.
You are right, the operation is not atomic. Concerning speed, it is not a port scanning if I understand that correctly and it is rather fast. lsof -i -P -n 0.02s user 0.08s system 90% cpu 0.106 total
.
I shortly thought about port 0
, but I didn't want to modify the registry. I also don't know, if that works everywhere, so I went for listing the ports instead. But yes, that is probably the cleaner solution.
I don't need it asap. I was investigating with nix&npm and your package works nicely. I was looking into separating the creation of node_modules
and the actual package build (as mentioned elsewhere afaik), and this was the first little thing to do.
@nmattia I made an update that now binds to a random port.
Unfortunately, using port 0
did not work. While it listened on a random port, npm was not able to get the data from the registry without errors. I'm not sure what the reason is. Therefore, I used the Warp.openFreePort
to achieve the goal. This seems to work well. (tested with the "deckdeckgo-starter" derivation on a mac)
Another challenge was to get the port that has been allocated. I first thought to retrieve it somehow from stdout. But that seems not that trivial since the application continues to run. As we anyway have to wait for the registry to start, I don't think retrieving the information with lsof
is much different from nc
.
Let me know what you think about the new solution.
Closing in favor of https://github.com/nmattia/napalm/pull/24, thanks for the inspiration!
This PR introduces a script to select an unused port for the registry. This should essentially address #10.
The solution has a dependency on
lsof
, but sincenc
is used already, I think that should be okay. Moreover, the registry is terminated via atrap ... EXIT
, so it is shutdown even in case of unexpected errors.