FileLock (fnord v0.8.83)

View Source

Cross-process filesystem lock helpers for arbitrary files. Uses a lock dir with atomic stale lock takeover.

API is intentionally small:

  • acquire_lock(path)
  • release_lock(path)
  • with_lock(path, fun, opts \ [])

Summary

Functions

Acquire a lock directory alongside the file at path.

Release the lock directory created for path.

Execute fun while holding a lock for path.

Functions

acquire_lock(path)

@spec acquire_lock(path :: binary()) :: :ok | {:error, term()}

Acquire a lock directory alongside the file at path.

Returns :ok on success, {:error, reason} on failure.

release_lock(path)

@spec release_lock(path :: binary()) :: :ok

Release the lock directory created for path.

with_lock(path, fun, opts \\ [])

@spec with_lock(binary(), (-> any()), keyword()) ::
  {:ok, any()}
  | {:error, :lock_failed}
  | {:error, term()}
  | {:callback_error, Exception.t()}

Execute fun while holding a lock for path.

Returns:

  • {:ok, result} when the callback returns normally
  • {:error, :lock_failed} when the lock cannot be acquired
  • {:callback_error, exception} when the callback raises
  • any {:error, reason} tuple returned by the callback itself