public class RiakConnectionPool extends Object
RiakConnection
s 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.Modifier and Type | Field and Description |
---|---|
static int |
LIMITLESS
Constant to use for
maxSize when creating an unbounded pool |
Constructor and Description |
---|
RiakConnectionPool(int initialSize,
int maxSize,
InetAddress host,
int port,
long connectionWaitTimeoutMillis,
int bufferSizeKb,
long idleConnectionTTLMillis,
int requestTimeoutMillis)
Crate a new host connection pool.
|
RiakConnectionPool(int initialSize,
Semaphore poolSemaphore,
InetAddress host,
int port,
long connectionWaitTimeoutMillis,
int bufferSizeKb,
long idleConnectionTTLMillis,
int requestTimeoutMillis)
Crate a new host connection pool.
|
Modifier and Type | Method and Description |
---|---|
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
|
public static final int LIMITLESS
maxSize
when creating an unbounded poolpublic RiakConnectionPool(int initialSize, int maxSize, InetAddress host, int port, long connectionWaitTimeoutMillis, int bufferSizeKb, long idleConnectionTTLMillis, int requestTimeoutMillis) throws IOException
initialSize
- the number of connections to create at pool creation timemaxSize
- 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 toport
- the port on host that this pool holds connections toconnectionWaitTimeoutMillis
- the connection timeoutbufferSizeKb
- 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 foreverrequestTimeoutMillis
- The SO_TIMEOUT flag on the socket; read/write timeout
0 means foreverIOException
- If the initial connection creation throws an IOExceptionpublic RiakConnectionPool(int initialSize, Semaphore poolSemaphore, InetAddress host, int port, long connectionWaitTimeoutMillis, int bufferSizeKb, long idleConnectionTTLMillis, int requestTimeoutMillis) throws IOException
initialSize
- the number of connections to create at pool creation timeclusterSemaphore
- a Semaphore
set with the number of permits for the
pool (and maybe cluster (see PoolSemaphore
))host
- the host this pool holds connections toport
- the port on host that this pool holds connections toconnectionWaitTimeoutMillis
- the connection timeoutbufferSizeKb
- 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 foreverrequestTimeoutMillis
- The SO_TIMEOUT flag on the socket; read/write timeout
0 means foreverIOException
- If the initial connection creation throws an IOExceptionpublic void start()
public static Semaphore getSemaphore(int maxSize)
maxSize, zero is limitless.
maxSize
- the number of permits to create a semaphore forSemaphore
with maxSize
permits, or a
LimitlessSemaphore
if maxSize
is zero or less.public com.basho.riak.pbc.RiakConnection getConnection(byte[] clientId) throws IOException
clientId
- the client id of the connection requestedIOException
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.public void releaseConnection(com.basho.riak.pbc.RiakConnection c)
c
- the connection to return.public void shutdown()
IllegalStateException
for calls to getConnection. While shutting
down it will still accept calls to releaseConnection.public String getPoolState()
State
of the poolCopyright © 2013. All Rights Reserved.