Closed
Description
Hello,
We are using RedisLockRegistry
in our project and noticed that, there is a Redis query every 100 milliseconds to check for the lock:
@Override
protected boolean tryRedisLockInner(long time) throws InterruptedException {
long now = System.currentTimeMillis();
if (time == -1L) {
while (!obtainLock()) {
Thread.sleep(100); //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
}
return acquired;
}
}
The issue is that we can't configure the sleep duration between these requests (Thread.sleep(100); //NOSONAR
), resulting in a large number of Redis requests.
In JdbcLockRegistry
, there is a similar property, idleBetweenTries, that allows this configuration.
My suggestion is to add a similar configuration option for RedisLockRegistry
. Specifically:
- Add a field:
private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE);
- Provide a setter method:
public void setIdleBetweenTries(Duration idleBetweenTries) {
Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
this.idleBetweenTries = idleBetweenTries;
}
- Use the
idleBetweenTries
in thetryRedisLockInner()
method:
@Override
protected boolean tryRedisLockInner(long time) throws InterruptedException {
long now = System.currentTimeMillis();
if (time == -1L) {
while (!obtainLock()) {
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(idleBetweenTries.toMillis()); //NOSONAR
}
return acquired;
}
}