Skip to content

Commit 7255e09

Browse files
committed
GH-205: Skip block in DynamoDbLockReg.tryLock()
Fixes #205 The `AmazonDynamoDBLockClient.acquireLock()` steps into a busy-wait loop with a sleep timeout. * Use `AcquireLockOptions.shouldSkipBlockingWait = true` for `tryLock()` without timeout to have an immediate answer according `tryLock()` contract * Reset flag to `false` for all other use-cases with `AcquireLockOptions` **Cherry-pick to `2.5.x`**
1 parent 055307e commit 7255e09

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@
5656
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
5757
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
5858
import com.amazonaws.services.dynamodbv2.model.KeyType;
59+
import com.amazonaws.services.dynamodbv2.model.LockCurrentlyUnavailableException;
5960
import com.amazonaws.services.dynamodbv2.model.LockNotGrantedException;
6061
import com.amazonaws.services.dynamodbv2.model.LockTableDoesNotExistException;
6162
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
@@ -450,7 +451,8 @@ public void lock() {
450451
private void setupDefaultAcquireLockOptionsBuilder() {
451452
this.acquireLockOptionsBuilder
452453
.withAdditionalTimeToWaitForLock(Long.MAX_VALUE - DynamoDbLockRegistry.this.leaseDuration)
453-
.withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod);
454+
.withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod)
455+
.withShouldSkipBlockingWait(false);
454456
}
455457

456458
@Override
@@ -505,7 +507,12 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
505507
.max(TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start, 0L);
506508

507509
this.acquireLockOptionsBuilder.withAdditionalTimeToWaitForLock(additionalTimeToWait)
508-
.withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod);
510+
.withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod)
511+
.withShouldSkipBlockingWait(false);
512+
513+
if (additionalTimeToWait == 0) {
514+
this.acquireLockOptionsBuilder.withShouldSkipBlockingWait(true);
515+
}
509516

510517
boolean acquired = false;
511518
try {
@@ -518,6 +525,10 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
518525
this.lastUsed = System.currentTimeMillis();
519526
}
520527
}
528+
catch (LockCurrentlyUnavailableException ex) {
529+
this.delegate.unlock();
530+
logger.trace("The lock '" + this + "' cannot be acquired at the moment", ex);
531+
}
521532
catch (Exception e) {
522533
this.delegate.unlock();
523534
rethrowAsLockException(e);

0 commit comments

Comments
 (0)