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 sys
import time import time
import traceback import traceback
import socket
from gunicorn.errors import HaltServer, AppImportError from gunicorn.errors import HaltServer, AppImportError
from gunicorn.pidfile import Pidfile from gunicorn.pidfile import Pidfile
@ -151,6 +152,7 @@ class Arbiter:
for fd in os.environ.pop('GUNICORN_FD').split(','): for fd in os.environ.pop('GUNICORN_FD').split(','):
fds.append(int(fd)) fds.append(int(fd))
if not (self.cfg.reuse_port and hasattr(socket, 'SO_REUSEPORT')):
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS]) listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS])
@ -604,6 +606,8 @@ class Arbiter:
try: try:
util._setproctitle("worker [%s]" % self.proc_name) util._setproctitle("worker [%s]" % self.proc_name)
self.log.info("Booting worker with pid: %s", worker.pid) 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) self.cfg.post_fork(self, worker)
worker.init_process() worker.init_process()
sys.exit(0) sys.exit(0)

View File

@ -41,7 +41,7 @@ class GeventWorker(AsyncWorker):
sockets = [] sockets = []
for s in self.sockets: for s in self.sockets:
sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM, sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM,
fileno=s.sock.fileno())) fileno=s.sock.detach()))
self.sockets = sockets self.sockets = sockets
def notify(self): def notify(self):