5 releases

0.6.1 Nov 12, 2023
0.6.0 Nov 7, 2023
0.4.3 Mar 1, 2023
0.4.2 Jan 2, 2023
0.4.1 May 16, 2022

#363 in Database interfaces

Download history 1333/week @ 2024-01-03 1977/week @ 2024-01-10 2162/week @ 2024-01-17 2156/week @ 2024-01-24 1603/week @ 2024-01-31 1385/week @ 2024-02-07 1802/week @ 2024-02-14 1546/week @ 2024-02-21 1521/week @ 2024-02-28 1523/week @ 2024-03-06 1536/week @ 2024-03-13 1467/week @ 2024-03-20 1510/week @ 2024-03-27 1258/week @ 2024-04-03 1598/week @ 2024-04-10 588/week @ 2024-04-17

5,239 downloads per month

Apache-2.0

43KB
696 lines

dynamodb lock

Dynamodb based distributed lock implemented in pure Rust. The design is largely inspired by amazon-dynamodb-lock-client.

It is used by the delta-rs project to implement PUT if absence semantic for concurrent S3 writes. It is considered production ready and battle tested through the kafka-delta-ingest project.

Usage

let region = dynamodb_lock::Region::default();
// This will use the default options
let lock_client = dynamodb_lock::DynamoDbLockClient::for_region(region);

let lock_data = "Moe";
let lock = lock_client.try_acquire_lock(Some(lock_data)).await?.unwrap();

if lock.acquired_expired_lock {
    // error handling when acquired an expired lock
}

// do stuff in the critical region

lock_client.release_lock(&lock).await?;

For real world example, see https://github.com/delta-io/delta-rs/blob/main/rust/src/storage/s3/mod.rs.

Deployment

Using the DynamoDb lock requires a DynamoDb table to have already been created in AWS. The following Terraform code is an example which will create table named "lock_example",.

resource "aws_dynamodb_table" "oxbow_locking" {
  name         = "lock_example"
  billing_mode = "PROVISIONED"
  # Default name of the partition key hard-coded in delta-rs
  hash_key       = "key"
  read_capacity  = 10
  write_capacity = 10

  attribute {
    name = "key"
    type = "S"
  }
  # The leaseDuration is used by dynamodb-lock-rs and *must* be a Number type
  attribute {
    name = "leaseDuration"
    type = "N"
  }
  ttl {
    attribute_name = "leaseDuration"
    enabled        = true
  }
}

The locking code should then be configured with the environment variable DYNAMO_LOCK_TABLE_NAME set to "lock_example"

Dependencies

~10–27MB
~390K SLoC