Skip to content

Commit ca1f66e

Browse files
authored
GH-9540: Add RedisLockRegistry.idleBetweenTries property
Fixes: #9540 Issue link: #9540 **Auto-cherry-pick to `6.3.x`**
1 parent 7a348fa commit ca1f66e

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;
@@ -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,16 @@ 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.2.10
224+
*/
225+
public void setIdleBetweenTries(Duration idleBetweenTries) {
226+
Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
227+
this.idleBetweenTries = idleBetweenTries;
228+
}
213229

214230
/**
215231
* Set {@link RedisLockType} mode to work in.
@@ -281,7 +297,7 @@ public void destroy() {
281297
public enum RedisLockType {
282298

283299
/**
284-
* 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.
285301
*/
286302
SPIN_LOCK,
287303

@@ -744,15 +760,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
744760
long now = System.currentTimeMillis();
745761
if (time == -1L) {
746762
while (!obtainLock()) {
747-
Thread.sleep(100); //NOSONAR
763+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
748764
}
749765
return true;
750766
}
751767
else {
752768
long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS);
753769
boolean acquired;
754770
while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
755-
Thread.sleep(100); //NOSONAR
771+
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
756772
}
757773
return acquired;
758774
}

0 commit comments

Comments
 (0)