fix: recover orphaned authored operations (#319)
This commit is contained in:
1 parent
b5858046ee
commit
dc18004e02
15 files changed
+321
-18
No files matched your search
+6
-2
@@ -78,7 +78,7 @@ class IdempotencyLedger:
|
||||
(now - self.ttl_seconds,),
|
||||
)
|
||||
row = connection.execute(
|
||||
"SELECT fingerprint, status, response_json FROM idempotency_operations "
|
||||
"SELECT fingerprint, status, response_json, created_at FROM idempotency_operations "
|
||||
"WHERE key = ?",
|
||||
(key,),
|
||||
).fetchone()
|
||||
@@ -87,9 +87,13 @@ class IdempotencyLedger:
|
||||
return Reservation("conflict")
|
||||
if row[1] == "completed":
|
||||
return Reservation("completed", json.loads(row[2]))
|
||||
if row[3] <= now - self.ttl_seconds:
|
||||
return Reservation("uncertain")
|
||||
return Reservation("pending")
|
||||
count = connection.execute(
|
||||
"SELECT COUNT(*) FROM idempotency_operations"
|
||||
"SELECT COUNT(*) FROM idempotency_operations "
|
||||
"WHERE status = 'completed' OR created_at > ?",
|
||||
(now - self.ttl_seconds,),
|
||||
).fetchone()[0]
|
||||
if count >= self.max_entries:
|
||||
completed = connection.execute(
|
||||
|
||||
+15
-2
@@ -314,9 +314,22 @@ async def _run_idempotent_authored_action(
|
||||
detail="Authored action queue is busy; please retry",
|
||||
headers={"Retry-After": "1"},
|
||||
)
|
||||
|
||||
existing = _authored_action_operations.get(idempotency_key)
|
||||
if reservation.state == "pending":
|
||||
if reservation.state == "uncertain" and (
|
||||
existing is None or existing[0] != fingerprint
|
||||
):
|
||||
operation.close()
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"code": "delivery_uncertain",
|
||||
"message": (
|
||||
"Delivery could not be confirmed. Verify it was not posted before retrying."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
if reservation.state in {"pending", "uncertain"}:
|
||||
operation.close()
|
||||
if existing is None or existing[0] != fingerprint:
|
||||
raise HTTPException(
|
||||
|
||||
Reference in new issue
Block a user