Files
mtusi2026lab5/app/idempotency.py
T
Artu-RitoandClaude Sonnet 5 bdba93f022 Лаба 5: Redis Streams очередь + асинхронная обработка изображений (оценка 4)
Переиспользован стек lab4 (LB/2 реплики/Postgres/SeaweedFS). upload теперь
202 queued; отдельный worker-процесс генерирует thumbnail/crop/optimized.
Transactional outbox закрывает разрыв COMMIT/publish; durable retry
(job_retry_schedule) переживает падение worker во время backoff; lease
recovery через XAUTOCLAIM; DLQ-реестр (job_failures) с явным operator
replay (scripts/replay_failed.py). make test/test-persistence/test-failover/
validate зелёные; в процессе экспериментов найдены и исправлены два
реальных бага (неполный try/except вокруг S3-записи вариантов, ошибка типов
asyncpg в построении interval) — см. REPORT.md и evidence/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 01:21:42 +03:00

90 lines
3.1 KiB
Python

"""Shared (cross-replica) idempotency for POST message, scoped to (user, method, path, key)."""
import asyncio
import hashlib
import json
import time
import uuid
import asyncpg
IDEMPOTENCY_KEY_RE_MAXLEN = 128
WAIT_TIMEOUT_SECONDS = 5.0
POLL_INTERVAL_SECONDS = 0.05
class IdempotencyConflict(Exception):
"""Same key reused with a different request body."""
def request_hash(body: dict) -> str:
canonical = json.dumps(body, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
async def run_once(
conn: asyncpg.Connection,
user_id: uuid.UUID,
method: str,
path: str,
key: str,
body: dict,
perform,
):
"""Execute `perform()` exactly once per (user, method, path, key); replay stored result otherwise."""
digest = request_hash(body)
inserted = await conn.fetchrow(
"""
INSERT INTO idempotency_keys (user_id, method, path, idem_key, request_hash)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (user_id, method, path, idem_key) DO NOTHING
RETURNING idem_key
""",
user_id, method, path, key, digest,
)
if inserted is None:
existing = await conn.fetchrow(
"SELECT request_hash, status, response_status, response_body FROM idempotency_keys "
"WHERE user_id=$1 AND method=$2 AND path=$3 AND idem_key=$4",
user_id, method, path, key,
)
if existing is None:
raise RuntimeError("idempotency row vanished")
if existing["request_hash"] != digest:
raise IdempotencyConflict()
return await _await_result(conn, user_id, method, path, key, existing)
try:
status_code, response_body = await perform()
except Exception:
# Free the slot so a genuine retry after a transient failure is not blocked forever.
await conn.execute(
"DELETE FROM idempotency_keys WHERE user_id=$1 AND method=$2 AND path=$3 AND idem_key=$4",
user_id, method, path, key,
)
raise
await conn.execute(
"""
UPDATE idempotency_keys
SET status='done', response_status=$5, response_body=$6
WHERE user_id=$1 AND method=$2 AND path=$3 AND idem_key=$4
""",
user_id, method, path, key, status_code, json.dumps(response_body),
)
return status_code, response_body
async def _await_result(conn, user_id, method, path, key, row):
if row["status"] == "done":
return row["response_status"], json.loads(row["response_body"])
deadline = time.monotonic() + WAIT_TIMEOUT_SECONDS
while time.monotonic() < deadline:
await asyncio.sleep(POLL_INTERVAL_SECONDS)
current = await conn.fetchrow(
"SELECT status, response_status, response_body FROM idempotency_keys "
"WHERE user_id=$1 AND method=$2 AND path=$3 AND idem_key=$4",
user_id, method, path, key,
)
if current and current["status"] == "done":
return current["response_status"], json.loads(current["response_body"])
raise TimeoutError("concurrent idempotent request did not finish in time")