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

Define options to refresh database connections via keep alive and pool #56

Merged
merged 2 commits into from
Oct 14, 2023
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
4 changes: 4 additions & 0 deletions api/src/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
engine = create_engine(
settings.DATABASE_URL if not settings.DEBUG else settings.TEST_DATABASE_URL,
echo=settings.DEBUG,
pool_pre_ping=True,
pool_size=5,
pool_recycle=3600,
pool_timeout=30,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Expand Down
2 changes: 1 addition & 1 deletion lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def create_connection() -> connection:
database_url = os.getenv("DATABASE_URL")
dsn = parse_dsn(database_url)
return psycopg2.connect(**dsn) # noqa
return psycopg2.connect(keepalives=1, keepalives_idle=30, keepalives_interval=10, keepalives_count=5, **dsn) # noqa


def create_cursor(conn: connection) -> cursor:
Expand Down