Skip to content

Commit 1d4fbbc

Browse files
authored
GH-9540: Add RedisLockRegistry.idleBetweenTries property
Fixes: #9540
1 parent 30afdff commit 1d4fbbc

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.ConcurrentModificationException;
2223
import java.util.Date;
@@ -100,8 +101,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
100101

101102
private static final int DEFAULT_CAPACITY = 100_000;
102103

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

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

@@ -211,6 +216,17 @@ public void setCacheCapacity(int cacheCapacity) {
211216
this.cacheCapacity = cacheCapacity;
212217
}
213218

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.4.0
224+
*/
225+
public void setIdleBetweenTries(Duration idleBetweenTries) {
226+
Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
227+
this.idleBetweenTries = idleBetweenTries;
228+
}
229+
214230
/**
215231
* Set {@link RedisLockType} mode to work in.
216232
* By default, the {@link RedisLockType#SPIN_LOCK} is used - works in all the environment.
@@ -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

@@ -743,15 +759,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
743759
long now = System.currentTimeMillis();
744760
if (time == -1L) {
745761
while (!obtainLock()) {
746-
Thread.sleep(100); //NOSONAR
762+
Thread.sleep(idleBetweenTries.toMillis()); //NOSONAR
747763
}
748764
return true;
749765
}
750766
else {
751767
long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS);
752768
boolean acquired;
753769
while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
754-
Thread.sleep(100); //NOSONAR
770+
Thread.sleep(idleBetweenTries.toMillis()); //NOSONAR
755771
}
756772
return acquired;
757773
}

0 commit comments

Comments
 (0)