Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

ability to reserve an available http port to use later on #7814

Closed
dylang opened this issue Jun 21, 2014 · 4 comments
Closed

ability to reserve an available http port to use later on #7814

dylang opened this issue Jun 21, 2014 · 4 comments

Comments

@dylang
Copy link

dylang commented Jun 21, 2014

Situation

Your code needs open a port (like to host a web page for your e2e tests) but it has to do some setup before the port can be opened. Some of that setup requires knowing the eventual port (like the test runner), so you find an open one early on and when it's time to open the port for real, you have a port number you want to use.

Problem

The problem is that between your search for an open port and actually opening that port another program did the same search, found that same port to be open, and is now using it. Your program crashes because the port was already in use. The happens frequently enough in your ci environment to drive you to excessive drinking.

Proposed Solution

// find an available port between (and including) 8080 and 9090.
net.reservePort(8080, 9090, function(err, port){
  // The port is now been opened by this process so no other process can try to open it.
  // The port number is stored in an array of "reserved ports".

  // Later on in the code...
  server.listen(port, ...);
  // When this process attempts to use that port, it will:
  // 1/ Find and remove it from the array of "reserved ports"
  // 2/ Close the port
  // 3/ Re-open the port using the passed in values.
  // Perhaps 2 and 3 must be done together in sync to avoid the port being stolen.
});

Notes

  • Reserving a port should always be optional.
  • If the second number is not included then the developer is requesting a specific port to be reserved, such as net.reservePort(80, cb) to make sure this process can get port 80.
  • An error can happen if the port is not available, or when a range is specified, no ports were available in that range.
  • If the developer needs multiple ports than that person should call this function multiple times.
  • The upper bound of reserved ports is the same as open ports, because each reserved port must be open so that no other process can use the port.
@Thomasdezeeuw
Copy link

I think the api looks a bit confusing. To me it looks like the function reserves port 8080 and 9090.

But can't to solve the problem with putting server.listen in a try catch block and just try with another random port if it fails?

@dylang
Copy link
Author

dylang commented Jun 21, 2014

Thanks for the feedback @Thomasdezeeuw.

Maybe my example wasn't good.

Lets say you are using a testing framework Like Protractor that has an option for a port.

You can use server.listen to find a port that is open, but you don't know if Protractor is going to open that port right away or later, after PhantomJS is up and running.

@indutny
Copy link
Member

indutny commented Jun 21, 2014

@dylang I think we are going to introduce uv_listen_stop in libuv eventually, let's keep this issue open and return back to it once it'll be completed.

@rlidwka
Copy link

rlidwka commented Jun 24, 2014

Why not open a port as soon as you can and record all connections to it somewhere? When the listener is ready, simulate all connection events.

If the listener is in another application, let it open another port and proxy all requests from the first port to the actual one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants