https://github.com/jonathanwoollett-light/redis-lock
Rusty distributed locking backed by Redis.
https://github.com/jonathanwoollett-light/redis-lock
Last synced: 7 months ago
JSON representation
Rusty distributed locking backed by Redis.
- Host: GitHub
- URL: https://github.com/jonathanwoollett-light/redis-lock
- Owner: JonathanWoollett-Light
- Created: 2024-10-22T10:27:39.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-10-24T10:47:11.000Z (8 months ago)
- Last Synced: 2024-10-25T04:31:55.886Z (8 months ago)
- Language: Rust
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# redis-lock
[](https://crates.io/crates/redis-lock)
[](https://docs.rs/redis-lock)Rusty distributed locking backed by Redis.
## Locking a single resource
```rust
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
```rust
// 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](https://github.com/hexcowboy/rslock)
I would recommend this library over [rslock](https://github.com/hexcowboy/rslock) when
- your application does operations that require exclusive access to multiple resources.
- you want to ensure the locks are freed without significantly impairing runtime performance[^1].## Similar work
- https://github.com/hexcowboy/rslock
[^1]: `rslock::LockGuard` notes:
Upon dropping the guard, LockManager::unlock will be ran synchronously on the executor.
This is known to block the tokio runtime if this happens inside of the context of a tokio runtime if tokio-comp is enabled as a feature on this crate or the redis crate.
To eliminate this risk, if the tokio-comp flag is enabled, the Drop impl will not be compiled, meaning that dropping the LockGuard will be a no-op. Under this circumstance, LockManager::unlock can be called manually using the inner lock at the appropriate point to release the lock taken in Redis.