Skip to content

Commit

Permalink
Merge pull request #2938 from Affirm/reuse-port-fix
Browse files Browse the repository at this point in the history
Fix reuse-port to balance requests across Gunicorn workers
  • Loading branch information
benoitc authored Oct 25, 2024
2 parents 903792f + b0115b9 commit bacbf8a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import time
import traceback
import socket

from gunicorn.errors import HaltServer, AppImportError
from gunicorn.pidfile import Pidfile
Expand Down Expand Up @@ -151,7 +152,8 @@ def start(self):
for fd in os.environ.pop('GUNICORN_FD').split(','):
fds.append(int(fd))

self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
if not (self.cfg.reuse_port and hasattr(socket, 'SO_REUSEPORT')):
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)

listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS])
self.log.debug("Arbiter booted")
Expand Down Expand Up @@ -604,6 +606,8 @@ def spawn_worker(self):
try:
util._setproctitle("worker [%s]" % self.proc_name)
self.log.info("Booting worker with pid: %s", worker.pid)
if self.cfg.reuse_port:
worker.sockets = sock.create_sockets(self.cfg, self.log)
self.cfg.post_fork(self, worker)
worker.init_process()
sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/workers/ggevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def patch(self):
sockets = []
for s in self.sockets:
sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM,
fileno=s.sock.fileno()))
fileno=s.sock.detach()))
self.sockets = sockets

def notify(self):
Expand Down

0 comments on commit bacbf8a

Please sign in to comment.