Skip to content

Commit 85aee1c

Browse files
committed
feat: improve uWSGI options
- Restart a worker after 1000 requests (max-requests) - Restart a worker every hour (max-worker-lifetime) - Restart a worker after 300MB resident memory usage (reload-on-rss) - Restart a worker by force after 60 seconds (worker-reload-mercy) - Use strict mode to catch unknown uWSGI options (strict) - Require an application module to start (need-app) - Avoid multiple interpreters (single-interpreter) - Only use processes for concurrency, so only run one thread per process - Suppress errors about clients closing sockets - Add Harakiri options - Add Sentry-setup - Add uwsgitop settings Refs: PT-1937
1 parent a81d708 commit 85aee1c

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

.prod/uwsgi.ini

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,62 @@
11
[uwsgi]
2+
# https://uwsgi-docs.readthedocs.io/en/latest/Options.html
3+
strict = true # Fail if any option is unknown
4+
25
http-socket = :8000
6+
http-timeout = 60
37
chdir = /app
48
module = palvelutarjotin.wsgi
59
static-map = /static=/var/static
610
static-map = /media=/app/var/media
711
uid = appuser
812
gid = appuser
913

14+
# Settings required for uwsgitop
15+
stats = /tmp/statsock
16+
memory-report = true
17+
18+
master = true
19+
# Enable threads for sentry, see:
20+
# https://docs.sentry.io/clients/python/advanced/#a-note-on-uwsgi
21+
enable-threads = true
22+
py-call-uwsgi-fork-hooks = true
23+
single-interpreter = true
24+
need-app = true
25+
processes = $(UWSGI_PROCESSES)
26+
threads = 1
27+
buffer-size = 65535 # Allow bigger requests that includes a big list of AD-groups. Default is 4096.
28+
29+
# by default uwsgi reloads on SIGTERM instead of terminating
30+
# this makes container slow to stop, so we change it here
31+
die-on-term = true
32+
33+
harakiri = 20
34+
harakiri-graceful-timeout = 5
35+
# Default listen queue is 100
36+
harakiri-queue-threshold = $(UWSGI_PROCESSES)
37+
1038
# Reload workers regularly to keep memory fresh
1139
# and ease potential memory leaks
12-
max-requests = 1000 # Restart workers after this many requests
13-
reload-on-rss = 300 # Restart workers after this much resident memory
14-
worker-reload-mercy = 60 # How long to wait before forcefully killing workers (default is 60)
40+
max-requests = 1000 # Restart workers after this many requests
41+
max-worker-lifetime = 3600 # Restart workers after this many seconds
42+
reload-on-rss = 300 # Restart workers after this much resident memory
43+
worker-reload-mercy = 60 # How long to wait before forcefully killing workers (default is 60)
44+
45+
# Suppress errors about clients closing sockets, happens with nginx as the ingress when
46+
# http pipes are closed before workers has had the time to serve content to the pipe
47+
ignore-sigpipe = true
48+
ignore-write-errors = true
49+
disable-write-exception = true
50+
51+
if-env = SENTRY_DSN
52+
print = Enabled sentry logging for uWSGI
53+
plugin = sentry
54+
alarm = logsentry sentry:dsn=$(SENTRY_DSN),logger=uwsgi.sentry
55+
# Log full queue, segfault and harakiri errors to sentry
56+
alarm-backlog = logsentry
57+
alarm-segfault = logsentry
58+
alarm-log = logsentry HARAKIRI \[core.*\]
59+
endif =
1560

16-
master = 1
17-
processes = 2
18-
threads = 2
1961
route = ^/readiness$ donotlog:
2062
route = ^/healthz$ donotlog:
21-
22-
buffer-size = 65535 # Allow bigger requests that includes a big list of AD-groups. Default is 4096.

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ RUN yum update -y && yum install -y \
1818
nc \
1919
&& pip install -U pip \
2020
&& pip install --no-cache-dir -r /app/requirements.txt \
21-
&& pip install --no-cache-dir -r /app/requirements-prod.txt
21+
&& pip install --no-cache-dir -r /app/requirements-prod.txt \
22+
&& yum clean all
2223

2324
# fatal: detected dubious ownership in repository at '/app'
2425
RUN git config --system --add safe.directory /app

docker-entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ if [[ ! -z "$@" ]]; then
2323
elif [[ "$DEV_SERVER" = "1" ]]; then
2424
python ./manage.py runserver 0.0.0.0:8081
2525
else
26+
export UWSGI_PROCESSES=${UWSGI_PROCESSES:-4}
27+
uwsgi --build-plugin https://github.com/City-of-Helsinki/uwsgi-sentry
2628
uwsgi --ini .prod/uwsgi.ini
2729
fi

0 commit comments

Comments
 (0)