Skip to content

lazureykis/mongo-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b24be2a · Jun 22, 2023

History

14 Commits
Jun 22, 2023
Jan 9, 2023
Jun 22, 2023
Jan 10, 2023

Repository files navigation

Distributed mutually exclusive locks in MongoDB.

Crates.io docs.rs

This crate contains only sync implementation. If you need a async version, use mongo-lock-async crate.

This implementation relies on system time. Ensure that NTP clients on your servers are configured properly.

Installation

Add this crate to Cargo.toml

[dependencies]
mongo_lock = "0"

Usage

fn main() {
    let mongo = mongodb::sync::Client::with_uri_str("mongodb://localhost").unwrap();

    // We need to ensure that mongodb collection has a proper index.
    mongo_lock::prepare_database(&mongo).unwrap();

    if let Ok(Some(lock)) =
        mongo_lock::Lock::try_acquire(
            &mongo,
            "my-key",
            std::time::Duration::from_secs(30)
        )
    {
        println!("Lock acquired.");

        // The lock will be released automatically after leaving the scope.
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages