Expand description
A lease based distributed lock with support for fencing tokens
DLock uses a lease based algorithm to provide locking mechanisms for distributed clients. DLock also provides a fencing token that can be used to prevent a stale lock from erroneously being use by a client.
§Examples
You can use the automatic lease renewal mechanism built into DLock or manually manage the lease yourself:
§Automatic
DLock::with will acquire, automatically renew, and release the lease. For more information see the function documentation.
use dlock::{DLock, DynamodbProvider};
use std::time::Duration;
const TABLE_NAME: &str = "dynamodb_table";
#[tokio::main]
async fn main() {
let config = aws_sdk_dynamodb::config::Builder::new().build();
let client = aws_sdk_dynamodb::Client::from_conf(config);
let provider = DynamodbProvider::builder()
.client(client)
.table_name(TABLE_NAME.to_string())
.build();
let lock = DLock::builder()
.name("test_lock".to_string())
.owner("test_owner".to_string())
.duration(Duration::from_secs(1))
.provider(provider)
.build();
let result = lock.with(async |token| {
// do synchronized work!
}).await;
}§Manually
You are responsible for acquiring, renewing, and releasing the lease.
use dlock::{DLock, DynamodbProvider, Lease};
use std::time::Duration;
use std::error::Error;
const TABLE_NAME: &str = "dynamodb_table";
#[tokio::main]
async fn main() {
let config = aws_sdk_dynamodb::config::Builder::new().build();
let client = aws_sdk_dynamodb::Client::from_conf(config);
let provider = DynamodbProvider::builder()
.client(client)
.table_name(TABLE_NAME.to_string())
.build();
let lock = DLock::builder()
.name("test_lock".to_string())
.owner("test_owner".to_string())
.duration(Duration::from_secs(1))
.provider(provider)
.build();
let lease = lock.acquire().await.unwrap();
// do synchronized work!
lease.release().await.unwrap();
}Re-exports§
pub use providers::dynamodb::DynamodbLease;pub use providers::dynamodb::DynamodbProvider;pub use providers::dynamodb::DynamodbRetry;pub use providers::Lease;pub use providers::Provider;
Modules§
Structs§
- DLock
- DLock
Builder - Use builder syntax to set the inputs and finish with
build().