Skip to content

Commit

Permalink
GH-97: wait at least leaseDuration to acquire the lock
Browse files Browse the repository at this point in the history
Fixes #97

This fixes an issue where `additionalTimeToWait` has a negative value
that is subtracted later by the Amazon DynamoDB client to
`leaseDuration`, making it impossible to acquire the lock if expired.

Also allow to use the existing `refreshPeriod` properties in this case.

* Use default refresh period in tryLock()

The 'refreshPeriod' value might not satisfy both tryLock() and lock()
requirements and should probably be used exclusively in the second case.

We can make the value used for tryLock() configurable as well in another
pull request.

* Rollback previous commit and add author name
  • Loading branch information
karllessard authored and artembilan committed Sep 21, 2018
1 parent 857d184 commit ed89f12
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* Can create table in DynamoDB if an external {@link AmazonDynamoDBLockClient} is not provided.
*
* @author Artem Bilan
* @author Karl Lessard
*
* @since 2.0
*/
Expand Down Expand Up @@ -467,11 +468,11 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return false;
}

long additionalTimeToWait = TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start;
long additionalTimeToWait = Math.max(TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start, 0L);

this.acquireLockOptionsBuilder
.withAdditionalTimeToWaitForLock(additionalTimeToWait)
.withRefreshPeriod(0L);
.withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod);

boolean acquired = false;
try {
Expand Down

0 comments on commit ed89f12

Please sign in to comment.