Skip to content

Commit

Permalink
Merge pull request #207 from scireum/sbi/LockExtension
Browse files Browse the repository at this point in the history
Adds api for passing a lock timeout when trying for a lock
  • Loading branch information
andyHa authored Nov 12, 2018
2 parents 7cdc084 + b35d289 commit e75fbd2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/sirius/biz/locks/BasicLockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeo
}
}

@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout) {
// Ignore lock timeout as its not supported here
return tryLock(lockName, acquireTimeout);
}

/**
* If the lock is already aquired, this returns the initial amount of milliseconds to wait.
*
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/sirius/biz/locks/LockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ public interface LockManager extends Named {
*
* @param lockName the name of the lock to acquire.
* @param acquireTimeout the max time to wait for a lock. Used <tt>null</tt> to immediatelly return if a lock
* cannot
* be obtained.
* cannot be obtained.
* @return <tt>true</tt> if the lock was acquired, <tt>false</tt> otherwise
*/
boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout);

/**
* Tries to acquire the lock with the given name within the given interval.
*
* @param lockName the name of the lock to acquire.
* @param acquireTimeout the max time to wait for a lock. Used <tt>null</tt> to immediatelly return if a lock
* cannot be obtained.
* @param lockTimeout the max duration for which the lock will be kept before auto-releasing it.
* @return <tt>true</tt> if the lock was acquired, <tt>false</tt> otherwise
*/
boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout);

/**
* Determines if the lock is currently being locked.
*
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/sirius/biz/locks/Locks.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeo
return manager.tryLock(lockName, acquireTimeout);
}

/**
* Tries to acquire the given lock in the given timeslot.
* <p>
* A sane value for the timeout might be in the range of 5-50s, highly depending on the algorithm
* being protected by the lock. If the value is <tt>null</tt>, no retries will be performed.
*
* @param lockName the name of the lock to acquire
* @param acquireTimeout the max duration during which retires will be performed
* @param lockTimeout the max duration for which the lock will be kept before auto-releasing it
* @return <tt>true</tt> if the lock was acquired, <tt>false</tt> otherwise
*/
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout) {
return manager.tryLock(lockName, acquireTimeout, lockTimeout);
}

/**
* Boilerplate method to perform the given task while holding the given lock.
* <p>
Expand Down Expand Up @@ -114,7 +129,8 @@ public void gather(MetricsCollector collector) {
LocalDateTime limitForAcquired = LocalDateTime.now().minus(LONG_RUNNING_LOGS_THRESHOLD);

collector.metric("locks_count", "locks-count", "Active Locks", locks.size(), null);
collector.metric("locks_long_running","locks-long-running",
collector.metric("locks_long_running",
"locks-long-running",
"Long locks",
locks.stream().filter(l -> l.getAcquired().isBefore(limitForAcquired)).count(),
null);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/sirius/biz/locks/RedisLockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public String getName() {

@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout) {
return redis.tryLock(lockName, acquireTimeout, Duration.ofMinutes(30));
return tryLock(lockName, acquireTimeout, Duration.ofMinutes(30));
}

@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout) {
return redis.tryLock(lockName, acquireTimeout, lockTimeout);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/sirius/biz/locks/SmartLockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeo
return getDelegate().tryLock(lockName, acquireTimeout);
}

@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout) {
return getDelegate().tryLock(lockName, acquireTimeout, lockTimeout);
}

@Override
public boolean isLocked(@Nonnull String lock) {
return getDelegate().isLocked(lock);
Expand Down

0 comments on commit e75fbd2

Please sign in to comment.