Skip to content

Commit

Permalink
GH-9540: Add RedisLockRegistry.idleBetweenTries property
Browse files Browse the repository at this point in the history
Fixes: #9540
Issue link: #9540

**Auto-cherry-pick to `6.3.x`**
  • Loading branch information
Ichanskiy authored Oct 9, 2024
1 parent 7a348fa commit ca1f66e
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +88,7 @@
* @author Myeonghyeon Lee
* @author Roman Zabaluev
* @author Alex Peelman
* @author Oleksandr Ichanskyi
*
* @since 4.0
*
Expand All @@ -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<String, RedisLock> locks =
new LinkedHashMap<>(16, 0.75F, true) {

Expand Down Expand Up @@ -210,6 +216,16 @@ public void setCacheCapacity(int cacheCapacity) {
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.
Expand Down Expand Up @@ -281,7 +297,7 @@ public void destroy() {
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,

Expand Down Expand Up @@ -744,15 +760,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
long now = System.currentTimeMillis();
if (time == -1L) {
while (!obtainLock()) {
Thread.sleep(100); //NOSONAR
Thread.sleep(RedisLockRegistry.this.idleBetweenTries.toMillis()); //NOSONAR
}
return true;
}
else {
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;
}
Expand Down

0 comments on commit ca1f66e

Please sign in to comment.