From 1e1e1acc392de171df06c3b9b4f3cad081870dbc Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 20 Jul 2018 12:44:51 -0400 Subject: [PATCH] Fix DynamoDbLockRegistry.tryLock for "fair" time Since we don't have choice and wait at minimum `leaseDuration` to be able to iterate in the Amazon LockClient at least two times and that is going to be just a contract of this client, so we need to add up a `timeout` for the `tryLock` to that `leaseDuration` --- .../integration/aws/lock/DynamoDbLockRegistry.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java index 1471704..f2ec880 100644 --- a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java +++ b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java @@ -448,8 +448,6 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing @Override public boolean tryLock() { - awaitForActive(); - try { return tryLock(0, TimeUnit.MILLISECONDS); } @@ -461,14 +459,18 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { + long start = System.currentTimeMillis(); + awaitForActive(); if (!this.delegate.tryLock(time, unit)) { return false; } + long additionalTimeToWait = TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start; + this.acquireLockOptionsBuilder - .withAdditionalTimeToWaitForLock(0L) + .withAdditionalTimeToWaitForLock(additionalTimeToWait) .withRefreshPeriod(0L); boolean acquired = false;