Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pool size and max overflow for timeout issue #338

Merged
merged 9 commits into from
Jan 6, 2025
Merged
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- run: make lint

test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
services:
postgres:
image: postgis/postgis:14-master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deply-to-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- run: make lint

test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
services:
postgres:
image: postgis/postgis:14-master
Expand Down
56 changes: 41 additions & 15 deletions application/db/session.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
from functools import lru_cache
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from typing import Iterator
import logging
from application.settings import get_settings
from contextlib import contextmanager

from fastapi_utils.session import FastAPISessionMaker
from sqlalchemy.orm import Session
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
# Set up logging
logger = logging.getLogger(__name__)

from application.settings import get_settings

def _create_engine():
settings = get_settings()
engine = create_engine(
settings.READ_DATABASE_URL,
pool_size=settings.DB_POOL_SIZE,
max_overflow=settings.DB_POOL_MAX_OVERFLOW,
)

# this can be used in fast api path functions using Depends to inject a db session
def get_session() -> Iterator[Session]:
yield from _get_fastapi_sessionmaker().get_db()
logger.info(
f"Engine created with pool_size={engine.pool.size()}, "
f"max_overflow={engine.pool._max_overflow} "
)

return engine

# this can be used in non path functions to create a context manager for a db session
# see https://github.com/dmontagu/fastapi-utils/blob/master/fastapi_utils/session.py#L77:L91
def get_context_session() -> Iterator[Session]:
return _get_fastapi_sessionmaker().context_session()

# Create session factory
engine = _create_engine()
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


@lru_cache()
def _get_fastapi_sessionmaker() -> FastAPISessionMaker:
database_uri = get_settings().READ_DATABASE_URL
return FastAPISessionMaker(database_uri)
def get_session() -> Iterator[Session]:
db = SessionLocal()
try:
yield db
finally:
db.close()


@contextmanager
def get_context_session() -> Iterator[Session]:
session = SessionLocal()
try:
yield session
finally:
session.close()
2 changes: 2 additions & 0 deletions application/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Settings(BaseSettings):
GA_MEASUREMENT_ID: Optional[str] = None
OS_CLIENT_KEY: Optional[str] = None
OS_CLIENT_SECRET: Optional[str] = None
DB_POOL_SIZE: Optional[int] = 5
DB_POOL_MAX_OVERFLOW: Optional[int] = 10


@lru_cache()
Expand Down
Loading