Skip to main content

with_write_retry

Function with_write_retry 

Source
pub fn with_write_retry<T, F>(
    conn: &mut Connection,
    max_total: Duration,
    f: F,
) -> Result<T>
where F: FnMut(&Transaction<'_>) -> Result<T>,
Expand description

Run f against a write transaction, retrying on SQLITE_BUSY/SQLITE_LOCKED with exponential backoff and jitter. Each attempt uses BEGIN IMMEDIATE so contention is detected up front rather than mid-transaction. Gives up once max_total elapses and returns the final busy error.

Why this exists: SQLite’s own busy_timeout retries are unfair under heavy write contention (a single waiter can spend the full timeout sleeping while other writers slip past). Jittered backoff at the caller gives fairness and a bounded worst-case latency suitable for the shell hot path.