Skip to main content

Crate justalock_client

Crate justalock_client 

Source
Expand description

justalock_client is a simple distributed lock backed by the justalock service.

The core functionality of this library allows you to hold a lock in order to perform work while the lock is held.

§Example

Here is a simple example of trying to hold a lock in order to calculate data on a regular cadence:

let lock = Lock::builder(lock_id).build().unwrap();
let result = lock.locked(|cancellation_token| async move {
    let mut counter = 0u64;
    loop {
        tokio::select! {
            _ = cancellation_token.cancelled() => return counter,
            _ = tokio::time::sleep(Duration::from_secs(4)) => {
                record_data().await;
                counter += 1;
            },
        }
    }
}).await;
match result {
    Ok(data_record_count) => {
        println!("Recorded data {data_record_count} times before losing the lock!");
    }
    Err(error) => {
        eprintln!("Error while obtaining the lock: {error:?}");
    }
}

Structs§

Lock
A distributed lock and its client-unique data.
LockBuilder
Used to create a Lock with advanced configuration.
LockID
The ID of a distrubuted lock.

Enums§

Error
Errors that can occur during locking.

Traits§

IntoLockID
A trait to convert some type into a LockID.