Crate redis_lock

Source
Expand description

Rusty distributed locking backed by Redis.

§Locking a single resource

let connection = Arc::new(Mutex::new(
    client.get_multiplexed_async_connection().await?
));
// Execute a function with the lock.
redis_lock::lock_across(
    &[connection],
    "account1",
    async move { /* .. */ },
    redis_lock::LockAcrossOptions::default()
).await?;

§Locking multiple resources

// Setup.
redis_lock::setup(&client).await?;
// Get lock.
let mut lock = redis_lock::MultiResourceLock::new(client.clone())?;
let resources = vec![String::from("account1"), String::from("account2")];
// Execute a function with the lock.
lock.map_default(&resources, async move { /* .. */ }).await?;

§Vs rslock

I would recommend this library over rslock when:

  • your application is focussed on async.
  • your application does operations that require exclusive access to multiple resources.

§Similar work

Modules§

syncsync
Synchronous implementation of the lock.

Structs§

LockAcrossOptions
Options to configure lock_across.
MultiResourceLock
A distributed mutual exclusion lock backed by Redis.

Enums§

AcquireLockError
LockAcrossError
MapError
Error for MultiResourceLock::map.
ReleaseLockError

Constants§

DEFAULT_DURATION
The default duration to attempt to acquire the lock.
DEFAULT_EXPIRATION
Default expiration duration for the lock.
DEFAULT_RETRY_DELAY
The default delay between retries when attempting to acquire the lock.
DEFAULT_SLEEP
Default sleep duration between attempts to acquire the lock.
DEFAULT_TIMEOUT
Default timeout duration for acquiring the lock.
DEFAULT_TTL
The default time-to-live for the lock.

Functions§

lock_across
Executes a function while locking on a single resource using the RedLock algorithm.
setup
Initializes a Redis instance with the Lua library functions required for locking.

Type Aliases§

RedisError
Re-export of redis::RedisError.
RedisResult
Mimic of redis::RedisResult.