Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rslock"
version = "0.8.0"
version = "0.9.0"
authors = [
"Jan-Erik Rediger <badboy@archlinux.us>",
"Romain Boces <bocesr@gmail.com>",
Expand All @@ -21,6 +21,7 @@ edition = "2021"
[features]
async-std-comp = ["redis/smol-rustls-comp"]
tokio-comp = ["redis/tokio-rustls-comp"]
cluster = ["async-std-comp", "redis/cluster-async"]
default = ["async-std-comp"]

[dependencies]
Expand All @@ -36,3 +37,7 @@ testcontainers = "0.27.3"
anyhow = "1.0.104"
tokio = { version = "1.53.1", features = ["macros", "rt-multi-thread"] }
tokio-test = "0.4.5"

[[example]]
name = "cluster"
required-features = ["cluster"]
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This is an implementation of Redlock, the [distributed locking mechanism](http:/
- Lock extending
- Async runtime support (async-std and tokio)
- Async redis
- Redis Cluster support

## Install

Expand All @@ -18,7 +19,7 @@ This is an implementation of Redlock, the [distributed locking mechanism](http:/

```bash
# It is recommended to pin the version to a minor release, as breaking changes may be introduced between minor versions before 1.0.0.
cargo add rslock --vers "~0.8.0"
cargo add rslock --vers "~0.9.0"
```

> [!NOTE]
Expand Down Expand Up @@ -73,6 +74,25 @@ async fn main() {
}
```

### Redis Cluster

Enable the `cluster` feature and pass one or more seed-node URIs for the same cluster:

```bash
cargo add rslock --vers "~0.9.0" --features cluster
```

```rust
let manager = LockManager::new_cluster(vec![
"redis://127.0.0.1:7000/",
"redis://127.0.0.1:7001/",
"redis://127.0.0.1:7002/",
])?;
```

For authentication, TLS, address mapping, and other advanced configuration, build a
`redis::cluster::ClusterClient` and pass it to `LockManager::from_cluster_client`.

## Extending Locks

Extending a lock effectively renews its duration instead of adding extra time to it. For instance, if a 1000ms lock is extended by 1000ms after 500ms pass, it will only last for a total of 1500ms, not 2000ms. This approach is consistent with the [Node.js Redlock implementation](https://www.npmjs.com/package/redlock). See the [extend script](https://github.com/hexcowboy/rslock/blob/main/src/lock.rs#L22-L30).
Expand All @@ -99,6 +119,7 @@ Run the examples:
cargo run --example basic
cargo run --example shared_lock
cargo run --example from_clients
cargo run --example cluster --features cluster
```

Stop the redis servers:
Expand Down
23 changes: 23 additions & 0 deletions examples/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use rslock::LockManager;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// These are seed nodes for one logical Redis Cluster, not independent Redlock servers.
// The cluster client discovers the rest of the topology automatically.
let manager = LockManager::new_cluster(vec![
"redis://127.0.0.1:7000/",
"redis://127.0.0.1:7001/",
"redis://127.0.0.1:7002/",
])?;

let lock = manager
.lock("cluster-example", Duration::from_secs(10))
.await?;

println!("Lock acquired");
manager.unlock(&lock).await;
println!("Lock released");

Ok(())
}
Loading
Loading