feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec change instance-id-idempotency) - rename the compose project to mtusi-lab3 and the image to :lab3 - add X-Instance-Id to every response and instance_id to every log record ([app] instance_id, hostname by default) - require Idempotency-Key on POST messages: keys table in Postgres, key and message in one transaction, replay returns the same 201, another body gets 409, TTL from [idempotency] ttl_seconds - send the key from student-tests, k6 scenarios and the skill driver; add idempotency, X-Instance-Id, key race and fault tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"""Какие ошибки драйвера означают «PostgreSQL недоступен» (→ 503).
|
||||
|
||||
Классы проверены на SQLAlchemy 2.1.1 и asyncpg 0.31 (задача 5.1 change
|
||||
`postgres-storage`, решение 6 его design):
|
||||
|
||||
- Postgres остановлен — `ConnectionRefusedError`, его SQLAlchemy не
|
||||
оборачивает; сервер не отвечает на подключение — `TimeoutError`; оба —
|
||||
`OSError`, как и срок запроса из `get_db_session`;
|
||||
- пул исчерпан — `sqlalchemy.exc.TimeoutError`;
|
||||
- сработал `statement_timeout` — `DBAPIError` с SQLSTATE `57014`;
|
||||
- соединение оборвано (рестарт, `pg_terminate_backend`) — `DBAPIError`
|
||||
с `connection_invalidated`.
|
||||
|
||||
Остальные `DBAPIError` (нарушение ограничения, синтаксис) — дефект
|
||||
сервиса, им положен 500, а не 503.
|
||||
"""
|
||||
|
||||
from sqlalchemy import exc as sa_exc
|
||||
|
||||
# класс 08 — ошибки соединения; 57014 — query_canceled (statement_timeout);
|
||||
# 57P01–57P03 — сервер останавливается или ещё не принимает подключения
|
||||
UNAVAILABLE_SQLSTATES = ("08", "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