Skip to content

Commit 2ed0b78

Browse files
committed
perf: Improved parameters for working with the database
1 parent 9e1b783 commit 2ed0b78

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

backend/src/app/core/db.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ def __init__(self, settings: Settings) -> None:
2121
2222
:param settings: The application settings object.
2323
"""
24-
self._async_engine: AsyncEngine = create_async_engine(url=settings.sqlalchemy_database_uri)
24+
self._async_engine: AsyncEngine = create_async_engine(
25+
url=settings.sqlalchemy_database_uri,
26+
# --- Recommendations for GENERAL DB (quota ~45 out of 100) ---
27+
pool_size=3, # Number of 'persistent' connections per worker.
28+
max_overflow=2, # Number of 'additional' connections for peaks.
29+
pool_pre_ping=True, # Check the connection before use.
30+
pool_recycle=1700, # Recreating 'old' connections.
31+
pool_timeout=30, # Waiting time for a free connection. (30 seconds)
32+
)
2533
self._async_session_factory: async_sessionmaker[AsyncSession] = async_sessionmaker(
2634
bind=self._async_engine, class_=AsyncSession, expire_on_commit=False
2735
)

backend/tests/app/core/test_db.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# pylint: disable=protected-access
55
# pylint: disable=redefined-outer-name
6+
# pylint: disable=duplicate-code
67

78
from typing import Any
89
from unittest.mock import AsyncMock
@@ -89,7 +90,14 @@ async def test_database_manager_init(
8990
db_manager: DatabaseManager = DatabaseManager(settings=mock_settings)
9091
assert db_manager._async_engine == mock_async_engine
9192
assert db_manager._async_session_factory == mock_async_session_factory
92-
mock_create_async_engine.assert_called_once_with(url=mock_settings.sqlalchemy_database_uri)
93+
mock_create_async_engine.assert_called_once_with(
94+
url=mock_settings.sqlalchemy_database_uri,
95+
pool_size=3,
96+
max_overflow=2,
97+
pool_pre_ping=True,
98+
pool_recycle=1700,
99+
pool_timeout=30,
100+
)
93101
mock_async_sessionmaker.assert_called_once_with(bind=mock_async_engine, class_=AsyncSession, expire_on_commit=False)
94102

95103

0 commit comments

Comments
 (0)