Files
mtusi2026lab4/app/idempotency.py
T
Artu-RitoandClaude Sonnet 5 38268f63f8 Лаба 4: SeaweedFS S3-хранилище и вложения (оценка 4)
Переиспользован стек lab3 (LB/2 реплики/Postgres/Redis/идемпотентность).
Добавлены: S3-хранилище байтов вложений (SeaweedFS вместо MinIO — публичные
MinIO-образы недоступны без авторизации), метаданные attachments в Postgres,
проверка изображений по содержимому с защитой от decompression bomb,
ограниченные S3-credentials, скрипт сверки orphan-объектов. make test/
test-persistence/test-failover/validate зелёные для обеих реплик; собраны
доказательства по превышению размера, MIME-спуфингу, чужим attachment_id,
приватности bucket, конкурентным загрузкам и orphan-очистке — см. REPORT.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 00:59:13 +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")