# Gunicorn config for Boundary Commission FastAPI app.
# 3 workers for 4GB RAM; set GUNICORN_WORKERS=2 (or 1) for 2GB to avoid OOM.

import multiprocessing
import os

# Bind (override with -b if needed)
bind = os.environ.get("GUNICORN_BIND", "127.0.0.1:8001")

# Workers: 3 for 4GB RAM; for 2GB set env GUNICORN_WORKERS=2
workers = int(os.environ.get("GUNICORN_WORKERS", "3"))
worker_class = "uvicorn.workers.UvicornWorker"

# Preload app before forking so workers share memory (copy-on-write). Reduces total RAM.
preload = True

# Timeouts (avoid workers killed by gunicorn during slow DB/startup)
timeout = 120
graceful_timeout = 30
keepalive = 5

# Logging
accesslog = "-"
errorlog = "-"
loglevel = os.environ.get("GUNICORN_LOG_LEVEL", "info")

# Process naming
proc_name = "boundary-fastapi"
