"""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")