|
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.ConcurrentModificationException; |
22 | 23 | import java.util.Date; |
@@ -100,8 +101,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl |
100 | 101 |
|
101 | 102 | private static final int DEFAULT_CAPACITY = 100_000; |
102 | 103 |
|
| 104 | + private static final int DEFAULT_IDLE = 100; |
| 105 | + |
103 | 106 | private final Lock lock = new ReentrantLock(); |
104 | 107 |
|
| 108 | + private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE); |
| 109 | + |
105 | 110 | private final Map<String, RedisLock> locks = |
106 | 111 | new LinkedHashMap<>(16, 0.75F, true) { |
107 | 112 |
|
@@ -211,6 +216,17 @@ public void setCacheCapacity(int cacheCapacity) { |
211 | 216 | this.cacheCapacity = cacheCapacity; |
212 | 217 | } |
213 | 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.4.0 |
| 224 | + */ |
| 225 | + public void setIdleBetweenTries(Duration idleBetweenTries) { |
| 226 | + Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null"); |
| 227 | + this.idleBetweenTries = idleBetweenTries; |
| 228 | + } |
| 229 | + |
214 | 230 | /** |
215 | 231 | * Set {@link RedisLockType} mode to work in. |
216 | 232 | * By default, the {@link RedisLockType#SPIN_LOCK} is used - works in all the environment. |
@@ -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 |
|
@@ -743,15 +759,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException { |
743 | 759 | long now = System.currentTimeMillis(); |
744 | 760 | if (time == -1L) { |
745 | 761 | while (!obtainLock()) { |
746 | | - Thread.sleep(100); //NOSONAR |
| 762 | + Thread.sleep(idleBetweenTries.toMillis()); //NOSONAR |
747 | 763 | } |
748 | 764 | return true; |
749 | 765 | } |
750 | 766 | else { |
751 | 767 | long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS); |
752 | 768 | boolean acquired; |
753 | 769 | while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR |
754 | | - Thread.sleep(100); //NOSONAR |
| 770 | + Thread.sleep(idleBetweenTries.toMillis()); //NOSONAR |
755 | 771 | } |
756 | 772 | return acquired; |
757 | 773 | } |
|
0 commit comments