Skip to content

Commit d69ea4c

Browse files
committed
feat(config): Add database connection pool configuration options
1 parent 4b3758e commit d69ea4c

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

app/core/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class Settings(BaseSettings):
104104
DB_PASS: str = ""
105105
DB_NAME: str = "app"
106106
DB_ECHO: bool = False
107+
DB_ECHO_POOL: bool = False
108+
DB_POOL_SIZE: int = 5
109+
DB_MAX_OVERFLOW: int = 10
107110

108111
# SQLAlchemy engine settings
109112
# The size of the pool to be maintained, defaults to 10

app/core/lifespan.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ def _setup_db(app: FastAPI) -> None: # pragma: no cover
4040
4141
:param app: fastAPI application.
4242
"""
43-
engine = create_async_engine(str(settings.db_url), echo=settings.DB_ECHO)
43+
engine = create_async_engine(
44+
str(settings.db_url),
45+
echo=settings.DB_ECHO,
46+
echo_pool=settings.DB_ECHO_POOL,
47+
pool_size=settings.DB_POOL_SIZE,
48+
max_overflow=settings.DB_MAX_OVERFLOW,
49+
)
4450
session_factory = async_sessionmaker(
4551
engine,
4652
# See https://fastapi-users.github.io/fastapi-users/latest/configuration/databases/sqlalchemy/#asynchronous-driver
4753
expire_on_commit=False,
54+
autoflush=False,
55+
# autocommit=False,
4856
)
4957
app.state.db_engine = engine
5058
app.state.db_session_factory = session_factory

0 commit comments

Comments
 (0)