Skip to content

Comments

Refactor the code to allow multiple concurrent connections from the UkWofost app to the SQL database#109

Merged
mcmancini merged 4 commits intodevfrom
iss_108_concurrent_db_connections
Sep 17, 2025
Merged

Refactor the code to allow multiple concurrent connections from the UkWofost app to the SQL database#109
mcmancini merged 4 commits intodevfrom
iss_108_concurrent_db_connections

Conversation

@mcmancini
Copy link
Owner

This PR attmepts to address Issue #108.
There is apparently a limit in the number of concurrent connections that can happen at the same time to a postgreSQL database. When trying to optimise management running instances of wofost in parallel, the whole thing crashes with the following error: An unexpected error occurred: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute.

To fix this, I created an utility function that generates a SQLAlchemy engine with a pool_size=300 and max_overflow=100; this is created only once, either when initialising the Uvicorn UkWofost app or when importing anything from ukwofost.core for the first time. This global engine is then used any time a connection to the SQL database is needed. This is clunkier than adding the engine within the app; I had to do this just because the code should also allow to run UkWofost locally and algorithmically without having to start and run the uvicorn server.

@mcmancini mcmancini added bug Something isn't working enhancement New feature or request feature labels Sep 17, 2025
@mcmancini mcmancini self-assigned this Sep 17, 2025
@mcmancini mcmancini merged commit 10ed4f1 into dev Sep 17, 2025
8 checks passed
@mcmancini mcmancini deleted the iss_108_concurrent_db_connections branch September 17, 2025 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant