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

Make SO_REUSEPORT Optional #1669

Merged
merged 5 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ to enable or disable its usage.
.. versionchanged:: 19.6
added support for the ``SENDFILE`` environment variable

.. _reuse-port:

reuse_port
~~~~~~~~~~

* ``--reuse-port``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation for the new option will be generated automatically from gunicorn/config.py when you run make html in docs/ directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand your comment, @berkerpeksag. I generated this change by running make -C docs html. Am I not supposed to commit settings.rst?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you manually edited it because I didn't see the "Set the SO_REUSEPORT flag on the listening socket." part in the generated file. Now I don't know why make html didn't get the documentation in the desc attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generated it, but then edited it because running it also brought in logconfig_dict (which presumably was omitted previously and should be in its own PR), and also changed chdir, user and group. I must have got the edit wrong. Sorry about that. It's all there now.

* ``False``

.. _chdir:

chdir
Expand Down
16 changes: 16 additions & 0 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def sendfile(self):

return True

@property
def reuse_port(self):
return self.settings['reuse_port'].get()

@property
def paste_global_conf(self):
raw_global_conf = self.settings['raw_paste_global_conf'].get()
Expand Down Expand Up @@ -953,6 +957,18 @@ class Sendfile(Setting):
added support for the ``SENDFILE`` environment variable
"""

class ReusePort(Setting):
name = "reuse_port"
section = "Server Mechanics"
cli = ["--reuse-port"]
validator = validate_bool
action = "store_true"
default = False

desc = """\
Set the ``SO_REUSEPORT`` flag on the listening socket.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add .. versionadded:: 19.8.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always forget this and you always catch it. Thanks! :-D

"""


class Chdir(Setting):
name = "chdir"
Expand Down
3 changes: 2 additions & 1 deletion gunicorn/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def __getattr__(self, name):

def set_options(self, sock, bound=False):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, 'SO_REUSEPORT'): # pragma: no cover
if (self.conf.reuse_port
and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
Expand Down