Skip to content

Commit

Permalink
OCT-2282: Lowering the db connection pool size & adding settings for …
Browse files Browse the repository at this point in the history
…it (#594)

## Description

To better handle the pool size of postgres we lowered the pool size from
100 and moved params to dedicated config.
The issue probably is due to the fact that pg has (default) 100
connection and pool size of sqlalchemy was equal to it, causing
overflows / connection drops because of reached limit.
  • Loading branch information
adam-gf authored Dec 18, 2024
1 parent fbe159b commit 95d249f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ProdConfig(Config):
os.getenv("SQLALCHEMY_CONNECTION_POOL_SIZE", 10)
)
SQLALCHEMY_CONNECTION_POOL_MAX_OVERFLOW = int(
os.getenv("SQLALCHEMY_CONNECTION_POOL_MAX_OVERFLOW", 100)
os.getenv("SQLALCHEMY_CONNECTION_POOL_MAX_OVERFLOW", 0)
)
SQLALCHEMY_DATABASE_URI = os.getenv("DB_URI")
SQLALCHEMY_ENGINE_OPTIONS = {
Expand Down
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(0, 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 95d249f

Please sign in to comment.