1717package org .springframework .integration .redis .util ;
1818
1919import java .text .SimpleDateFormat ;
20+ import java .time .Duration ;
2021import java .util .Collections ;
2122import java .util .Date ;
2223import java .util .LinkedHashMap ;
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