litellm/tests/unit/proxy/db/test_connection_budget.py
Kerry Lu eec29c814f
fix(gateway): honor the configured Postgres connection cap (#52729)
* fix(gateway): honor the configured Postgres connection cap

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): retain lifespan call compatibility coverage

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(gateway): use the original DSN for the Postgres observer

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(gateway): preserve eager routes when applying the database cap

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs(gateway): explain why pool sizing updates the worker environment

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): use typed settings for startup validation

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-19 04:10:10 +00:00

110 lines
4.5 KiB
Python

import os
from typing import Final
import pytest
from litellm.proxy.db.connection_budget import (
MIN_CONNECTION_LIMIT,
RUST_READER_CONNECTIONS_ENV_VAR,
RUST_RESERVED_CONNECTIONS,
RUST_WRITER_CONNECTIONS_ENV_VAR,
ConnectionBudget,
RustConnections,
UnsplittableConnectionLimit,
rust_connections,
with_configured_connection_budget,
)
@pytest.mark.parametrize("total", range(MIN_CONNECTION_LIMIT, 41))
def test_prisma_and_rust_never_open_more_than_the_total(total: int) -> None:
budget: Final = ConnectionBudget.of("DATABASE_URL", total)
assert budget.python + budget.rust == total
assert budget.python >= 1
assert budget.rust >= 1
@pytest.mark.parametrize(
("total", "python", "rust"),
[(2, 1, 1), (3, 1, 2), (4, 2, 2), (100, 98, 2)],
ids=["two_connections", "three_connections", "four_connections", "large"],
)
def test_rust_takes_what_is_left_of_its_reserve_after_prisma_keeps_one(total: int, python: int, rust: int) -> None:
budget: Final = ConnectionBudget.split(total, rust_reserved=2)
assert (budget.python, budget.rust) == (python, rust)
@pytest.mark.parametrize("connection_limit", ["7", 7, 7.0])
def test_a_whole_number_limit_is_split(connection_limit: str | float) -> None:
assert ConnectionBudget.of("DATABASE_URL", connection_limit, rust_reserved=2) == ConnectionBudget.split(
7, rust_reserved=2
)
@pytest.mark.parametrize("connection_limit", ["1", 1, "0", 0, "-1", -1, "abc", "10.0", 10.5, "", "²", True])
def test_a_limit_that_leaves_no_room_for_both_pools_is_refused(connection_limit: str | float) -> None:
with pytest.raises(UnsplittableConnectionLimit) as refused:
ConnectionBudget.of("DATABASE_URL_READ_REPLICA", connection_limit)
assert str(refused.value) == (
f"DATABASE_URL_READ_REPLICA connection_limit {connection_limit!r} must be a whole number of at least 2, "
"one connection for Prisma and at least one for the Rust pool that reads keys for auth"
)
def test_prisma_params_carry_prisma_share() -> None:
params: Final = {"connection_limit": "010", "pool_timeout": 30}
assert ConnectionBudget.split(10).prisma_params(params) == {"connection_limit": 8, "pool_timeout": 30}
@pytest.mark.parametrize(
("environ", "expected"),
[
({RUST_WRITER_CONNECTIONS_ENV_VAR: "2", RUST_READER_CONNECTIONS_ENV_VAR: "1"}, RustConnections(2, 1)),
({RUST_WRITER_CONNECTIONS_ENV_VAR: "1"}, RustConnections(1, RUST_RESERVED_CONNECTIONS)),
({}, RustConnections(RUST_RESERVED_CONNECTIONS, RUST_RESERVED_CONNECTIONS)),
(
{RUST_WRITER_CONNECTIONS_ENV_VAR: "-1", RUST_READER_CONNECTIONS_ENV_VAR: "two"},
RustConnections(RUST_RESERVED_CONNECTIONS, RUST_RESERVED_CONNECTIONS),
),
],
ids=["cli_split_both", "cli_split_writer_only", "started_without_cli", "not_a_number"],
)
def test_a_worker_sizes_each_rust_pool_from_the_cli_split(environ: dict[str, str], expected: RustConnections) -> None:
assert rust_connections(environ) == expected
@pytest.mark.parametrize(
("yaml_limit", "writer_query", "reader_query", "connections"),
[
(2, "schema=app&connection_limit=1", "connection_limit=1", RustConnections(1, 1)),
(10, "schema=app&connection_limit=8", "connection_limit=3", RustConnections(2, 2)),
(None, "schema=app", "connection_limit=3", RustConnections(4, 2)),
],
)
def test_configured_budget_caps_prisma_and_reserves_rust_until_lifespan_exit(
monkeypatch: pytest.MonkeyPatch,
yaml_limit: int | None,
writer_query: str,
reader_query: str,
connections: RustConnections,
) -> None:
writer: Final = "postgresql://db/app?schema=app"
reader: Final = "postgresql://reader/app?connection_limit=3"
monkeypatch.setenv("DATABASE_URL", writer)
monkeypatch.setenv("DATABASE_URL_READ_REPLICA", reader)
monkeypatch.setenv(RUST_WRITER_CONNECTIONS_ENV_VAR, "4")
monkeypatch.delenv(RUST_READER_CONNECTIONS_ENV_VAR, raising=False)
with with_configured_connection_budget(yaml_limit):
assert os.environ["DATABASE_URL"] == f"postgresql://db/app?{writer_query}"
assert os.environ["DATABASE_URL_READ_REPLICA"] == f"postgresql://reader/app?{reader_query}"
assert rust_connections(os.environ) == connections
assert os.environ["DATABASE_URL"] == writer
assert os.environ["DATABASE_URL_READ_REPLICA"] == reader
assert rust_connections(os.environ) == RustConnections(4, 2)
assert RUST_READER_CONNECTIONS_ENV_VAR not in os.environ