Skip to content

Commit 288e0e8

Browse files
Ichanskiyartembilan
authored andcommitted
GH-9540: Add RedisLockRegistry.idleBetweenTries property
Fixes: #9540
1 parent 5398c5d commit 288e0e8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.redis.util;
1818

1919
import java.text.SimpleDateFormat;
20+
import java.time.Duration;
2021
import java.util.Collections;
2122
import java.util.Date;
2223
import java.util.LinkedHashMap;
@@ -99,8 +100,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
99100

100101
private static final int DEFAULT_CAPACITY = 100_000;
101102

103+
private static final int DEFAULT_IDLE = 100;
104+
102105
private final Lock lock = new ReentrantLock();
103106

107+
private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE);
108+
104109
private final Map<String, RedisLock> locks =
105110
new LinkedHashMap<>(16, 0.75F, true) {
106111

@@ -210,6 +215,17 @@ public void setCacheCapacity(int cacheCapacity) {
210215
this.cacheCapacity = cacheCapacity;
211216
}
212217

218+
/**
219+
* Specify a @link Duration} to sleep between obtainLock attempts.
220+
* Defaults to 100 milliseconds.
221+
* @param idleBetweenTries the {@link Duration} to sleep between obtainLock attempts.
222+
* @since 6.4.0
223+
*/
224+
public void setIdleBetweenTries(Duration idleBetweenTries) {
225+
Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
226+
this.idleBetweenTries = idleBetweenTries;
227+
}
228+
213229
/**
214230
* Set {@link RedisLockType} mode to work in.
215231
* By default, the {@link RedisLockType#SPIN_LOCK} is used - works in all the environment.
@@ -280,7 +296,7 @@ public void destroy() {
280296
public enum RedisLockType {
281297

282298
/**
283-
* The lock is acquired by periodically(100ms) checking whether the lock can be acquired.
299+
* The lock is acquired by periodically(idleBetweenTries property) checking whether the lock can be acquired.
284300
*/
285301
SPIN_LOCK,
286302

@@ -742,15 +758,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
742758
long now = System.currentTimeMillis();
743759
if (time == -1L) {
744760
while (!obtainLock()) {
745-
Thread.sleep(100); //NOSONAR
761+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
746762
}
747763
return true;
748764
}
749765
else {
750766
long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS);
751767
boolean acquired;
752768
while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
753-
Thread.sleep(100); //NOSONAR
769+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
754770
}
755771
return acquired;
756772
}

0 commit comments

Comments
 (0)