Class ConnectionPoolImpl

java.lang.Object
express.mvp.myra.transport.ConnectionPoolImpl
All Implemented Interfaces:
ConnectionPool, AutoCloseable

public final class ConnectionPoolImpl extends Object implements ConnectionPool
Thread-safe connection pool implementation.

This implementation manages per-endpoint connection pools with configurable limits. Each endpoint (host:port) gets its own pool with independent connection tracking and semaphore-based limiting.

Implementation Details

  • Thread safety: Uses ConcurrentHashMap and Semaphore for lock-free concurrent access
  • Per-endpoint isolation: Each endpoint has independent connection tracking and limits
  • Lazy initialization: Pool entries are created on first access
  • Connection reuse: Idle connections are preferred over new connections

Connection Limits

The default maximum connections per host is 16. This can be tuned based on:

  • Expected concurrent requests to each host
  • Server connection limits
  • Available file descriptors
See Also:
  • Constructor Details

    • ConnectionPoolImpl

      public ConnectionPoolImpl(TransportConfig config)
      Creates a new connection pool with the specified configuration.
      Parameters:
      config - the transport configuration for creating connections
  • Method Details

    • acquire

      public long acquire(SocketAddress endpoint, ConnectionHandler handler)
      Description copied from interface: ConnectionPool
      Acquires a connection to the specified endpoint.

      This method attempts to provide a connection in the following order:

      1. Return an existing idle connection if one is available and healthy
      2. Create a new connection if below the per-host limit
      3. Fail with an error if the pool is exhausted

      The result is delivered asynchronously via the handler.

      Specified by:
      acquire in interface ConnectionPool
      Parameters:
      endpoint - the remote endpoint to connect to (e.g., InetSocketAddress)
      handler - the handler to notify when connection is ready or failed
      Returns:
      a token identifying this operation for correlation
    • release

      public void release(Transport connection)
      Description copied from interface: ConnectionPool
      Releases a connection back to the pool.

      The connection is evaluated and either returned to the idle pool (if healthy) or closed (if unhealthy). Released connections become available for future ConnectionPool.acquire(SocketAddress, ConnectionHandler) calls.

      This method is idempotent; releasing an already-released connection has no effect.

      Specified by:
      release in interface ConnectionPool
      Parameters:
      connection - the connection to release (may be null, which is ignored)
    • getActiveConnectionCount

      public int getActiveConnectionCount()
      Description copied from interface: ConnectionPool
      Returns the total number of active (in-use) connections.

      Active connections are those acquired but not yet released.

      Specified by:
      getActiveConnectionCount in interface ConnectionPool
      Returns:
      the active connection count across all endpoints
    • getIdleConnectionCount

      public int getIdleConnectionCount()
      Description copied from interface: ConnectionPool
      Returns the total number of idle (available) connections.

      Idle connections are ready for immediate reuse.

      Specified by:
      getIdleConnectionCount in interface ConnectionPool
      Returns:
      the idle connection count across all endpoints
    • close

      public void close()
      Description copied from interface: ConnectionPool
      Closes all connections and shuts down the pool.

      After closing:

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ConnectionPool