use of okhttp3.internal.connection.RealConnection in project okhttp by square.
the class ConnectionPool method cleanup.
/**
* 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.
*
* <p>Returns the duration in nanos to sleep until the next scheduled call to this method. Returns
* -1 if no further cleanups are required.
*/
long cleanup(long now) {
int inUseConnectionCount = 0;
int idleConnectionCount = 0;
RealConnection longestIdleConnection = null;
long longestIdleDurationNs = Long.MIN_VALUE;
// Find either a connection to evict, or the time that the next eviction is due.
synchronized (this) {
for (Iterator<RealConnection> i = connections.iterator(); i.hasNext(); ) {
RealConnection connection = i.next();
// If the connection is in use, keep searching.
if (pruneAndGetAllocationCount(connection, now) > 0) {
inUseConnectionCount++;
continue;
}
idleConnectionCount++;
// If the connection is ready to be evicted, we're done.
long idleDurationNs = now - connection.idleAtNanos;
if (idleDurationNs > longestIdleDurationNs) {
longestIdleDurationNs = idleDurationNs;
longestIdleConnection = connection;
}
}
if (longestIdleDurationNs >= this.keepAliveDurationNs || idleConnectionCount > this.maxIdleConnections) {
// We've found a connection to evict. Remove it from the list, then close it below (outside
// of the synchronized block).
connections.remove(longestIdleConnection);
} else if (idleConnectionCount > 0) {
// A connection will be ready to evict soon.
return keepAliveDurationNs - longestIdleDurationNs;
} else if (inUseConnectionCount > 0) {
// All connections are in use. It'll be at least the keep alive duration 'til we run again.
return keepAliveDurationNs;
} else {
// No connections, idle or in use.
cleanupRunning = false;
return -1;
}
}
closeQuietly(longestIdleConnection.socket());
// Cleanup again immediately.
return 0;
}
use of okhttp3.internal.connection.RealConnection in project okhttp by square.
the class ConnectionPool method evictAll.
/** Close and remove all idle connections in the pool. */
public void evictAll() {
List<RealConnection> evictedConnections = new ArrayList<>();
synchronized (this) {
for (Iterator<RealConnection> i = connections.iterator(); i.hasNext(); ) {
RealConnection connection = i.next();
if (connection.allocations.isEmpty()) {
connection.noNewStreams = true;
evictedConnections.add(connection);
i.remove();
}
}
}
for (RealConnection connection : evictedConnections) {
closeQuietly(connection.socket());
}
}
use of okhttp3.internal.connection.RealConnection in project okhttp by square.
the class ConnectionPoolTest method oldestConnectionsEvictedIfIdleLimitExceeded.
@Test
public void oldestConnectionsEvictedIfIdleLimitExceeded() throws Exception {
ConnectionPool pool = new ConnectionPool(2, 100L, TimeUnit.NANOSECONDS);
// Prevent the cleanup runnable from being started.
pool.cleanupRunning = true;
RealConnection c1 = newConnection(pool, routeA1, 50L);
RealConnection c2 = newConnection(pool, routeB1, 75L);
// With 2 connections, there's no need to evict until the connections time out.
assertEquals(50L, pool.cleanup(100L));
assertEquals(2, pool.connectionCount());
assertFalse(c1.socket().isClosed());
assertFalse(c2.socket().isClosed());
// Add a third connection
RealConnection c3 = newConnection(pool, routeC1, 75L);
// The third connection bounces the first.
assertEquals(0L, pool.cleanup(100L));
assertEquals(2, pool.connectionCount());
assertTrue(c1.socket().isClosed());
assertFalse(c2.socket().isClosed());
assertFalse(c3.socket().isClosed());
}
use of okhttp3.internal.connection.RealConnection in project okhttp by square.
the class ConnectionPoolTest method allocateAndLeakAllocation.
/** Use a helper method so there's no hidden reference remaining on the stack. */
private void allocateAndLeakAllocation(ConnectionPool pool, RealConnection connection) {
synchronized (pool) {
StreamAllocation leak = new StreamAllocation(pool, connection.route().address(), null);
leak.acquire(connection);
}
}
use of okhttp3.internal.connection.RealConnection in project okhttp by square.
the class ConnectionPoolTest method cleanupPrioritizesEarliestEviction.
@Test
public void cleanupPrioritizesEarliestEviction() throws Exception {
ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
// Prevent the cleanup runnable from being started.
pool.cleanupRunning = true;
RealConnection c1 = newConnection(pool, routeA1, 75L);
RealConnection c2 = newConnection(pool, routeB1, 50L);
// Running at time 75, the pool returns that nothing can be evicted until time 150.
assertEquals(75L, pool.cleanup(75L));
assertEquals(2, pool.connectionCount());
// Running at time 149, the pool returns that nothing can be evicted until time 150.
assertEquals(1L, pool.cleanup(149L));
assertEquals(2, pool.connectionCount());
// Running at time 150, the pool evicts c2.
assertEquals(0L, pool.cleanup(150L));
assertEquals(1, pool.connectionCount());
assertFalse(c1.socket().isClosed());
assertTrue(c2.socket().isClosed());
// Running at time 150, the pool returns that nothing can be evicted until time 175.
assertEquals(25L, pool.cleanup(150L));
assertEquals(1, pool.connectionCount());
// Running at time 175, the pool evicts c1.
assertEquals(0L, pool.cleanup(175L));
assertEquals(0, pool.connectionCount());
assertTrue(c1.socket().isClosed());
assertTrue(c2.socket().isClosed());
}
Aggregations