Skip to content

Commit

Permalink
Add option to set SO_KEEPALIVE on listener socket
Browse files Browse the repository at this point in the history
Closes #3115
  • Loading branch information
cedk committed Jan 6, 2024
1 parent 26aba9e commit 5c2a23e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions THANKS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Brett Randall <[email protected]>
Brian Rosner <[email protected]>
Bruno Bigras <[email protected]>
Caleb Brown <[email protected]>
Cédric Krier <[email protected]>
Chris Adams <[email protected]>
Chris Forbes <[email protected]>
Chris Lamb <[email protected]>
Expand Down
19 changes: 19 additions & 0 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ def sendfile(self):

return True

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

@property
def reuse_port(self):
return self.settings['reuse_port'].get()
Expand Down Expand Up @@ -1029,6 +1033,21 @@ class Sendfile(Setting):
"""


class KeepAlive(Setting):
name = "so_keepalive"
section = "Server Mechanics"
cli = ["--so-keep-alive"]
validator = validate_bool
action = "store_true"
default = False

desc = """\
Set the ``SO_KEEPALIVE`` flag on the listening socket.
.. versionadded:: 21.3
"""


class ReusePort(Setting):
name = "reuse_port"
section = "Server Mechanics"
Expand Down
2 changes: 2 additions & 0 deletions gunicorn/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def __getattr__(self, name):

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

0 comments on commit 5c2a23e

Please sign in to comment.