com.basho.riak.pbc
Class RiakConnectionPool

java.lang.Object
  extended by com.basho.riak.pbc.RiakConnectionPool

public class RiakConnectionPool
extends Object

A bounded or boundless pool of RiakConnections to be reused by RiakClient The pool is designed to be threadsafe, and ideally to be used as a singleton. Due to backwards compatibility requirements it has not been implemented as a singleton. This is really a host connection pool. There is a minor optimization for reusing a connection by client Id, but more work needs doing here.

Author:
russell

Field Summary
static int LIMITLESS
          Constant to use for maxSize when creating an unbounded pool
 
Constructor Summary
RiakConnectionPool(int initialSize, int maxSize, InetAddress host, int port, long connectionWaitTimeoutMillis, int bufferSizeKb, long idleConnectionTTLMillis)
          Crate a new host connection pool.
RiakConnectionPool(int initialSize, Semaphore poolSemaphore, InetAddress host, int port, long connectionWaitTimeoutMillis, int bufferSizeKb, long idleConnectionTTLMillis)
          Crate a new host connection pool.
 
Method Summary
 com.basho.riak.pbc.RiakConnection getConnection(byte[] clientId)
          Get a connection from the pool for the given client Id.
 String getPoolState()
          Convenience method to check the state of the pool.
static Semaphore getSemaphore(int maxSize)
          Create the correct type of semaphore for the maxSize, zero is limitless.
 void releaseConnection(com.basho.riak.pbc.RiakConnection c)
          Returns a connection to the pool (unless the connection is closed (for some reason))
 void shutdown()
          Close this pool and all its connections.
 void start()
          Starts the reaper thread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LIMITLESS

public static final int LIMITLESS
Constant to use for maxSize when creating an unbounded pool

See Also:
Constant Field Values
Constructor Detail

RiakConnectionPool

public RiakConnectionPool(int initialSize,
                          int maxSize,
                          InetAddress host,
                          int port,
                          long connectionWaitTimeoutMillis,
                          int bufferSizeKb,
                          long idleConnectionTTLMillis)
                   throws IOException
Crate a new host connection pool. NOTE: before using you must call start()

Parameters:
initialSize - the number of connections to create at pool creation time
maxSize - the maximum number of connections this pool will have at any one time, 0 means limitless (i.e. creates a new connection if none are available)
host - the host this pool holds connections to
port - the port on host that this pool holds connections to
connectionWaitTimeoutMillis - the connection timeout
bufferSizeKb - the size of the socket/stream read/write buffers (3 buffers, each of this size)
idleConnectionTTLMillis - How long for an idle connection to exist before it is reaped, 0 mean forever
Throws:
IOException - If the initial connection creation throws an IOException

RiakConnectionPool

public RiakConnectionPool(int initialSize,
                          Semaphore poolSemaphore,
                          InetAddress host,
                          int port,
                          long connectionWaitTimeoutMillis,
                          int bufferSizeKb,
                          long idleConnectionTTLMillis)
                   throws IOException
Crate a new host connection pool. NOTE: before using you must call start()

Parameters:
initialSize - the number of connections to create at pool creation time
clusterSemaphore - a Semaphore set with the number of permits for the pool (and maybe cluster (see PoolSemaphore))
host - the host this pool holds connections to
port - the port on host that this pool holds connections to
connectionWaitTimeoutMillis - the connection timeout
bufferSizeKb - the size of the socket/stream read/write buffers (3 buffers, each of this size)
idleConnectionTTLMillis - How long for an idle connection to exist before it is reaped, 0 mean forever
Throws:
IOException - If the initial connection creation throws an IOException
Method Detail

start

public void start()
Starts the reaper thread


getSemaphore

public static Semaphore getSemaphore(int maxSize)
Create the correct type of semaphore for the maxSize, zero is limitless.

Parameters:
maxSize - the number of permits to create a semaphore for
Returns:
a Semaphore with maxSize permits, or a LimitlessSemaphore if maxSize is zero or less.

getConnection

public com.basho.riak.pbc.RiakConnection getConnection(byte[] clientId)
                                                throws IOException
Get a connection from the pool for the given client Id. If there is a connection with that client id in the pool, get that, if there is a connection in the pool but with the wrong Id, call setClientId on it and return it, if there is no available connection and the pool is under limit create a connection, set the id on it and return it.

Parameters:
clientId - the client id of the connection requested
Returns:
a RiakConnection with the clientId set
Throws:
IOException
AcquireConnectionTimeoutException - if unable to acquire a permit to create a *new* connection within the timeout configured. This means that the pool has no available connections and there are no permits available to create new connections. Repeated incidences of this exception probably indicate that you have sized your pool too small.

releaseConnection

public void releaseConnection(com.basho.riak.pbc.RiakConnection c)
Returns a connection to the pool (unless the connection is closed (for some reason))

Parameters:
c - the connection to return.

shutdown

public void shutdown()
Close this pool and all its connections. The pool will throw IllegalStateException for calls to getConnection. While shutting down it will still accept calls to releaseConnection.


getPoolState

public String getPoolState()
Convenience method to check the state of the pool.

Returns:
the name of the current State of the pool


Copyright © 2012. All Rights Reserved.