17
17
package org .springframework .integration .redis .util ;
18
18
19
19
import java .text .SimpleDateFormat ;
20
+ import java .time .Duration ;
20
21
import java .util .Collections ;
21
22
import java .util .Date ;
22
23
import java .util .LinkedHashMap ;
87
88
* @author Myeonghyeon Lee
88
89
* @author Roman Zabaluev
89
90
* @author Alex Peelman
91
+ * @author Oleksandr Ichanskyi
90
92
*
91
93
* @since 4.0
92
94
*
@@ -99,8 +101,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
99
101
100
102
private static final int DEFAULT_CAPACITY = 100_000 ;
101
103
104
+ private static final int DEFAULT_IDLE = 100 ;
105
+
102
106
private final Lock lock = new ReentrantLock ();
103
107
108
+ private Duration idleBetweenTries = Duration .ofMillis (DEFAULT_IDLE );
109
+
104
110
private final Map <String , RedisLock > locks =
105
111
new LinkedHashMap <>(16 , 0.75F , true ) {
106
112
@@ -210,6 +216,16 @@ public void setCacheCapacity(int cacheCapacity) {
210
216
this .cacheCapacity = cacheCapacity ;
211
217
}
212
218
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
+ }
213
229
214
230
/**
215
231
* Set {@link RedisLockType} mode to work in.
@@ -281,7 +297,7 @@ public void destroy() {
281
297
public enum RedisLockType {
282
298
283
299
/**
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.
285
301
*/
286
302
SPIN_LOCK ,
287
303
@@ -744,15 +760,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
744
760
long now = System .currentTimeMillis ();
745
761
if (time == -1L ) {
746
762
while (!obtainLock ()) {
747
- Thread .sleep (100 ); //NOSONAR
763
+ Thread .sleep (RedisLockRegistry . this . idleBetweenTries . toMillis () ); //NOSONAR
748
764
}
749
765
return true ;
750
766
}
751
767
else {
752
768
long expire = now + TimeUnit .MILLISECONDS .convert (time , TimeUnit .MILLISECONDS );
753
769
boolean acquired ;
754
770
while (!(acquired = obtainLock ()) && System .currentTimeMillis () < expire ) { //NOSONAR
755
- Thread .sleep (100 ); //NOSONAR
771
+ Thread .sleep (RedisLockRegistry . this . idleBetweenTries . toMillis () ); //NOSONAR
756
772
}
757
773
return acquired ;
758
774
}
0 commit comments