Class ConnectionPool

java.lang.Object
com.loomcache.client.ConnectionPool
All Implemented Interfaces:
AutoCloseable

public class ConnectionPool extends Object implements 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.

  • 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 port
      minConnections - minimum connections to maintain
      maxConnections - maximum connections allowed
      maxIdleTime - 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 port
      minConnections - minimum connections to maintain
      maxConnections - maximum connections allowed
      maxIdleTime - 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

      public void initialize() throws IOException
      Throws:
      IOException
    • acquire

    • release

      public void release(ConnectionPool.PooledConnection conn)
    • 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

      public ConnectionPool.PoolHealthReport getHealthReport()
      Get a health report snapshot of the connection pool.
      Returns:
      PoolHealthReport with current health metrics
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • 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

      public ConnectionPool.PoolStats getStats()
      Get comprehensive pool statistics.
      Returns:
      PoolStats with totalCreated, totalDestroyed, activeCount, idleCount, healthCheckFailures