Skip to content

Commit

Permalink
Moves the pg database config to settings. Lowers the connection pool …
Browse files Browse the repository at this point in the history
…size
  • Loading branch information
adam-gf committed Dec 18, 2024
1 parent fbe159b commit 8d7c585
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions backend/v2/core/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def get_w3(

class DatabaseSettings(OctantSettings):
db_uri: str = Field(..., alias="db_uri")

pg_pool_size: int = Field(10, alias="sqlalchemy_connection_pool_size")
pg_max_overflow: int = Field(30, alias="sqlalchemy_connection_pool_max_overflow")
pg_pool_timeout: int = 60
pg_pool_recycle: int = 30 * 60 # 30 minutes
pg_pool_pre_ping: bool = True
# TODO other settings of the database

@property
Expand All @@ -60,11 +66,11 @@ def get_sessionmaker(
kw = {}
if "postgresql" in settings.sqlalchemy_database_uri:
kw = {
"pool_size": 100, # Initial pool size (default is 5)
"max_overflow": 10, # Extra connections if pool is exhausted
"pool_timeout": 30, # Timeout before giving up on a connection
"pool_recycle": 3600, # Recycle connections after 1 hour (for long-lived connections)
"pool_pre_ping": True, # Check if the connection is alive before using it
"pool_size": settings.pg_pool_size,
"max_overflow": settings.pg_max_overflow,
"pool_timeout": settings.pg_pool_timeout,
"pool_recycle": settings.pg_pool_recycle,
"pool_pre_ping": settings.pg_pool_pre_ping,
}

engine = create_async_engine(
Expand Down

0 comments on commit 8d7c585

Please sign in to comment.