Skip to content

Commit cce28e4

Browse files
GH-9540: Add RedisLockRegistry.idleBetweenTries property
1 parent 5398c5d commit cce28e4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

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

Lines changed: 20 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;
@@ -87,6 +88,7 @@
8788
* @author Myeonghyeon Lee
8889
* @author Roman Zabaluev
8990
* @author Alex Peelman
91+
* @author Oleksandr Ichanskyi
9092
*
9193
* @since 4.0
9294
*
@@ -99,8 +101,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
99101

100102
private static final int DEFAULT_CAPACITY = 100_000;
101103

104+
private static final int DEFAULT_IDLE = 100;
105+
102106
private final Lock lock = new ReentrantLock();
103107

108+
private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE);
109+
104110
private final Map<String, RedisLock> locks =
105111
new LinkedHashMap<>(16, 0.75F, true) {
106112

@@ -210,6 +216,17 @@ public void setCacheCapacity(int cacheCapacity) {
210216
this.cacheCapacity = cacheCapacity;
211217
}
212218

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

282299
/**
283-
* The lock is acquired by periodically(100ms) checking whether the lock can be acquired.
300+
* The lock is acquired by periodically(idleBetweenTries property) checking whether the lock can be acquired.
284301
*/
285302
SPIN_LOCK,
286303

@@ -742,15 +759,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
742759
long now = System.currentTimeMillis();
743760
if (time == -1L) {
744761
while (!obtainLock()) {
745-
Thread.sleep(100); //NOSONAR
762+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
746763
}
747764
return true;
748765
}
749766
else {
750767
long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS);
751768
boolean acquired;
752769
while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
753-
Thread.sleep(100); //NOSONAR
770+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
754771
}
755772
return acquired;
756773
}

0 commit comments

Comments
 (0)