From 31308b9b06ba252ef95325dcf117823329ba73e3 Mon Sep 17 00:00:00 2001 From: Juha Louhiranta Date: Mon, 8 Jan 2024 12:59:56 +0200 Subject: [PATCH] chore: update uwsgi configuration Allows controlling amount of uwsgi processes using environment variables. Also copies some good configuration from other projects. Refs: HP-2134 --- .prod/uwsgi.ini | 28 +++++++++++++++++++++++++--- docker-entrypoint.sh | 3 +++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.prod/uwsgi.ini b/.prod/uwsgi.ini index 3aed9e3f..d198ed44 100644 --- a/.prod/uwsgi.ini +++ b/.prod/uwsgi.ini @@ -1,4 +1,5 @@ [uwsgi] +# https://uwsgi-docs.readthedocs.io/en/latest/Options.html http-socket = :8000 chdir = /app mount = $(APP_URL_PATH)=tunnistamo/wsgi.py @@ -7,7 +8,28 @@ static-map = $(MEDIA_URL_ROOTLESS)=$(MEDIA_ROOT) static-map = $(STATIC_URL_ROOTLESS)=$(STATIC_ROOT) uid = appuser gid = appuser +enable-threads = true +master = true +processes = $(UWSGI_THREADS) +threads = $(UWSGI_THREADS) + +# by default uwsgi reloads on SIGTERM instead of terminating +# this makes container slow to stop, so we change it here +die-on-term = true +harakiri = 20 +harakiri-graceful-timeout = 5 +# Default listen queue is 100 +harakiri-queue-threshold = 80 buffer-size = 32768 -master = 1 -processes = 2 -threads = 2 + +# Reload workers regularly to keep memory fresh +# and ease potential memory leaks +max-requests = 1000 # Restart workers after this many requests +reload-on-rss = 300 # Restart workers after this much resident memory +worker-reload-mercy = 60 # How long to wait before forcefully killing workers (default is 60) + +# Suppress errors about clients closing sockets, happens with nginx as the ingress when +# http pipes are closed before workers has had the time to serve content to the pipe +ignore-sigpipe = true +ignore-write-errors = true +disable-write-exception = true diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index b6b815a7..d2c8b64c 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -92,6 +92,9 @@ if [[ ! -z "$@" ]]; then elif [[ "$DEV_SERVER" = "1" ]]; then python ./manage.py runserver 0.0.0.0:8000 else + export UWSGI_THREADS=${UWSGI_THREADS:-1} + export UWSGI_PROCESSES=${UWSGI_PROCESSES:-4} + # We want to have a *_URL environment variables configure # both Django static/media files URL generation # and the corresponding file serving in uWSGI.