Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.ConcurrentModificationException;
import java.util.Date;
Expand Down Expand Up @@ -100,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 @@ -211,6 +216,17 @@ 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.4.0
*/
public void setIdleBetweenTries(Duration idleBetweenTries) {
Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
this.idleBetweenTries = idleBetweenTries;
}

/**
* Set {@link RedisLockType} mode to work in.
* By default, the {@link RedisLockType#SPIN_LOCK} is used - works in all the environment.
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 @@ -743,15 +759,15 @@ protected boolean tryRedisLockInner(long time) throws InterruptedException {
long now = System.currentTimeMillis();
if (time == -1L) {
while (!obtainLock()) {
Thread.sleep(100); //NOSONAR
Thread.sleep(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(idleBetweenTries.toMillis()); //NOSONAR
}
return acquired;
}
Expand Down