Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ postgres:
user: "tma_user"
password: "tma_password"
db: "tma_db"
# pool_size: 30
# pool_timeout: 30
# pool_recycle: 3600
# max_overflow: 20
# pool_pre_ping: true
# echo_pool: false

auth:
secret_key: "secret"
Expand Down
6 changes: 6 additions & 0 deletions src/infrastructure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class PostgresConfig(BaseModel):
password: str
db: str
echo: bool = False
pool_size: int = 30
pool_timeout: int = 30
pool_recycle: int = 3600
max_overflow: int = 20
pool_pre_ping: bool = True
echo_pool: bool = False

@property
def url(self) -> str:
Expand Down
18 changes: 7 additions & 11 deletions src/infrastructure/db/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@ def create_pool(db_config: PostgresConfig) -> async_sessionmaker[AsyncSession]:
return create_session_maker(engine)


def create_engine(
db_config: PostgresConfig,
pool_size: int = 30,
pool_timeout: int = 30,
pool_recycle: int = 3600,
max_overflow: int = 20,
) -> AsyncEngine:
def create_engine(db_config: PostgresConfig) -> AsyncEngine:
return create_async_engine(
url=make_url(db_config.url),
echo=db_config.echo,
pool_size=pool_size,
pool_timeout=pool_timeout,
pool_recycle=pool_recycle,
max_overflow=max_overflow,
pool_size=db_config.pool_size,
pool_timeout=db_config.pool_timeout,
pool_recycle=db_config.pool_recycle,
max_overflow=db_config.max_overflow,
pool_pre_ping=db_config.pool_pre_ping,
echo_pool=db_config.echo_pool,
)


Expand Down
34 changes: 34 additions & 0 deletions tests/unit/infrastructure/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ def test_missing_required_fields(self):
with pytest.raises(ValidationError):
PostgresConfig()

def test_pool_defaults(self):
config = PostgresConfig(
host="localhost", port=5432, user="user", password="pass", db="db"
)

assert config.pool_size == 30
assert config.pool_timeout == 30
assert config.pool_recycle == 3600
assert config.max_overflow == 20
assert config.pool_pre_ping is True
assert config.echo_pool is False

def test_pool_custom_values(self):
config = PostgresConfig(
host="localhost",
port=5432,
user="user",
password="pass",
db="db",
pool_size=10,
pool_timeout=15,
pool_recycle=1800,
max_overflow=5,
pool_pre_ping=False,
echo_pool=True,
)

assert config.pool_size == 10
assert config.pool_timeout == 15
assert config.pool_recycle == 1800
assert config.max_overflow == 5
assert config.pool_pre_ping is False
assert config.echo_pool is True

@pytest.mark.parametrize(
"port,echo,should_raise",
[
Expand Down
Loading