ZkAsyncSemaphore is a distributed semaphore with asynchronous execution.
Grabbing a permit constitutes a vote on the number of permits the semaphore
can permit and returns a Future[Permit]. If consensus on the number of permits
is lost, an exception is raised when acquiring a permit (so expect it).
Care must be taken to handle zookeeper client session expiry. A ZkAsyncSemaphore cannot
be used after the zookeeper session has expired. Likewise, any permits acquired
via the session must be considered invalid. Additionally, it is the client's responsibility
to determine if a permit is still valid in the case that the zookeeper client becomes
disconnected.
Attempts to clone AsyncSemaphore
Ex.
implicitval timer = new JavaTimer(true)
val connector = NativeConnector("localhost:2181", 5.seconds, 10.minutes)
val zk = ZkClient(connector).withRetryPolicy(RetryPolicy.Basic(3))
val path = "/testing/twitter/service/charm/shards"val sem = new ZkAsyncSemaphore(zk, path, 4)
sem.acquire flatMap { permit =>
Future { ... } ensure { permit.release }
} // handle { ... }
ZkAsyncSemaphore is a distributed semaphore with asynchronous execution. Grabbing a permit constitutes a vote on the number of permits the semaphore can permit and returns a Future[Permit]. If consensus on the number of permits is lost, an exception is raised when acquiring a permit (so expect it).
Care must be taken to handle zookeeper client session expiry. A ZkAsyncSemaphore cannot be used after the zookeeper session has expired. Likewise, any permits acquired via the session must be considered invalid. Additionally, it is the client's responsibility to determine if a permit is still valid in the case that the zookeeper client becomes disconnected.
Attempts to clone AsyncSemaphore
Ex.