Skip to content

Commit 36b42c6

Browse files
Merge pull request #117 from QueryaHub/issue-116-pool-management
feat(db): connection pool management in AppState (Closes #116)
2 parents 788f2df + 7805111 commit 36b42c6

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

oxyroute/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ async def __rsgi_init__(self, *args: Any, **kwargs: Any) -> None:
387387
return None
388388

389389
async def __rsgi_del__(self, *args: Any, **kwargs: Any) -> None:
390-
"""RSGI worker teardown (no-op in the base class)."""
391-
return None
390+
"""RSGI worker teardown. Closes the global connection pool if it exists."""
391+
await self.close_database()
392392

393393
async def __rsgi__(self, scope: Any, protocol: Any) -> Any:
394394
"""

tests/test_database_pool.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
from oxyroute import App
3+
4+
5+
@pytest.mark.anyio
6+
async def test_database_pool_failure():
7+
app = App()
8+
9+
# Attempting to connect to a non-existent database should fail fast
10+
with pytest.raises(RuntimeError, match="DB connect error:"):
11+
await app.setup_database("postgresql://foo:bar@127.0.0.1:12345/baz")
12+
13+
14+
@pytest.mark.anyio
15+
async def test_database_pool_teardown_no_op():
16+
app = App()
17+
18+
# Calling close without setup should be a no-op and not raise any errors
19+
await app.close_database()
20+
21+
# __rsgi_del__ should also gracefully do nothing if no db was set up
22+
await app.__rsgi_del__()

0 commit comments

Comments
 (0)