diff --git a/docs/source/settings.rst b/docs/source/settings.rst index 248f061e..dafa9869 100644 --- a/docs/source/settings.rst +++ b/docs/source/settings.rst @@ -428,6 +428,18 @@ to enable or disable its usage. .. versionchanged:: 19.6 added support for the ``SENDFILE`` environment variable +.. _reuse-port: + +reuse_port +~~~~~~~~~~ + +* ``--reuse-port`` +* ``False`` + +Set the ``SO_REUSEPORT`` flag on the listening socket. + +.. versionadded:: 19.8 + .. _chdir: chdir diff --git a/gunicorn/config.py b/gunicorn/config.py index cab56ea5..dfba8a45 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -204,6 +204,10 @@ class Config(object): 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() @@ -954,6 +958,21 @@ class Sendfile(Setting): """ +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. + + .. versionadded:: 19.8 + """ + + class Chdir(Setting): name = "chdir" section = "Server Mechanics" diff --git a/gunicorn/sock.py b/gunicorn/sock.py index cc6c589a..bbb32aaa 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -39,7 +39,8 @@ class BaseSocket(object): 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: