Skip to content

Add idleBetweenTries property to RedisLockRegistry #9540

Closed
@Ichanskiy

Description

@Ichanskiy

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:

  1. Add a field:
    private Duration idleBetweenTries = Duration.ofMillis(DEFAULT_IDLE);
  2. Provide a setter method:
public void setIdleBetweenTries(Duration idleBetweenTries) {
    Assert.notNull(idleBetweenTries, "'idleBetweenTries' must not be null");
    this.idleBetweenTries = idleBetweenTries;
}
  1. Use the idleBetweenTries in the tryRedisLockInner() 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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions