Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCT-2282: Lowering the db connection pool size & adding settings for it #594

Merged
Merged
Changes from 1 commit
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
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
Loading