Skip to content

Commit

Permalink
Merge pull request #4 from delta-incubator/leaseDuration-3
Browse files Browse the repository at this point in the history
Set the TTL attribute properly
  • Loading branch information
rtyler authored Nov 7, 2023
2 parents f4e21a8 + 16b8b8d commit 0101dd0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "dynamodb_lock"
description = "Distributed lock backed by Dynamodb"
version = "0.5.0"
version = "0.6.0"
authors = [
"Mykhailo Osypov <[email protected]>",
"R Tyler Croy <[email protected]>",
Expand All @@ -26,6 +26,9 @@ rusoto_sts = { version ="0.47", default-features = false, optional = true }
maplit = "1"
tokio = { version = "1", features = ["fs", "macros", "rt", "io-util"] }

[dev-dependencies]
pretty_env_logger = "*"

[features]
default = ["rustls"]
sts = ["rusoto_sts",
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ Usage
-----

```rust
let dynamodb_client = rusoto_dynamodb::DynamoDbClient::new(rusoto_core::Region::default());
let lock_client = dynamodb_lock::DynamoDbLockClient::new(
dynamodb_client,
dynamodb_lock::DynamoDbOptions::default(),
);
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(lock_data).await?.unwrap();
let lock = lock_client.try_acquire_lock(Some(lock_data)).await?.unwrap();

if lock.acquired_expired_lock {
// error handling when acquired an expired lock
Expand Down
23 changes: 23 additions & 0 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use dynamodb_lock::*;

#[tokio::main]
async fn main() -> Result<(), DynamoError> {
pretty_env_logger::init();
let region = Region::default();
// This will use the default options
let lock_client = 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?;
Ok(())
}
44 changes: 43 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl DynamoDbLockClient {
PARTITION_KEY_NAME.to_string() => attr(self.opts.partition_key_value.clone()),
OWNER_NAME.to_string() => attr(&self.opts.owner_name),
RECORD_VERSION_NUMBER.to_string() => attr(&rvn),
LEASE_DURATION.to_string() => attr(self.opts.lease_duration),
LEASE_DURATION.to_string() => num_attr(lease_duration_after(self.opts.lease_duration)),
};

if let Some(d) = data {
Expand Down Expand Up @@ -590,6 +590,15 @@ impl DynamoDbLockClient {
}
}

/// Return a u64 lease duration the given seconds from the current time
fn lease_duration_after(after: u64) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
+ after
}

fn now_millis() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -605,6 +614,13 @@ fn attr<T: ToString>(s: T) -> AttributeValue {
}
}

fn num_attr<T: ToString>(s: T) -> AttributeValue {
AttributeValue {
n: Some(s.to_string()),
..Default::default()
}
}

fn get_string(attr: Option<&AttributeValue>) -> Result<String, DynamoError> {
Ok(attr
.and_then(|r| r.s.as_ref())
Expand Down Expand Up @@ -843,4 +859,30 @@ mod tests {
options
);
}

#[test]
fn test_lease_duration_after() {
use std::time::SystemTime;
let now = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n.as_secs(),
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
};
let duration: u64 = lease_duration_after(60);

assert!(duration > now);
assert!(duration >= (now + 60));
assert!(duration <= (now + 70));
}

#[test]
fn test_lease_duration_attr() {
let n = num_attr(1);
assert!(n.n.is_some());
if let Some(num) = n.n {
assert_eq!(1, num.parse::<u64>().unwrap());
} else {
println!("attr {n:?}");
assert!(false);
}
}
}

0 comments on commit 0101dd0

Please sign in to comment.