Write entry only if nothing live is held under key. Returns "claimed", or what is held.
When the claim was taken.
How long past claimedAt the claim is honoured before the attempt is presumed dead.
Random per attempt. complete and release act only while the claim
under the key still carries it, so an attempt that outlived its lease
cannot touch the claim of the attempt that replaced it.
Replace the claim with the outcome, only if key holds a live claim whose
owner is owner. Returns whether it did. entry.retainFor is a hint
for reclaiming space; the age is re-checked on the way out and store
expiry is never trusted for correctness.
Drop whatever is held, whoever holds it. Called by Ledger.forget.
Drop the claim, only if key holds a claim whose owner is owner.
Returns whether it did. Called when an action threw or shouldStore
declined.
Where the keys live. Async everywhere, including in memory, so that moving from a process-local store to a networked one is a constructor change.
Three methods MUST be atomic, each as one operation:
claimis the whole guarantee: two callers claiming one key at once see exactly one"claimed". It MUST treat a claim whose lease has expired, and a completion whose retention has expired, as absent, so a retry of a dead attempt can become the first attempt.SET NX PXin Redis andINSERT ... ON CONFLICT DO NOTHINGin Postgres are this method.completeandreleasecompare, then write or delete. An attempt can outlive its lease while a retry claims the key; when the first attempt finally finishes, a blind write would overwrite the retry's claim and a blind delete would remove it, letting a third copy run beside the retry. In Redis each is a Lua script that reads the entry, checks it is a live claim withowner, and only then runsSET ... PXorDEL. In Postgres they areUPDATE ... WHERE key = $1 AND owner = $2 AND expires_at > now()andDELETE ... WHERE key = $1 AND owner = $2, with the row count saying whether it happened.