Flockit

Advisory file locks for Elixir using flock(2).

{:ok, lock} = Flockit.lock("/var/run/my_app.lock")
# ... only one holder at a time, across OS processes ...
:ok = Flockit.unlock(lock)
# Or scoped:
{:ok, result} = Flockit.with_lock("/var/run/my_app.lock", fn -> do_work() end)
# Shared (read) locks, timeouts and non-blocking attempts:
{:ok, lock} = Flockit.lock(path, mode: :shared, timeout: 5_000)
{:error, :eagain} = Flockit.try_lock(path)

See the documentation for the full semantics.

Tested on Linux and macOS. Other Unix systems should work, with timed retries in place of release notifications, but are untested.

Installation

def deps do
[
{:flockit, "~> 1.0"}
]
end

A C compiler and make are needed to build the NIF.