|
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; |
@@ -99,8 +100,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl |
99 | 100 |
|
100 | 101 | private static final int DEFAULT_CAPACITY = 100_000; |
101 | 102 |
|
| 103 | + private static final int DEFAULT_IDLE = 100; |
| 104 | + |
102 | 105 | private final Lock lock = new ReentrantLock(); |
103 | 106 |
|
| 107 | + private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE); |
| 108 | + |
104 | 109 | private final Map<String, RedisLock> locks = |
105 | 110 | new LinkedHashMap<>(16, 0.75F, true) { |
106 | 111 |
|
@@ -210,6 +215,17 @@ public void setCacheCapacity(int cacheCapacity) { |
210 | 215 | this.cacheCapacity = cacheCapacity; |
211 | 216 | } |
212 | 217 |
|
| 218 | + /** |
| 219 | + * Specify a @link Duration} to sleep between obtainLock attempts. |
| 220 | + * Defaults to 100 milliseconds. |
| 221 | + * @param idleBetweenTries the {@link Duration} to sleep between obtainLock attempts. |
| 222 | + * @since 6.4.0 |
| 223 | + */ |
| 224 | + public void setIdleBetweenTries(Duration idleBetweenTries) { |
| 225 | + Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null"); |
| 226 | + this.idleBetweenTries = idleBetweenTries; |
| 227 | + } |
| 228 | + |
213 | 229 | /** |
214 | 230 | * Set {@link RedisLockType} mode to work in. |
215 | 231 | * By default, the {@link RedisLockType#SPIN_LOCK} is used - works in all the environment. |
@@ -280,7 +296,7 @@ public void destroy() { |
280 | 296 | public enum RedisLockType { |
281 | 297 |
|
282 | 298 | /** |
283 | | - * The lock is acquired by periodically(100ms) checking whether the lock can be acquired. |
| 299 | + * The lock is acquired by periodically(idleBetweenTries property) checking whether the lock can be acquired. |
284 | 300 | */ |
285 | 301 | SPIN_LOCK, |
286 | 302 |
|
@@ -742,15 +758,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException { |
742 | 758 | long now = System.currentTimeMillis(); |
743 | 759 | if (time == -1L) { |
744 | 760 | while (!obtainLock()) { |
745 | | - Thread.sleep(100); //NOSONAR |
| 761 | + Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR |
746 | 762 | } |
747 | 763 | return true; |
748 | 764 | } |
749 | 765 | else { |
750 | 766 | long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS); |
751 | 767 | boolean acquired; |
752 | 768 | while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR |
753 | | - Thread.sleep(100); //NOSONAR |
| 769 | + Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR |
754 | 770 | } |
755 | 771 | return acquired; |
756 | 772 | } |
|
0 commit comments