-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for dynamic port allocation #128
Comments
This is definitely someting that seems useful. If I have some spare time over the coming months I will look into putting something together. In the meantime feel free to try something yourself. This would need to be stored and returned in the Line 140 in 0db55ea
Cheers |
@fergusstrange thanks a lot for the positive and welcoming feedback. I started implementing the feature in the place you suggested and even though it is certainly possible it looks out of place. Config looks like it is intended as a container for configurations, it does not compute anything right now. Lack of error handling possibilities due to chaining and the missing logger makes the code ugly. See for yourself:
A cleaner solution would be to resolve the real port at a later time inside embedded_postgres.go, for instance inside Start(). There we would have proper error handling and a logger, which would make the implementation much cleaner. Either way is fine for me. Just let me know and I'll get a PR ready. |
I also forgot to mention a technical detail of either of those solutions. What is happening in my example is, that we ask the the OS to dynamically allocate a port and immediately close it. After that we ask Postgers to start using that very same port. In the meantime the resource is freed ant the OS may allocate it for some other process. It is possible to end up with a race condition here. Race conditions can not happen if the process who is using the port is also the same who allocated it dynamically. In this case the solution without race conditions, would be to ask postgres to allocate a port dynamically and later on find out which port was allocated by it. This would overly complicate things for this minor feature. I guess it would be enough to document the problem and implement a simpler solution. |
Hey, completely agree with all the points above.
If possible you should try implementing around where the You'll also need to ensure |
To be able to start my postgres in a test environment where other tests for other services may run in parallel I'd like to dynamically allocate the port and retrieve the actually allocated port when postgres is started. net.Listen() already supports dynamic port allocation. This is done by passing the port number 0. When I tried using port 0 with embedded-postgres I got this error message:
The text was updated successfully, but these errors were encountered: