Method Lease(string, TimeSpan?, CancellationToken)
that also names the claim it took: identifies this claim of
the key, so that and
act on it only while it is still the one held. A holder
that outlived abandonedAfter and was taken over then confirms or removes nothing of the new
holder's claim.
The default answers without a token, and the token methods then act on the key as
and do.
public Task<IdempotentLease> Lease(string key, TimeSpan? abandonedAfter, CancellationToken ct = default)
Remarks
Answered from the row's confirmed flag and created_at; the claim's token is a new random id written
to the row's claim_id. An abandoned claim is taken over by deleting the row only while it is still
unconfirmed and older than the cutoff, then inserting a new one: of two callers that saw the same stale row, one
deletes it and the other deletes nothing and finds the new claim in flight. Inside an ambient transaction the
statements run on the transaction's connection, as does. A row without
a claim_id (written by or before 4.8.0) is answered and taken
over the same way.
Both the stamp and the cutoff are the database's clock (created_at keeps its ISO 8601 UTC text, so a row
written with the application's clock compares as before), and an insert that waits on locks for longer than a tenth
of abandonedAfter is stamped again before it commits.