import multiprocessing
import os

# Gunicorn configuration file for production deployment

# Port to bind (default to 8000, customizable via PORT env)
port = os.getenv("PORT", "8000")
bind = f"0.0.0.0:{port}"

# Worker processes (formula: (2 * CPUs) + 1, customizable via WEB_CONCURRENCY)
workers = int(os.getenv("WEB_CONCURRENCY", multiprocessing.cpu_count() * 2 + 1))

# Worker class specifically for handling FastAPI's async nature
worker_class = "uvicorn.workers.UvicornWorker"

# Maximum time (in seconds) a worker can spend responding to a request before being killed
timeout = int(os.getenv("TIMEOUT", "120"))

# Keep-alive connections timeout
keepalive = 5

# Logging destinations (stdout/stderr for container logging compatibility)
accesslog = "-"
errorlog = "-"
loglevel = os.getenv("LOG_LEVEL", "info")
