Crates.io | mongo-lock-async |
lib.rs | mongo-lock-async |
version | 0.4.0 |
source | src |
created_at | 2023-01-09 21:07:50.691738 |
updated_at | 2024-07-04 22:30:58.166677 |
description | Distributed mutex locks with MongoDB |
homepage | |
repository | https://github.com/lazureykis/mongo-lock-async |
max_upload_size | |
id | 754787 |
size | 11,808 |
This crate contains only async implementation.
If you need a synchronous version, use mongo-lock
crate.
This implementation relies on system time. Ensure that NTP clients on your servers are configured properly.
Add this crate to Cargo.toml
[dependencies]
mongo_lock_async = "0"
#[tokio::main]
async fn main() {
let mongo = mongodb::Client::with_uri_str("mongodb://localhost").await.unwrap();
// We need to ensure that mongodb collection has a proper index.
mongo_lock_async::prepare_database(&mongo).await.unwrap();
if let Ok(Some(lock)) =
mongo_lock_async::Lock::try_acquire(
&mongo,
"my-key",
std::time::Duration::from_secs(30)
).await
{
println!("Lock acquired.");
// Release the lock before ttl expires to allow others to acquire it.
lock.release().await.ok();
}
}