Off the top of my head: put the leases returned from the Curator recipe in a DelayQueue and have a thread pull leases out and release them.
-JZ From: Arie Zilberstein [email protected] Reply: [email protected] [email protected] Date: May 13, 2014 at 4:58:37 PM To: [email protected] [email protected] Subject: Distributed TimedSemaphore Hi, Can anyone advise how to implement a distributed timed semaphore? An in-memory implementation of this object, TimedSemaphore, exists in Apache Commons. Here is its description from the Javadoc: ---Start of description A specialized semaphore implementation that provides a number of permits in a given time frame. This class is similar to the java.util.concurrent.Semaphore class provided by the JDK in that it manages a configurable number of permits. Using the acquire() method a permit can be requested by a thread. However, there is an additional timing dimension: there is no release() method for freeing a permit, but all permits are automatically released at the end of a configurable time frame. If a thread calls acquire() and the available permits are already exhausted for this time frame, the thread is blocked. When the time frame ends all permits requested so far are restored, and blocking threads are waked up again, so that they can try to acquire a new permit. This basically means that in the specified time frame only the given number of operations is possible. A use case for this class is to artificially limit the load produced by a process. As an example consider an application that issues database queries on a production system in a background process to gather statistical information. This background processing should not produce so much database load that the functionality and the performance of the production system are impacted. Here a TimedSemaphore could be installed to guarantee that only a given number of database queries are issued per second. ---End of description So, as a first approximation I thought about using the Curator shared semaphore; I would call acquire() to acquire a permit, and I would immediately fire up a thread that releases the permit after a specified amount of time. A major problem with this approach is that, using the Curator semaphore, killed processes / threads automatically release their permit so that other clients can then acquire permits although they shouldn't be allowed to do that (permits should only be released when they are expired). So what is the best way to implement a timed semaphore? Thanks, Arie
