forked from mtusi2026/mtusi2026lab7
chore: port lab 6 solution into the lab 7 skeleton
Source: mtusi2026lab6-safronov, solution/bvt2301-safronov, 6c6a065. Files follow AGENTS.md, "Переход в следующую лабу". Contracts, public tests, README, REPORT and evidence of lab 6 are not carried over; infra and scripts of the lab 7 skeleton stay as they are. Makefile is the lab 6 one with the lab 7 skeleton part restored (HTTPS BASE_URL on 8443, test-security); .gitignore and .dockerignore are taken from lab 6 because both skeletons have the same files; .env.example is the lab 6 one without changes. The compose project, image and CI prefix are renamed to lab7, and references to the previous lab now name lab6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""Какие ошибки драйвера означают «PostgreSQL недоступен» (→ 503).
|
||||
|
||||
Классы проверены на SQLAlchemy 2.1.1 и asyncpg 0.31 (задача 5.1 change
|
||||
`postgres-storage`, решение 6 его design):
|
||||
|
||||
- Postgres остановлен — `ConnectionRefusedError`, его SQLAlchemy не
|
||||
оборачивает; сервер не отвечает на подключение — `TimeoutError`; оба —
|
||||
`OSError`, как и срок блока из `session_scope`;
|
||||
- пул исчерпан — `sqlalchemy.exc.TimeoutError`;
|
||||
- сработал `statement_timeout` — `DBAPIError` с SQLSTATE `57014`;
|
||||
- соединение оборвано (рестарт, `pg_terminate_backend`) — `DBAPIError`
|
||||
с `connection_invalidated`;
|
||||
- отказ в подключении сверх `max_connections` — `TooManyConnectionsError`
|
||||
asyncpg, `DBAPIError` с SQLSTATE `53300` (класс 53: нехватка ресурсов),
|
||||
без `connection_invalidated`. Найден серией `pool` change
|
||||
`scaling-bench`; остальные коды класса 53 (диск, память) сюда не
|
||||
входят: их не воспроизводили.
|
||||
|
||||
Остальные `DBAPIError` (нарушение ограничения, синтаксис) — дефект
|
||||
сервиса, им положен 500, а не 503.
|
||||
"""
|
||||
|
||||
from sqlalchemy import exc as sa_exc
|
||||
|
||||
# класс 08 — ошибки соединения; 53300 — too_many_connections (отказ в
|
||||
# подключении сверх max_connections); 57014 — query_canceled
|
||||
# (statement_timeout); 57P01–57P03 — сервер останавливается или ещё не
|
||||
# принимает подключения
|
||||
UNAVAILABLE_SQLSTATES = ("08", "53300", "57014", "57P01", "57P02", "57P03")
|
||||
|
||||
|
||||
def is_db_unavailable(error: BaseException) -> bool:
|
||||
if isinstance(error, OSError | sa_exc.TimeoutError):
|
||||
return True
|
||||
if isinstance(error, sa_exc.DBAPIError):
|
||||
if error.connection_invalidated:
|
||||
return True
|
||||
sqlstate = getattr(error.orig, "sqlstate", None) or ""
|
||||
return sqlstate.startswith(UNAVAILABLE_SQLSTATES)
|
||||
return False
|
||||
Reference in New Issue
Block a user