diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-02 15:32:37 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-02 15:32:37 +0900 |
| commit | 78d48f99f93b6738f4239c3f4bb718d3aa5cbb56 (patch) | |
| tree | 419a66a59c8032321b2982155f1a17efd5355deb /shared/src | |
| parent | 0e7fd5059e8a813ccffe2c376b1ff43898b4d966 (diff) | |
feat: add DB connection pooling with configurable pool_size, overflow, recycle
Diffstat (limited to 'shared/src')
| -rw-r--r-- | shared/src/shared/config.py | 3 | ||||
| -rw-r--r-- | shared/src/shared/db.py | 19 |
2 files changed, 20 insertions, 2 deletions
diff --git a/shared/src/shared/config.py b/shared/src/shared/config.py index b6ccebd..b6b9d69 100644 --- a/shared/src/shared/config.py +++ b/shared/src/shared/config.py @@ -9,6 +9,9 @@ class Settings(BaseSettings): alpaca_paper: bool = True # Use paper trading by default redis_url: str = "redis://localhost:6379" database_url: str = "postgresql://trading:trading@localhost:5432/trading" + db_pool_size: int = 20 + db_max_overflow: int = 10 + db_pool_recycle: int = 3600 log_level: str = "INFO" risk_max_position_size: float = 0.1 risk_stop_loss_pct: float = 5.0 diff --git a/shared/src/shared/db.py b/shared/src/shared/db.py index 9cc8686..e7cad92 100644 --- a/shared/src/shared/db.py +++ b/shared/src/shared/db.py @@ -36,9 +36,24 @@ class Database: self._engine = None self._session_factory = None - async def connect(self) -> None: + async def connect( + self, + pool_size: int = 20, + max_overflow: int = 10, + pool_recycle: int = 3600, + ) -> None: """Create the async engine, session factory, and all tables.""" - self._engine = create_async_engine(self._database_url) + if self._database_url.startswith("sqlite"): + # SQLite doesn't support pooling options + self._engine = create_async_engine(self._database_url) + else: + self._engine = create_async_engine( + self._database_url, + pool_pre_ping=True, + pool_size=pool_size, + max_overflow=max_overflow, + pool_recycle=pool_recycle, + ) self._session_factory = async_sessionmaker(self._engine, expire_on_commit=False) async with self._engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) |
