async-redis-lock
A simple and easy-to-use asynchronous redis distributed read-write lock implementation based on tokio and redis-rs.
Features:
- Automatic extension: When a lock is acquired, the lifetime of the lock will be automatically extended in background
until the lock is released.
- Passive release: When progress exit abnormally, the lock will be automatically released the lifetime is exhausted.
- Drop support: Lock can be released implicitly by drop as well as explicitly by call release method.
Examples
1. General usage
use async_redis_lock::Locker;
use async_redis_lock::options::Options;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut locker = Locker::from_redis_url("redis://127.0.0.1:6379/0").await?;
let lock_key = "lock_key";
let lock = locker.acquire(lock_key).await?;
sleep(Duration::from_secs(5)).await;
lock.release()?;
{
let _lock = locker.acquire(lock_key).await?;
}
let opts = Options::new()
.lifetime(Duration::from_secs(5))
.retry_interval(Duration::from_secs(1))
.retry_timeout(Some(Duration::from_secs(3)))
.extend_interval(Duration::from_secs(3));
locker.acquire_with_options(&opts, lock_key).await?;
Ok(())
}