public final class ConnectionPool
extends java.lang.Object
Address
may share a Connection
. This class implements the policy
of which connections to keep open for future use.Modifier and Type | Field and Description |
---|---|
private java.lang.Runnable |
cleanupRunnable |
(package private) boolean |
cleanupRunning |
private java.util.Deque<RealConnection> |
connections |
private static java.util.concurrent.Executor |
executor
Background threads are used to cleanup expired connections.
|
private long |
keepAliveDurationNs |
private int |
maxIdleConnections
The maximum number of idle connections for each address.
|
(package private) RouteDatabase |
routeDatabase |
Constructor and Description |
---|
ConnectionPool()
Create a new connection pool with tuning parameters appropriate for a single-user application.
|
ConnectionPool(int maxIdleConnections,
long keepAliveDuration,
java.util.concurrent.TimeUnit timeUnit) |
Modifier and Type | Method and Description |
---|---|
(package private) long |
cleanup(long now)
Performs maintenance on this pool, evicting the connection that has been idle the longest if
either it has exceeded the keep alive limit or the idle connections limit.
|
(package private) boolean |
connectionBecameIdle(RealConnection connection)
Notify this pool that
connection has become idle. |
int |
connectionCount()
Returns total number of connections in the pool.
|
(package private) java.net.Socket |
deduplicate(Address address,
StreamAllocation streamAllocation)
Replaces the connection held by
streamAllocation with a shared connection if possible. |
void |
evictAll()
Close and remove all idle connections in the pool.
|
(package private) RealConnection |
get(Address address,
StreamAllocation streamAllocation,
Route route)
Returns a recycled connection to
address , or null if no such connection exists. |
int |
idleConnectionCount()
Returns the number of idle connections in the pool.
|
private int |
pruneAndGetAllocationCount(RealConnection connection,
long now)
Prunes any leaked allocations and then returns the number of remaining live allocations on
connection . |
(package private) void |
put(RealConnection connection) |
private static final java.util.concurrent.Executor executor
private final int maxIdleConnections
private final long keepAliveDurationNs
private final java.lang.Runnable cleanupRunnable
private final java.util.Deque<RealConnection> connections
final RouteDatabase routeDatabase
boolean cleanupRunning
public ConnectionPool()
public ConnectionPool(int maxIdleConnections, long keepAliveDuration, java.util.concurrent.TimeUnit timeUnit)
public int idleConnectionCount()
public int connectionCount()
idleConnectionCount()
to count connections not currently
in use.@Nullable RealConnection get(Address address, StreamAllocation streamAllocation, Route route)
address
, or null if no such connection exists. The
route is null if the address has not yet been routed.@Nullable java.net.Socket deduplicate(Address address, StreamAllocation streamAllocation)
streamAllocation
with a shared connection if possible.
This recovers when multiple multiplexed connections are created concurrently.void put(RealConnection connection)
boolean connectionBecameIdle(RealConnection connection)
connection
has become idle. Returns true if the connection has
been removed from the pool and should be closed.public void evictAll()
long cleanup(long now)
Returns the duration in nanos to sleep until the next scheduled call to this method. Returns -1 if no further cleanups are required.
private int pruneAndGetAllocationCount(RealConnection connection, long now)
connection
. Allocations are leaked if the connection is tracking them but the
application code has abandoned them. Leak detection is imprecise and relies on garbage
collection.