Skip to main content

try_with_file_lock

Function try_with_file_lock 

Source
pub async fn try_with_file_lock<T, F>(
    lock_path: &Path,
    f: F,
) -> StorageResult<T>
where T: Send + 'static, F: FnOnce() -> StorageResult<T> + Send + 'static,
Expand description

Fail-fast counterpart of with_file_lock (#105): the same advisory flock on lock_path and the same hold scope — lock → read-modify-write → publish → release — but acquisition is non-blocking (flock(LOCK_EX | LOCK_NB)). On contention the call returns immediately with StorageError::LockWouldBlock naming the lock file instead of parking the waiter on the blocking pool, so consumers whose contract is fail-fast on contention (e.g. a multi-process CLI append that must exit non-zero on a concurrent append) can adopt the blessed convention without waiting.

Contention is distinctable: match on StorageError::LockWouldBlock { path } and map it into your own taxonomy (IO errors, task-join failures and the closure’s own errors keep their original shapes). Everything with_file_lock documents holds here too: the lock file is created on demand (missing parents included) and left in place — a rendezvous point, not a commit artifact; the closure runs on the blocking pool and cannot await with_commit_actor; off unix this fails with StorageError::UnsupportedFormat. Advisory means only cooperating writers that resolve the same lock file are excluded.