| Crates.io | mongo-lock |
| lib.rs | mongo-lock |
| version | 0.3.0 |
| created_at | 2023-01-09 05:58:56.046509+00 |
| updated_at | 2023-06-22 15:37:08.660163+00 |
| description | Distributed mutex locks with MongoDB |
| homepage | |
| repository | https://github.com/lazureykis/mongo-lock |
| max_upload_size | |
| id | 754138 |
| size | 12,066 |
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.
Add this crate to Cargo.toml
[dependencies]
mongo_lock = "0"
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.
}
}