Skip to content
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 assigning 0 to http/https port for OS assigned port. #414

Merged
merged 1 commit into from Oct 15, 2013
Merged

Support assigning 0 to http/https port for OS assigned port. #414

merged 1 commit into from Oct 15, 2013

Conversation

johnrengelman
Copy link
Contributor

Enables support for assigning server.port=0 and server.port.https=0 and have the OS assign an available port. This is to facilitate testing on CI servers with multiple jobs, allowing Grails to bind to an available port.

Tested manually in the following scenarios:
grails -Dserver.port=0 run-app
grails -Dserver.port.https=0 run-app -https
grails -Dserver.port=0 run-war
grails -Dserver.port.https=0 run-war -https
grails -Dserver.port=0 run-app (with grails.project.fork.run=true)
grails -Dserver.port.https=0 run-app -https (with grails.project.fork.run=true)

Notes

  1. Needed to move the Tomcat kill switch port to server.port - 1 instead of + 1. This is because when running both http and https as 0, they will typically be assigned sequential ports which causes the kill switch and https to use the same port.
  2. Needed to add a new event in the test phase testPhasePrepared. This executes after Tomcat is initialized and will allow plugins (i.e. Geb) to get the actual server ports (instead of 0).

I was hoping that this could go into 2.2.5 since I think there will a number of people who won't be upgrading to 2.3 for a while. This patch should work fine in 2.3.x as well although the changes to the Tomcat plugin will need to be transferred to its repo.

@ColinHarrington
Copy link
Contributor

Ahh, that is what the magic '0' port means.

http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
port:

"The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. This is typically only useful in embedded and testing applications."

@johnrengelman
Copy link
Contributor Author

Yup...This has been supported in embedded Tomcat for a while, but Grails is resetting a value of 0 to be the default port value. This gets rid of the override and then has to do some magic to get the real port back so it can be logged to the console and used to kill the spawned tomcat container.

0 is the standard for binding to a kernel assigned free port AFAIK in C and Java.

@graemerocher
Copy link
Member

Could you please submit a pull request to the tomcat plugin as well? A lot of the code changed here has moved to the Tomcat plugin for 2.3.x https://github.com/grails-plugins/grails-tomcat-plugin

@johnrengelman
Copy link
Contributor Author

@graemerocher Added PR to grails-tomcat-plugin.

graemerocher added a commit that referenced this pull request Oct 15, 2013
Support assigning 0 to http/https port for OS assigned port.
@graemerocher graemerocher merged commit d422bb4 into grails:2.2.x Oct 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants