View Source RedisMutex behaviour (redis_mutex v1.0.0)
An Elixir library for using Redis locks.
Summary
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 toRedisMutex
. If you have provided a different name for the connection during initiation of the connection, you must provide that name in the options forwith_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() ]
Callbacks
@callback child_spec(opts :: connection_options()) :: Supervisor.child_spec()
@callback start_link(start_options :: connection_options()) :: {:ok, pid()} | {:error, 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
.
@spec start_link(connection_options()) :: :ignore | {:error, any()} | {:ok, pid()}
Starts a process as part of a supervision tree.
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 toRedisMutex
. If you have provided a different name for the connection during initiation of the connection, you must provide that name in the options forwith_lock/3
.:timeout
- how longRedisMutex
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.