Class ConnectionPoolImpl
- All Implemented Interfaces:
ConnectionPool, AutoCloseable
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
ConcurrentHashMapandSemaphorefor 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 Summary
ConstructorsConstructorDescriptionConnectionPoolImpl(TransportConfig config) Creates a new connection pool with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionlongacquire(SocketAddress endpoint, ConnectionHandler handler) Acquires a connection to the specified endpoint.voidclose()Closes all connections and shuts down the pool.intReturns the total number of active (in-use) connections.intReturns the total number of idle (available) connections.voidReleases a connection back to the pool.
-
Constructor Details
-
ConnectionPoolImpl
Creates a new connection pool with the specified configuration.- Parameters:
config- the transport configuration for creating connections
-
-
Method Details
-
acquire
Description copied from interface:ConnectionPoolAcquires a connection to the specified endpoint.This method attempts to provide a connection in the following order:
- Return an existing idle connection if one is available and healthy
- Create a new connection if below the per-host limit
- Fail with an error if the pool is exhausted
The result is delivered asynchronously via the handler.
- Specified by:
acquirein interfaceConnectionPool- 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
Description copied from interface:ConnectionPoolReleases 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:
releasein interfaceConnectionPool- Parameters:
connection- the connection to release (may be null, which is ignored)
-
getActiveConnectionCount
public int getActiveConnectionCount()Description copied from interface:ConnectionPoolReturns the total number of active (in-use) connections.Active connections are those acquired but not yet released.
- Specified by:
getActiveConnectionCountin interfaceConnectionPool- Returns:
- the active connection count across all endpoints
-
getIdleConnectionCount
public int getIdleConnectionCount()Description copied from interface:ConnectionPoolReturns the total number of idle (available) connections.Idle connections are ready for immediate reuse.
- Specified by:
getIdleConnectionCountin interfaceConnectionPool- Returns:
- the idle connection count across all endpoints
-
close
public void close()Description copied from interface:ConnectionPoolCloses all connections and shuts down the pool.After closing:
- All active and idle connections are closed
ConnectionPool.acquire(SocketAddress, ConnectionHandler)will fail immediately- The pool cannot be restarted
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceConnectionPool
-