Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions .envs/.production/.django
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,17 @@ FETCH_DATA_TIMEOUT=10
# Exemplo: 10 segundos
DB_CONNECT_TIMEOUT=10

# --- Opções de Pool de Conexões ---
# (Aplicável se você estiver usando uma biblioteca de pool de conexões integrada ao Django,
# como django-db-connection-pool, que reconheça POOL_OPTIONS.)
# --- Gunicorn ---
# Number of gunicorn worker processes
# GUNICORN_WORKERS=3

# Tamanho mínimo do pool de conexões
# Exemplo: 10 conexões
DB_POOL_SIZE=10
# Max simultaneous connections per worker (gevent greenlets)
# GUNICORN_WORKER_CONNECTIONS=1000

# Número máximo de conexões extras que o pool pode criar em caso de pico
# Exemplo: 20 conexões de "overflow"
DB_MAX_OVERFLOW=20

# Tempo máximo (em segundos) que uma conexão pode viver antes de ser reciclada
# Ajuda a evitar conexões "stale" ou "bad". Exemplo: 300 segundos (5 minutos)
DB_RECYCLE=300
# Django CONN_MAX_AGE: seconds to keep DB connections alive (0 = close after each request).
# Use 0 (default) with gevent to prevent connection exhaustion
# (each greenlet holds its own connection when CONN_MAX_AGE > 0).
# CONN_MAX_AGE=0


# ---- profiling
Expand Down
14 changes: 13 additions & 1 deletion compose/production/django/start
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ if compress_enabled; then
# NOTE this command will fail if django-compressor is disabled
python /app/manage.py compress
fi
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app --timeout 1000 --workers 3 --worker-connections=1000 --worker-class=gevent

GUNICORN_WORKERS=${GUNICORN_WORKERS:-3}
GUNICORN_WORKER_CONNECTIONS=${GUNICORN_WORKER_CONNECTIONS:-1000}
GUNICORN_WORKER_CLASS=${GUNICORN_WORKER_CLASS:-gevent}
GUNICORN_TIMEOUT=${GUNICORN_TIMEOUT:-1000}

/usr/local/bin/gunicorn config.wsgi \
--bind 0.0.0.0:5000 \
--chdir=/app \
--timeout "$GUNICORN_TIMEOUT" \
--workers "$GUNICORN_WORKERS" \
--worker-connections="$GUNICORN_WORKER_CONNECTIONS" \
--worker-class="$GUNICORN_WORKER_CLASS"
15 changes: 6 additions & 9 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
# ------------------------------------------------------------------------------
DATABASES["default"] = env.db("DATABASE_URL") # noqa F405
DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=0) or env.int("DJANGO_CONN_MAX_AGE", default=60) # noqa F405
# CONN_MAX_AGE=0 closes DB connections after each request, which is recommended
# when using gevent workers to prevent connection exhaustion — each greenlet
# holds its own persistent connection when CONN_MAX_AGE > 0.
# Set CONN_MAX_AGE env var to a positive integer (e.g. 60) only if using a
# connection pooler like PgBouncer in front of PostgreSQL.
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=0) # noqa F405
DATABASES["default"]["CONN_HEALTH_CHECKS"] = env.bool('DJANGO_CONN_HEALTH_CHECKS', True)
DATABASES["default"]["ENGINE"] = 'django_prometheus.db.backends.postgresql'
# Melhoria: Usando variáveis de ambiente para OPTIONS e POOL_OPTIONS com defaults
DATABASES["default"]["OPTIONS"] = {
"connect_timeout": env.int("DB_CONNECT_TIMEOUT", default=10),
# Adicione outras opções de conexão aqui se necessário
}
DATABASES["default"]["POOL_OPTIONS"] = {
'POOL_SIZE': env.int("DB_POOL_SIZE", default=10),
'MAX_OVERFLOW': env.int("DB_MAX_OVERFLOW", default=20),
'RECYCLE': env.int("DB_RECYCLE", default=300),
# Adicione outras opções do pool aqui se necessário
}
# CACHES
# ------------------------------------------------------------------------------
Expand Down