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`
This commit is contained in:
Artem Bilan
2018-07-20 12:44:51 -04:00
parent dd52e8f304
commit 1e1e1acc39

View File

@@ -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;