I am looking for a programming technique that would allow a “supervisor” parent process to reserve a port number for a server child process that will be activated in the near future.
Example
- The parent supervisor decides that 10391 is available and can be used for listening.
- The parent supervisor somehow reserves or binds port 10391.
- The parent supervisor spawns a child and passes 10391 as an environment variable.
- The child is guaranteed to be able to bind ad listen on 10391.
The question stems from Heroku; I used Heroku for a while and I know that the listen port is passed as an environment variable to the app. The app must listen on that port. See also this official sample. How can Heroku guarantee that the port will be available by the time the child process tries to bind it?
If I were to implement something like this, I would probably design it in a way that the supervisor actually binds a port, and then passes an open listenable socket (that is, a file descriptor) to the child.
But I am kinda curious if there is a way to do this by passing the port number (like Heroku does).