Search in sources :

Example 6 with RealConnection

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;
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection)

Example 7 with RealConnection

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());
    }
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) ArrayList(java.util.ArrayList)

Example 8 with RealConnection

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());
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Example 9 with RealConnection

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);
    }
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation)

Example 10 with RealConnection

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());
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Aggregations

RealConnection (okhttp3.internal.connection.RealConnection)8 Test (org.junit.Test)6 HttpCodec (okhttp3.internal.http.HttpCodec)3 IOException (java.io.IOException)2 Interceptor (okhttp3.Interceptor)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 StreamAllocation (okhttp3.internal.connection.StreamAllocation)2 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Route (okhttp3.Route)1 RealInterceptorChain (okhttp3.internal.http.RealInterceptorChain)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1