[CURATOR-459] Support for asynchronous locks in Curator Async
- Dominant language
- Java
- Stars
- 3.2k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
It would be great to have a support for asynchronous locks. Here's an interface suggestion
/**
* A non-reentrant Lock with the ability to react when the lock is acquired async, so that the thread which
* acquires the lock is not blocked.
*/
public interface CompletableLock {/**
* Acquire the lock non-blocking. The resulting {@link CompletableFuture} can then be used to block
* until the lock is actually acquired. The {@link CompletableFuture#cancel(boolean)} can be used
* to abort the acquisition of the lock. As soon as the lock is actually acquired, this method has no
* effect. The Lock itself is then released with {@link Lease#unlock()}.
*
* Several calls results in several independent locking requests. So it is equivalent to
* call this method multiple times or get a new lock and then call the method once.
*
* @return an instance of {@link CompletableFuture} which represents the actual lock.
*/
CompletableFuture lock();default void withLock(Runnable body) {
lock().thenAccept(lock -> {
try {
body.run();
} finally {
lock.unlock();
}
});
}/**
* Same as {@link #lock()} but if the lock can't be acquired within the given amount of time,
* the {@link CompletableFuture} throws a {@link java.util.concurrent.ExecutionException} with a nested
* {@link java.util.concurrent.TimeoutException} when it is accessed the next time.
*
* @param time the maximum waiting time for acquiring the lock.
* @param unit the {@link TimeUnit} for the given time.
* @return an instance of {@link CompletableFuture} which represents the actual lock.
*/
CompletableFuture lock(int time, TimeUnit unit);/**
* Gets the lock only if possible. If lock is currently acquired, this returns immediately with an undefined option.
*/
Optional tryLock();/**
* Gets the lock only if possible. If lock is currently acquired, this returns an undefined option after the
* given timeout.
*/
Lease tryLock(int time, TimeUnit unit);/**
* For unlocking a successfully acquired lock.
*/
interface Lease {/**
* Releases the lock.
*/
void unlock();
}
}
---
Originally reported by peter.rietzler@smarter-ecommerce.com, imported from: Support for asynchronous locks in Curator Async
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.