Merge pull request #2938 from Affirm/reuse-port-fix

Fix reuse-port to balance requests across Gunicorn workers
This commit is contained in:
Benoit Chesneau 2024-10-25 11:36:15 +02:00 committed by GitHub
commit bacbf8aa51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import signal
import sys
import time
import traceback
import socket
from gunicorn.errors import HaltServer, AppImportError
from gunicorn.pidfile import Pidfile
@ -151,7 +152,8 @@ class Arbiter:
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")
@ -604,6 +606,8 @@ class Arbiter:
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)

View File

@ -41,7 +41,7 @@ class GeventWorker(AsyncWorker):
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):