View Source RedisMutex behaviour (redis_mutex v1.1.0)

An Elixir library for using Redis locks.

Summary

Types

Options for connecting to Redis.

Functions

The specification for starting a connection with Redis. Can include any of the connection_options.

Starts a process as part of a supervision tree.

Provides a mutex for performing a function.

Types

@type connection_options() :: [
  name: name(),
  redis_url: String.t(),
  host: String.t(),
  port: non_neg_integer(),
  database: String.t() | non_neg_integer(),
  username: String.t(),
  password: Redix.password(),
  timeout: timeout(),
  sync_connect: boolean(),
  exit_on_disconnection: boolean(),
  backoff_initial: non_neg_integer(),
  backoff_max: timeout(),
  ssl: boolean(),
  socket_opts: [term()],
  hibernate_after: non_neg_integer(),
  spawn_opt: keyword(),
  debug: keyword(),
  sentinel: keyword()
]

Options for connecting to Redis.

## Options

  • :name - the name to use for a Redis connection. When not provided, the connection name defaults to RedisMutex. If you have provided a different name for the connection during initiation of the connection, you must provide that name in the options for with_lock/3.

  • :redis_url - The URL to use connecting to Redis. When this is provided, other options are not needed. When :redis_url is provided, the only other options honored are :name and :sync_connect.

When :redis_url is not provided, other connection options (like :host and :port) must be provided.

@type lock_opts() :: [
  name: name(),
  timeout: non_neg_integer(),
  expiry: non_neg_integer()
]
@type name() :: String.t() | atom() | module()

Callbacks

@callback child_spec(opts :: connection_options()) :: Supervisor.child_spec()
Link to this callback

start_link(start_options)

View Source
@callback start_link(start_options :: connection_options()) ::
  {:ok, pid()} | {:error, any()}
@callback with_lock(key :: String.t(), fun :: (-> any())) :: any()
Link to this callback

with_lock(key, fun, opts)

View Source
@callback with_lock(key :: String.t(), fun :: (-> any()), opts :: lock_opts()) :: any()

Functions

@spec child_spec(opts :: connection_options()) :: Supervisor.child_spec()

The specification for starting a connection with Redis. Can include any of the connection_options.

Link to this function

start_link(start_options)

View Source
@spec start_link(connection_options()) :: :ignore | {:error, any()} | {:ok, pid()}

Starts a process as part of a supervision tree.

Link to this function

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

View Source
@spec with_lock(key :: String.t(), fun :: (-> any()), opts :: lock_opts()) :: any()

Provides a mutex for performing a function.

The lock is defined by the key argument. When the key is already taken, the function will not be performed. When the key is not already in use, the function argument is run.

The key should be unique to the operation being performed.

The function provided should be a zero-arity function.

## Options

  • :name - the name of the Redis connection to use when performing the lock. Defaults to RedisMutex. If you have provided a different name for the connection during initiation of the connection, you must provide that name in the options for with_lock/3.

  • :timeout - how long RedisMutex will try before abandoning the attempt to gain the lock. Timeout is in milliseconds. Defaults to 4_000.

  • :expiry - how long the lock will be held before expiring. Expiry is in milliseconds. Defaults to 2_000.