fix: answer 503 when PostgreSQL refuses connections with too many clients
SQLSTATE 53300 (too_many_connections) joins UNAVAILABLE_SQLSTATES, so a refused pool connection gives 503 unavailable instead of 500. New fault check fills max_connections through the failure stand hook and expects 503 in the contract format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,9 @@ IDEMPOTENCY_TTL_S = 2
|
||||
POOL_TIMEOUT_S = 1
|
||||
# срок проверки Postgres в /health/ready
|
||||
READINESS_TIMEOUT_S = 2
|
||||
# сколько секунд hook держит слоты max_connections; clear-connections
|
||||
# освобождает их раньше
|
||||
FILL_HOLD_S = 30
|
||||
ME = f"{API}/users/me"
|
||||
|
||||
|
||||
@@ -217,6 +220,29 @@ def test_pool_exhausted(tight_api: Api, stand: Stand) -> None:
|
||||
assert_ok(tight_api.request("GET", ME, actor=fresh), 200, "User")
|
||||
|
||||
|
||||
@pytest.mark.spec("data-storage", "Предел соединений PostgreSQL исчерпан")
|
||||
def test_connection_limit_reached(tight_api: Api, stand: Stand) -> None:
|
||||
actor = tight_api.register()
|
||||
# hook оборвал соединение пула: при выдаче pool_pre_ping
|
||||
# переподключается и получает «too many clients» (SQLSTATE 53300)
|
||||
stand("fill-connections", str(FILL_HOLD_S))
|
||||
try:
|
||||
resp, took = timed(lambda: tight_api.request("GET", ME, actor=actor))
|
||||
error = assert_error(resp, 503)
|
||||
# отказ PostgreSQL мгновенный: ответ не ждёт срока пула
|
||||
assert took < POOL_TIMEOUT_S + 0.5, f"503 took {took:.2f}s"
|
||||
# недоступность хранилища — не отказ в аутентификации
|
||||
assert "WWW-Authenticate" not in resp.headers
|
||||
for word in ("postgres", "sql", "client", "connect"):
|
||||
assert word not in error["message"].lower()
|
||||
finally:
|
||||
stand("clear-connections")
|
||||
# слоты свободны: сервис снова обслуживает запросы без рестарта; срок
|
||||
# сессии 2 с за время заполнения истёк — новый вход
|
||||
fresh = tight_api.register()
|
||||
assert_ok(tight_api.request("GET", ME, actor=fresh), 200, "User")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Readiness
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user