Class ConnectionPool
java.lang.Object
com.loomcache.client.ConnectionPool
- All Implemented Interfaces:
AutoCloseable
Per-node connection pool that manages multiple socket connections.
Includes periodic health checks for idle connections. Connections that fail health checks are evicted and replaced. Configuration: healthCheckIntervalMs (default 30000ms, set to 0 to disable).
Uses interfaces for connection states (Active/Idle/Validating/Closed) and ReentrantLock for synchronization on capacity checks.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordstatic final recordPool health report recordstatic final recordPool statistics record (totalCreated, totalDestroyed, activeCount, idleCount, waitingCount) -
Constructor Summary
ConstructorsConstructorDescriptionConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext) Create a connection pool with default health check interval.ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, boolean strictHandshake) Create a connection pool with default health checks and opt-in strict protocol handshake.ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, long healthCheckIntervalMs) Create a connection pool with custom health check interval.ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, long healthCheckIntervalMs, boolean strictHandshake) Create a connection pool with custom health check interval and opt-in strict protocol handshake. -
Method Summary
Modifier and TypeMethodDescription@Nullable ConnectionPool.PooledConnectionintintvoidclose()voidintevictIdleConnections(long maxIdleMillis) Evict idle connections that have been idle longer than specified time.intGet the number of active (in-use) connections.longGet the number of health check failures since pool creation.Get a health report snapshot of the connection pool.intGet the number of idle (available) connections.intGet the number of currently outstanding connections that have been acquired but not returned.getStats()Get comprehensive pool statistics.longGet the number of suspected connection leaks observed through timed-out acquires.voidvoidvoidsetMaxWaitTime(long millis) Set the maximum wait time for acquiring a connection.intintwarmUp(int targetConnections) Warm up the connection pool by pre-creating connections up to target count.
-
Constructor Details
-
ConnectionPool
public ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext) Create a connection pool with default health check interval.- Parameters:
host- the server hostname (must not be null)port- the server portminConnections- minimum connections to maintainmaxConnections- maximum connections allowedmaxIdleTime- maximum idle time for connections (must not be null)connectTimeout- connection timeout (must not be null)tlsConfig- TLS configuration (may be null)sslContext- SSL context for TLS (may be null)
-
ConnectionPool
public ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, boolean strictHandshake) Create a connection pool with default health checks and opt-in strict protocol handshake. -
ConnectionPool
public ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, long healthCheckIntervalMs) Create a connection pool with custom health check interval.- Parameters:
host- the server hostname (must not be null)port- the server portminConnections- minimum connections to maintainmaxConnections- maximum connections allowedmaxIdleTime- maximum idle time for connections (must not be null)connectTimeout- connection timeout (must not be null)tlsConfig- TLS configuration (may be null)sslContext- SSL context for TLS (may be null)healthCheckIntervalMs- health check interval in milliseconds- Throws:
NullPointerException- if required parameters are null
-
ConnectionPool
public ConnectionPool(String host, int port, int minConnections, int maxConnections, Duration maxIdleTime, Duration connectTimeout, @Nullable TlsConfig tlsConfig, @Nullable SSLContext sslContext, long healthCheckIntervalMs, boolean strictHandshake) Create a connection pool with custom health check interval and opt-in strict protocol handshake.- Parameters:
strictHandshake- when true, run PROTOCOL_HELLO on every newly-created socket
-
-
Method Details
-
initialize
- Throws:
IOException
-
acquire
public @Nullable ConnectionPool.PooledConnection acquire(Duration timeout) throws IOException, InterruptedException - Throws:
IOExceptionInterruptedException
-
release
-
evictIdle
public void evictIdle() -
activeCount
public int activeCount() -
availableCount
public int availableCount() -
totalCount
public int totalCount() -
warmUp
public int warmUp(int targetConnections) Warm up the connection pool by pre-creating connections up to target count.- Parameters:
targetConnections- target number of connections to warm up- Returns:
- number of connections successfully created
-
getIdleConnectionCount
public int getIdleConnectionCount()Get the number of idle (available) connections.- Returns:
- count of idle connections
-
getActiveConnectionCount
public int getActiveConnectionCount()Get the number of active (in-use) connections.- Returns:
- count of active connections
-
getOutstandingConnectionCount
public int getOutstandingConnectionCount()Get the number of currently outstanding connections that have been acquired but not returned.- Returns:
- count of outstanding connections
-
setMaxWaitTime
public void setMaxWaitTime(long millis) Set the maximum wait time for acquiring a connection.- Parameters:
millis- maximum wait time in milliseconds
-
evictIdleConnections
public int evictIdleConnections(long maxIdleMillis) Evict idle connections that have been idle longer than specified time.- Parameters:
maxIdleMillis- maximum idle time in milliseconds- Returns:
- number of connections evicted
-
getHealthReport
Get a health report snapshot of the connection pool.- Returns:
- PoolHealthReport with current health metrics
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getHealthCheckFailures
public long getHealthCheckFailures()Get the number of health check failures since pool creation. -
getSuspectedConnectionLeakCount
public long getSuspectedConnectionLeakCount()Get the number of suspected connection leaks observed through timed-out acquires.- Returns:
- suspected connection leak counter
-
getStats
Get comprehensive pool statistics.- Returns:
- PoolStats with totalCreated, totalDestroyed, activeCount, idleCount, healthCheckFailures
-