com.basho.riak.client.raw.pbc
Class PoolSemaphore

java.lang.Object
  extended by java.util.concurrent.Semaphore
      extended by com.basho.riak.client.raw.pbc.PoolSemaphore
All Implemented Interfaces:
Serializable

public class PoolSemaphore
extends Semaphore

Wraps a semaphore for the cluster and a semaphore for the pool. Used by RiakConnectionPool

Author:
russell
See Also:
Serialized Form

Constructor Summary
PoolSemaphore(Semaphore clusterSemaphore, int poolPermits)
          Creates a pool Semaphore with permits permits.
 
Method Summary
 void release()
          Both semaphores are released
 boolean tryAcquire(int permits)
          Tries to acquire from the cluster semaphore, and only if that succeeds does it try the pool semaphore.
 boolean tryAcquire(long timeout, TimeUnit unit)
          Tries to acquire on the cluster semaphore, and only if that succeeds does it try the pool semaphore.
 
Methods inherited from class java.util.concurrent.Semaphore
acquire, acquire, acquireUninterruptibly, acquireUninterruptibly, availablePermits, drainPermits, getQueuedThreads, getQueueLength, hasQueuedThreads, isFair, reducePermits, release, toString, tryAcquire, tryAcquire
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PoolSemaphore

public PoolSemaphore(Semaphore clusterSemaphore,
                     int poolPermits)
Creates a pool Semaphore with permits permits. Calls to tryAcquire(long, TimeUnit), release() and tryAcquire(int) call both cluster and pool Semaphore

Parameters:
cluster - a Semaphore configured for the total cluster maximum number of connections
permits - maximum connections for the pool
Method Detail

release

public void release()
Both semaphores are released

Overrides:
release in class Semaphore

tryAcquire

public boolean tryAcquire(long timeout,
                          TimeUnit unit)
                   throws InterruptedException
Tries to acquire on the cluster semaphore, and only if that succeeds does it try the pool semaphore.

Warning: this means potentially double the timeout

TODO configure option for timeout to be shared (i.e. pool semaphore gets remaining time after cluster acquire)

Overrides:
tryAcquire in class Semaphore
Throws:
InterruptedException
See Also:
Semaphore.tryAcquire(long, java.util.concurrent.TimeUnit)

tryAcquire

public boolean tryAcquire(int permits)
Tries to acquire from the cluster semaphore, and only if that succeeds does it try the pool semaphore.

Overrides:
tryAcquire in class Semaphore
See Also:
Semaphore.tryAcquire(int)


Copyright © 2012. All Rights Reserved.