| Crates.io | rds_lock |
| lib.rs | rds_lock |
| version | 1.0.5 |
| created_at | 2025-04-05 10:05:16.006765+00 |
| updated_at | 2025-09-04 04:28:40.47197+00 |
| description | A simple and easy-to-use asynchronous redis distributed read-write lock implementation based on tokio and redis-rs. |
| homepage | |
| repository | https://github.com/imstevez/rds_lock |
| max_upload_size | |
| id | 1621928 |
| size | 47,152 |
A simple and easy-to-use asynchronous redis distributed read-write lock implementation based on tokio and redis-rs.
It supports the following features:
use rds_lock::{Locker, Mode};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = redis::Client::open("redis://127.0.0.1:6379")?;
let con = redis::aio::ConnectionManager::new(cli).await?;
let key = "lock_key".into();
// Lock key with default write mode, and
// the passive timeout will be automatically extend
// util this lock unlocked.
let w_unlock = Locker::new(con.clone()).lock(key.clone()).await?;
// Do something with lock guard.
for x in 1..10 {
println!("{}", x);
}
// When key is locked in write mode, the other write or read lock should fail.
assert!(Locker::new(con.clone()).mode(&Mode::W).lock(key.clone()).await.is_err());
assert!(Locker::new(con.clone()).mode(&Mode::R).lock(key.clone()).await.is_err());
// Explicit unlock is required.
// In most cases you should ignore unlock errors.
let _ = w_unlock.await?;
Ok(())
}
use rds_lock::{Locker, Mode};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = redis::Client::open("redis://127.0.0.1:6379")?;
let con = redis::aio::ConnectionManager::new(cli).await?;
let key = "lock_key".into();
// Do something in closure with lock guard, no explicit unlock needed.
// If the lock is successful, lock_exec finally returns
// the return value of the closure.
Locker::new(con.clone()).lock_exec(key.clone(), async {
for x in 1..10 {
println!("{}", x);
}
Ok(())
}).await
}
use rds_lock::{Locker, Mode};
//!
#[tokio::main]
async fn main() -> anyhow::Result<i32> {
let cli = redis::Client::open("redis://127.0.0.1:6379/0")?;
let con = redis::aio::ConnectionManager::new(cli).await?;
let key = "lock_key".into();
let locker = Locker::new(con)
.mode(&Mode::R) // Set lock mode, default write-mode.
.to(2000) // Set milliseconds of lock passive timeout, default 3000.
.rty_int(200) // Set milliseconds of retry lock interval milliseconds, default 100.
.rty_to(1500) // Set milliseconds of retry lock timeout milliseconds, default 1000, if set to -1, means retry never timed out.
.ext_int(800); // Set milliseconds of extend lock interval, default 1000.
// Do something with lock guard
locker.lock_exec(key, async {
let mut r = 0;
for x in 1..10 {
r += x;
}
Ok(r)
})
}