diff --git a/AGENTS.md b/AGENTS.md index 6d8bdaf..1cc84f9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,7 +36,7 @@ Python is 3.14, dependencies managed by `uv`. The API is exposed on `:8000`. `UseCases` group (request-scoped): one provider per use case in `app/use_cases/`. modern-di fills each dataclass field by its type, so a use case needs no `kwargs`. -**Pool and server sizing**: `DB_POOL_SIZE` (positive) and `DB_MAX_OVERFLOW` (non-negative) size the pool, and `app/__main__.py` sets Granian's `backpressure` to their sum, so each worker accepts only as many requests at once as it has connections. Requests beyond that wait in Granian's socket backlog instead of queueing on the pool. `APP_WORKERS` sets the Granian worker count, and each worker has its own pool. `DB_POOL_TIMEOUT` (default 30 s) bounds the wait for a connection, and a timeout returns 503 through `exceptions.database_unavailable_handler`. advanced-alchemy wraps the timeout in a `RepositoryError`, so the handler is also registered for `RepositoryError` and answers anything not caused by a pool timeout the way Litestar's default handler would: the debug page when `SERVICE_DEBUG` is on, a plain 500 otherwise. `DB_POOL_RECYCLE` defaults to SQLAlchemy's `-1` (never). Set it below the idle timeout of any proxy or load balancer in front of Postgres, so a connection it dropped is not handed out and retried after a 1-2 s backoff. +**Pool and server sizing**: `DB_POOL_SIZE` (positive) and `DB_MAX_OVERFLOW` (non-negative) size the pool, and a request that finds it empty waits for a connection. Granian's `backpressure` is left at its default: it limits open connections per worker, idle keep-alive ones included, so setting it to the pool size makes new connections wait while the app is idle. `APP_WORKERS` sets the Granian worker count, and each worker has its own pool. `DB_POOL_TIMEOUT` (default 30 s) bounds the wait for a connection, and a timeout returns 503 through `exceptions.database_unavailable_handler`. advanced-alchemy wraps the timeout in a `RepositoryError`, so the handler is also registered for `RepositoryError` and answers anything not caused by a pool timeout the way Litestar's default handler would: the debug page when `SERVICE_DEBUG` is on, a plain 500 otherwise. `DB_POOL_RECYCLE` defaults to SQLAlchemy's `-1` (never). Set it below the idle timeout of any proxy or load balancer in front of Postgres, so a connection it dropped is not handed out and retried after a 1-2 s backoff. **Use cases and lost connections**: `pool_pre_ping` is off by default (`DB_POOL_PRE_PING`). A pooled connection that died while idle is handled by `db_retry.postgres_retry` instead, which saves a round trip on every checkout. That only works if the retried function owns its whole transaction, so every use case has this shape: diff --git a/app/__main__.py b/app/__main__.py index 386d0e9..758a902 100644 --- a/app/__main__.py +++ b/app/__main__.py @@ -11,7 +11,6 @@ address=settings.app_host, port=settings.app_port, workers=settings.app_workers, - backpressure=settings.db_pool_size + settings.db_max_overflow, interface=Interfaces.ASGI, log_level=settings.log_level, loop=Loops.uvloop, diff --git a/tests/test_main.py b/tests/test_main.py index 6a47d5a..2ad56b4 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -17,13 +17,11 @@ def test_main(monkeypatch: pytest.MonkeyPatch) -> None: granian = mock.Mock() monkeypatch.setattr("granian.Granian", granian) monkeypatch.setattr(settings, "app_workers", 4) - monkeypatch.setattr(settings, "db_pool_size", 5) - monkeypatch.setattr(settings, "db_max_overflow", 2) runpy.run_module("app.__main__", run_name="__main__") assert granian.call_args.kwargs["workers"] == 4 # noqa: PLR2004 - assert granian.call_args.kwargs["backpressure"] == 7 # noqa: PLR2004 + assert "backpressure" not in granian.call_args.kwargs async def test_session() -> None: