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
10 changes: 3 additions & 7 deletions .envs/.production/.django
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ 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.)
# --- Opções de Pool de Conexões (django-db-connection-pool) ---

# Tamanho mínimo do pool de conexões
# Exemplo: 10 conexões
# Tamanho do pool de conexões por worker
DB_POOL_SIZE=10

# 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
DB_MAX_OVERFLOW=5

# 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)
Expand Down
8 changes: 3 additions & 5 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
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
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"]["ENGINE"] = 'core.db.backends.postgresql'
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),
'MAX_OVERFLOW': env.int("DB_MAX_OVERFLOW", default=5),
'RECYCLE': env.int("DB_RECYCLE", default=300),
# Adicione outras opções do pool aqui se necessário
'PRE_PING': True, # Validates connections before use to avoid stale/broken connections
}
# CACHES
# ------------------------------------------------------------------------------
Expand Down
Empty file added core/db/__init__.py
Empty file.
Empty file added core/db/backends/__init__.py
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions core/db/backends/postgresql/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Custom PostgreSQL database backend that combines:
- Connection pooling from django-db-connection-pool (SQLAlchemy QueuePool)
- Prometheus metrics from django-prometheus

This backend inherits from both mixins so that connections are managed
by a pool while still being tracked by Prometheus.
"""

from dj_db_conn_pool.backends.postgresql.mixins import PGDatabaseWrapperMixin
from django_prometheus.db.backends.postgresql.base import (
DatabaseWrapper as PrometheusDatabaseWrapper,
)


class DatabaseWrapper(PGDatabaseWrapperMixin, PrometheusDatabaseWrapper):
"""
PostgreSQL backend with connection pooling and Prometheus monitoring.

MRO: PGDatabaseWrapperMixin is first so its get_new_connection() manages
the SQLAlchemy QueuePool. When the pool needs a new connection, it calls
through to PrometheusDatabaseWrapper which adds metrics tracking.
"""

pass
1 change: 1 addition & 0 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gevent==24.2.1 # http://www.gevent.org/
# ==============================================================================

psycopg2-binary==2.9.9 # https://github.com/psycopg/psycopg2
django-db-connection-pool==1.2.6 # https://github.com/altairbow/django-db-connection-pool

# ==============================================================================
# EMAIL
Expand Down