diff --git a/src/core/config.py b/src/core/config.py index 3cf5575..fe4941c 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -1,3 +1,5 @@ +import asyncio + from dotenv import find_dotenv, load_dotenv from pydantic_settings import BaseSettings @@ -13,3 +15,5 @@ class Settings(BaseSettings): settings = Settings() + +DB_SEMAPHORE = asyncio.Semaphore(100) diff --git a/src/core/fastapi/dependencies/session.py b/src/core/fastapi/dependencies/session.py index 08f5092..ecdc4ae 100644 --- a/src/core/fastapi/dependencies/session.py +++ b/src/core/fastapi/dependencies/session.py @@ -1,9 +1,11 @@ from sqlalchemy.ext.asyncio import AsyncSession +from src.core.config import DB_SEMAPHORE from src.core.database.database import SessionFactory # Dependency to get an asynchronous session async def get_session() -> AsyncSession: - async with SessionFactory() as session: - yield session + async with DB_SEMAPHORE: # Acquire semaphore before accessing the session + async with SessionFactory() as session: + yield session # Provide the session to the calling function