Skip to content

Commit

Permalink
fix: address memory leak by closing SQLAlchemy sessions and add conne…
Browse files Browse the repository at this point in the history
…ction pool options

- Close SQLAlchemy sessions after use to prevent memory leaks.
- Add connection pool options to SQLAlchemy engine to improve performance.
- Add `memray` dependency for memory profiling, can be used under poetry shell.
  • Loading branch information
iPromKnight authored and Gaisberg committed Aug 17, 2024
1 parent a7f7586 commit 0ebd38f
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
151 changes: 150 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ruff = "^0.3.0"
isort = "^5.10.1"
codecov = "^2.1.13"
httpx = "^0.27.0"
memray = "^1.13.4"

[tool.poetry.group.test]
optional = true
Expand Down
7 changes: 6 additions & 1 deletion src/program/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from utils import data_dir_path
from utils.logger import logger

db = SQLAlchemy(settings_manager.settings.database.host)
engine_options={
"pool_size": 50,
"pool_recycle": 600,
}

db = SQLAlchemy(settings_manager.settings.database.host, engine_options=engine_options)

script_location = data_dir_path / "alembic/"

Expand Down
2 changes: 2 additions & 0 deletions src/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def _retry_library(self) -> None:
.offset(page_number * number_of_rows_per_page)
).unique().scalars().all()

session.expunge(items_to_submit)
session.close()
for item in items_to_submit:
self._push_event_queue(Event(emitted_by="RetryLibrary", item=item))

Expand Down

0 comments on commit 0ebd38f

Please sign in to comment.