redis-lock 0.2.0

Rusty distributed locking backed by Redis.
Documentation
redis-lock-0.2.0 has been yanked.

redis-lock

Crates.io docs

Rusty distributed locking backed by Redis.

// Setup.
redis_lock::setup(&client).await?;
let mut lock = redis_lock::MultiResourceLock::new(client.clone())?;
let mut conn = client.get_multiplexed_async_connection().await?;
let from = "account1";
let to = "account2";
let resources = vec![String::from(from), String::from(to)];
// Acquire lock.
let opt = lock.lock_default(&resources).await?;
let guard = opt.ok_or("timed out")?;
// Perform transfer.
let amount = 500;
let from_balance: i64 = conn.get("account1").await?;
// Execute transaction if the sender has enough funds.
if from_balance >= amount {
    let to_balance: i64 = conn.get(to).await?;
    let new_from = from_balance.checked_sub(amount).ok_or("underflow")?;
    conn.set(from, new_from).await?;
    let new_to = to_balance.checked_add(amount).ok_or("overflow")?;
    conn.set(to, new_to).await?;
}
// Lock releases when dropped.

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