Skip to main content

claim_for_delivery

Function claim_for_delivery 

Source
pub async fn claim_for_delivery(
    pool: &PgPool,
    now: i64,
    limit: u32,
) -> Result<Vec<QueuedMessage>, Error>
Expand description

Atomically claim up to limit pending messages and transition them to inflight, returning the claimed rows.

Implemented as UPDATE … WHERE id IN (SELECT … FOR UPDATE SKIP LOCKED) RETURNING … so concurrent workers never pick the same row (SKIP LOCKED skips rows already locked by other workers), and the claim+transition collapse to a single round-trip and single WAL fsync per batch instead of one SELECT + N per-row UPDATEs.

Correctness vs the legacy dequeue + per-row mark_inflight flow: SKIP LOCKED prevents the duplicate-delivery race that the pre-existing flow exposed when more than one worker was running concurrently (both workers would SELECT the same pending rows and both proceed past their racing UPDATEs). With this claim path, each pending row is delivered by at most one worker.