RedMutex v0.2.2 RedMutex behaviour View Source
Defines a mutex.
A mutex defines an easy to use interface to interact with an distributed lock backed by redis.
When used, the mutex expects the :otp_app as option. The :otp_app should point to an OTP application that has the mutex configuration. For example, the mutex:
defmodule MyApp.MyMutex do
use RedMutex, otp_app: :my_app
end
Could be configured with:
config :my_app, MyApp.MyMutex,
url: "redis://localhost:6379",
key: "red_mutex_lock",
expiration_in_seconds: 3_600
Options:
:url
- the redis url. Required.:key
- The key at redis used to store the lock information. Defaults to"red_mutex_lock"
.:expiration_in_seconds
- Time in seconds that the resource will be kept locked. After that time the lock will be automattically released. Defaults to3600
, one hour.
Link to this section Summary
Callbacks
Attempts to acquire an lock.
Checks if an lock exists. Returns {:ok, true}
if the resource is locked.
Releases the lock.
Obtains an lock, run the callback, and releases the lock when the block completes.
Link to this section Types
Link to this type
callback() View Source
Link to this type
lock()
View Source
lock()
View Source
lock() :: String.t()
lock() :: String.t()
Link to this type
reason()
View Source
reason()
View Source
reason() :: any()
reason() :: any()
Link to this section Callbacks
Link to this callback
acquire_lock() View Source
Attempts to acquire an lock.
Examples
iex> MyMutex.acquire_lock()
{:ok, lock}
iex> MyMutex.acquire_lock()
{:error, :already_locked}
iex> MyMutex.acquire_lock()
{:error, reason}
Link to this callback
exists_lock() View Source
Checks if an lock exists. Returns {:ok, true}
if the resource is locked.
Examples
iex> MyMutex.exists_lock()
{:ok, true}
iex> MyMutex.exists_lock()
{:ok, false}
iex> MyMutex.exists_lock()
{:error, reason}
Link to this callback
release_lock(lock) View Source
Releases the lock.
Examples
iex> MyMutex.release_lock(lock)
:ok
iex> MyMutex.release_lock(lock)
{:error, :unlock_fail}
iex> MyMutex.release_lock(lock)
{:error, reason}
Link to this callback
synchronize(callback) View Source
Obtains an lock, run the callback, and releases the lock when the block completes.
Examples
iex> MyMutex.synchronize(fn ->
...> # work
...> {:ok, "completed"}
...> end)
{:ok, "completed"}
iex> MyMutex.synchronize({MyApp, :work, []})
{:ok, "completed"}