Skip to content

Commit b0aebe3

Browse files
✨ feat(db): read SQLAlchemy engine pool configuration from config file (#73) (#74)
Move hardcoded engine pool parameters (pool_size, pool_timeout, pool_recycle, max_overflow) from factory function defaults into PostgresConfig, allowing per-environment tuning via YAML config. Also add pool_pre_ping (default: true) and echo_pool (default: false). Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2d845ef commit b0aebe3

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

config-example.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ postgres:
99
user: "tma_user"
1010
password: "tma_password"
1111
db: "tma_db"
12+
# pool_size: 30
13+
# pool_timeout: 30
14+
# pool_recycle: 3600
15+
# max_overflow: 20
16+
# pool_pre_ping: true
17+
# echo_pool: false
1218

1319
auth:
1420
secret_key: "secret"

src/infrastructure/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class PostgresConfig(BaseModel):
1111
password: str
1212
db: str
1313
echo: bool = False
14+
pool_size: int = 30
15+
pool_timeout: int = 30
16+
pool_recycle: int = 3600
17+
max_overflow: int = 20
18+
pool_pre_ping: bool = True
19+
echo_pool: bool = False
1420

1521
@property
1622
def url(self) -> str:

src/infrastructure/db/factory.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@ def create_pool(db_config: PostgresConfig) -> async_sessionmaker[AsyncSession]:
1414
return create_session_maker(engine)
1515

1616

17-
def create_engine(
18-
db_config: PostgresConfig,
19-
pool_size: int = 30,
20-
pool_timeout: int = 30,
21-
pool_recycle: int = 3600,
22-
max_overflow: int = 20,
23-
) -> AsyncEngine:
17+
def create_engine(db_config: PostgresConfig) -> AsyncEngine:
2418
return create_async_engine(
2519
url=make_url(db_config.url),
2620
echo=db_config.echo,
27-
pool_size=pool_size,
28-
pool_timeout=pool_timeout,
29-
pool_recycle=pool_recycle,
30-
max_overflow=max_overflow,
21+
pool_size=db_config.pool_size,
22+
pool_timeout=db_config.pool_timeout,
23+
pool_recycle=db_config.pool_recycle,
24+
max_overflow=db_config.max_overflow,
25+
pool_pre_ping=db_config.pool_pre_ping,
26+
echo_pool=db_config.echo_pool,
3127
)
3228

3329

tests/unit/infrastructure/test_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,40 @@ def test_missing_required_fields(self):
104104
with pytest.raises(ValidationError):
105105
PostgresConfig()
106106

107+
def test_pool_defaults(self):
108+
config = PostgresConfig(
109+
host="localhost", port=5432, user="user", password="pass", db="db"
110+
)
111+
112+
assert config.pool_size == 30
113+
assert config.pool_timeout == 30
114+
assert config.pool_recycle == 3600
115+
assert config.max_overflow == 20
116+
assert config.pool_pre_ping is True
117+
assert config.echo_pool is False
118+
119+
def test_pool_custom_values(self):
120+
config = PostgresConfig(
121+
host="localhost",
122+
port=5432,
123+
user="user",
124+
password="pass",
125+
db="db",
126+
pool_size=10,
127+
pool_timeout=15,
128+
pool_recycle=1800,
129+
max_overflow=5,
130+
pool_pre_ping=False,
131+
echo_pool=True,
132+
)
133+
134+
assert config.pool_size == 10
135+
assert config.pool_timeout == 15
136+
assert config.pool_recycle == 1800
137+
assert config.max_overflow == 5
138+
assert config.pool_pre_ping is False
139+
assert config.echo_pool is True
140+
107141
@pytest.mark.parametrize(
108142
"port,echo,should_raise",
109143
[

0 commit comments

Comments
 (0)