#redis #locking #distributed #redlock #dls

rslock

Implementation of the distributed locking mechanism built on top of Async Redis

7 unstable releases (3 breaking)

1.0.0-alpha.1 Aug 31, 2023
0.3.0 Feb 13, 2024
0.2.2 Sep 7, 2023
0.2.1 Aug 31, 2023
0.0.4 Sep 26, 2022

#315 in Database interfaces

Download history 300/week @ 2024-01-01 310/week @ 2024-01-08 266/week @ 2024-01-15 216/week @ 2024-01-22 305/week @ 2024-01-29 336/week @ 2024-02-05 322/week @ 2024-02-12 292/week @ 2024-02-19 340/week @ 2024-02-26 271/week @ 2024-03-04 403/week @ 2024-03-11 277/week @ 2024-03-18 361/week @ 2024-03-25 412/week @ 2024-04-01 175/week @ 2024-04-08 284/week @ 2024-04-15

1,238 downloads per month

BSD-3-Clause

26KB
492 lines

rslock - Redlock for Redis in Rust

Crates.io Docs badge

This is an implementation of Redlock, the distributed locking mechanism built on top of Redis.

Features

  • Lock extending
  • Async runtime support (tokio)
  • Async redis

Install

cargo add rslock

[!NOTE]
The default feature of this crate will provide async-std. You may optionally use tokio by supplying the tokio-comp feature flag when installing, but tokio has limitations that will not grant access to some parts of the API (read more here).

Build

cargo build --release

Usage

use rslock::LockManager;

#[tokio::main]
async fn main() {
    let rl = LockManager::new(vec![
        "redis://127.0.0.1:6380/",
        "redis://127.0.0.1:6381/",
        "redis://127.0.0.1:6382/",
    ]);

    let lock;
    loop {
        // Create the lock
        if let Ok(l) = rl.lock("mutex".as_bytes(), 1000).await {
            lock = l;
            break;
        }
    }

    // Extend the lock
    match rl.extend(&lock, 1000).await {
        Ok(_) => println!("lock extended!"),
        Err(_) => println!("lock couldn't be extended"),
    }

    // Unlock the lock
    rl.unlock(&lock).await;
}

Extending Locks

It should be noted that "extending" locks actually just renews them. For example, when you extend a 1000ms lock after 500ms have elapsed by another 1000ms, the lock will live for a total of 1500ms. It does not add additional time the the existing lock. This is how it was implemented in the Node.js version of Redlock and it will remain that way to be consistent. See the extend script.

Tests

Run tests with:

cargo test

Contribute

If you find bugs or want to help otherwise, please open an issue.

License

BSD. See LICENSE.

Dependencies

~6–19MB
~265K SLoC