Fact.Lock (Fact v0.2.0)
View SourceProvides a cross-VM exclusive lock for Fact database instances.
This modules uses a UNIX Domain Socket to coordinate access to a Fact instance, ensuring that only one BEAM VM can perform certain operations at a time. The lock defines three modes.
:run- normal instance operation:restore- doing an overwrite restore of a backup:create- initialization of a new instance
Features
- Cross-VM safe: Only one process across all BEAM VMs can hold the lock at a time.
- Crash-safe: The lock is automatically released if the owning VM exits.
- Stale cleanup: Detects and removes stale socket files left by crashed processes.
- Metadata: Store JSON metadata including the OS PID, BEAM PID, BEAM node, lock mode, and timestamp.
Summary
Types
The deserialized representation of Fact.Lock.metadata/0, with string keys instead of atoms.
Types
@type metadata_record() :: map()
The deserialized representation of Fact.Lock.metadata/0, with string keys instead of atoms.
@type mode() :: :run | :restore | :create
Functions
@spec acquire(Fact.database_id(), mode()) :: {:ok, t()} | {:error, {:locked, metadata()}} | {:error, term()}
Acquire a lock for the instance in the specified mode.
@spec release(Fact.database_id(), t()) :: :ok
Release an acquired lock.
Closes the socket, deletes the socket file, and deletes the metadata file.