From ca1f66eaf1b6ba945ec348a0568fc3e42442e214 Mon Sep 17 00:00:00 2001 From: Oleksandr Ichanskyi Date: Wed, 9 Oct 2024 21:11:50 +0200 Subject: [PATCH] GH-9540: Add RedisLockRegistry.idleBetweenTries property Fixes: #9540 Issue link: https://github.com/spring-projects/spring-integration/issues/9540 **Auto-cherry-pick to `6.3.x`** --- .../redis/util/RedisLockRegistry.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java index fe881308a0..c3c6bbd7e3 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java @@ -17,6 +17,7 @@ package org.springframework.integration.redis.util; import java.text.SimpleDateFormat; +import java.time.Duration; import java.util.Collections; import java.util.Date; import java.util.LinkedHashMap; @@ -87,6 +88,7 @@ import org.springframework.util.ReflectionUtils; * @author Myeonghyeon Lee * @author Roman Zabaluev * @author Alex Peelman + * @author Oleksandr Ichanskyi * * @since 4.0 * @@ -99,8 +101,12 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl private static final int DEFAULT_CAPACITY = 100_000; + private static final int DEFAULT_IDLE = 100; + private final Lock lock = new ReentrantLock(); + private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE); + private final Map locks = new LinkedHashMap<>(16, 0.75F, true) { @@ -210,6 +216,16 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl this.cacheCapacity = cacheCapacity; } + /** + * Specify a @link Duration} to sleep between obtainLock attempts. + * Defaults to 100 milliseconds. + * @param idleBetweenTries the {@link Duration} to sleep between obtainLock attempts. + * @since 6.2.10 + */ + public void setIdleBetweenTries(Duration idleBetweenTries) { + Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null"); + this.idleBetweenTries = idleBetweenTries; + } /** * Set {@link RedisLockType} mode to work in. @@ -281,7 +297,7 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl public enum RedisLockType { /** - * The lock is acquired by periodically(100ms) checking whether the lock can be acquired. + * The lock is acquired by periodically(idleBetweenTries property) checking whether the lock can be acquired. */ SPIN_LOCK, @@ -744,7 +760,7 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl long now = System.currentTimeMillis(); if (time == -1L) { while (!obtainLock()) { - Thread.sleep(100); //NOSONAR + Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR } return true; } @@ -752,7 +768,7 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl long expire = now + TimeUnit.MILLISECONDS.convert(time, TimeUnit.MILLISECONDS); boolean acquired; while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR - Thread.sleep(100); //NOSONAR + Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR } return acquired; }