rdlock

Crates.iordlock
lib.rsrdlock
version1.0.3
sourcesrc
created_at2024-10-24 00:40:50.820445
updated_at2024-10-25 03:43:40.152816
descriptionA library for distributed redis locks written in rust
homepage
repositoryhttps://github.com/RedaOps/rdlock
max_upload_size
id1420734
size25,206
Tudor (RedaOps)

documentation

README

rdlock

Crates.io License: MIT

A library for distributed redis locks written in rust.

These are some useful synchronization primitives which work among multiple separate processes, servers and clusters, as long as they communicate with the same redis server.

The redis instance must have notify-keyspace-events enabled with at least KA flags. If this is too much overhead for your current instance, consider running a separate redis instance just for the distributed locks.

Usage and examples on docs.rs

Supported primitives

  • BasicLock - a basic type of distributed lock with no protected data. This is the most basic synchronization primitive.

TLDR

It uses redis SET with the NX option enabled for creating a "lockfile".

Any lock function that waits for the lock to become available (e.g: .lock() or .lock_timeout()) will use redis pubsub in order to monitor for changes and try to acquire the "lockfile" once it is available.

Known issues

  • The local TTL check, which checks if the TTL of the specific lock expired before doing any extra actions, is not perfect, since it's only a local sanity TTL check, and doesn't verify TTL in any way with the redis instance.
  • If the pubsub drops while waiting for a lock and/or we miss a message, we will never acquire it, since redis pubsub is fire and forget.

TODO

  • Move generic lock functions that will be used in all locks out of src/lock/mod.rs

Types of locks we want implemented:

  • Mutex
  • RwLock

Footnotes

Please note this is a work in progress and for starters I will be implementing only what I need or want to have fun with. If there is a certain synchronization primitive that is not available and you need it or you want to implement it for fun, contributions are always welcome :)

Commit count: 13

cargo fmt