百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rslock

> 数据库
开源

分布式锁以 Aync Redis 支持锁扩展(Redlock 执行)

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

分布式锁以 Aync Redis 支持锁扩展(Redlock 执行)

rslock - Redlock for Redis in Rust

Docs badge

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

Features

  • Lock extending
  • Smol and Tokio Redis I/O backends
  • Async Redis
  • Redis Cluster support

Install

[!WARNING] Before release 1.0.0, this crate will have breaking changes between minor versions. You can upgrade to patch versions without worrying about breaking changes.

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@~0.9.1"

[!NOTE] The default async-std-comp feature uses Redis's Smol backend with Rustls. The feature name is retained for compatibility. For standalone Redis, you can select Redis's Tokio backend instead by disabling the default features:

bash
cargo add "rslock@~0.9.1" --no-default-features --features tokio-comp

Build

cargo build --release

Usage

The example below uses Tokio. Select the tokio-comp backend as shown above and add Tokio to your application:

bash
cargo add tokio --features macros,rt-multi-thread
…

More examples:

  • Basic locking
  • Creating a manager from Redis clients
  • Sharing a lock between tasks
  • Using Redis Cluster

Redis Cluster

Enable the cluster feature and pass one or more seed-node URIs for one logical cluster. The cluster counts as one backend when rslock calculates quorum:

bash
cargo add "rslock@~0.9.1" --features cluster

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. See the extend script.

Tests

The integration tests use Testcontainers and require Docker. Run both the all-features and default-feature test configurations:

bash
cargo test --all-features --all-targets
cargo test --all-targets

Running the examples

Start the standalone Redis servers used by the first three examples:

bash
docker compose -f examples/docker-compose.yml up -d

Run the standalone Redis examples:

bash
cargo run --example basic
cargo run --example shared_lock
cargo run --example from_clients

Stop the redis servers:

bash
docker compose -f examples/docker-compose.yml down

To run the Redis Cluster example, first start a Redis Cluster with seed nodes on ports 7000–7002, then run:

bash
cargo run --example cluster --features cluster

Contribute

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

License

BSD. See LICENSE.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustdistributed-lockdlsredisredlock

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库