-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgunicorn.conf.py
50 lines (33 loc) · 1.5 KB
/
gunicorn.conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import multiprocessing
import psutil
# https://docs.gunicorn.org/en/stable/configure.html#configuration-file
wsgi_app = "config.wsgi"
accesslog = "-"
worker_class = "gthread"
# Calculate the number of workers (CPU * 2 + 1)
workers = (multiprocessing.cpu_count() * 2) + 1
# Get total available memory in MiB
memory = psutil.virtual_memory().total // (2**20)
# Estimate max threads per worker (based on memory)
memory_per_worker = memory // workers
# Threads should be at least 2
threads = max(2, memory_per_worker // 128)
# Calculate max_requests (prevent memory leaks)
max_requests = max(300, memory * 50 // 1024)
# Set max_requests_jitter to 5% of max_requests
max_requests_jitter = max_requests // 20
# Base timeout: slightly above the database statement timeout (30s in PostgreSQL settings)
base_timeout = 35
# Scale timeout based on threading: more threads = less per-thread CPU time
timeout = max(30, base_timeout + (threads * 2))
# Graceful timeout: allow extra time for clean shutdown
graceful_timeout = timeout + 10 # Give workers 10 extra seconds to finish tasks
base_keepalive_timeout = (
20 # 20 seconds, slightly shorter than the connection pool timeout
)
# Adjust keep-alive based on number of threads and CPU count (to scale with worker load)
keepalive_timeout = max(
15, base_keepalive_timeout + (threads * 2)
) # Ensure a reasonable value
# Add a max limit to avoid excessively long keep-alive times
keepalive_timeout = min(30, keepalive_timeout) # Max keep-alive timeout capped at 30s