Search in sources :

Example 1 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class ConnectionPoolTest method inUseConnectionsNotEvicted.

@Test
public void inUseConnectionsNotEvicted() 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, 50L);
    synchronized (pool) {
        StreamAllocation streamAllocation = new StreamAllocation(pool, addressA, null);
        streamAllocation.acquire(c1);
    }
    // Running at time 50, the pool returns that nothing can be evicted until time 150.
    assertEquals(100L, pool.cleanup(50L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    // Running at time 60, the pool returns that nothing can be evicted until time 160.
    assertEquals(100L, pool.cleanup(60L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    // Running at time 160, the pool returns that nothing can be evicted until time 260.
    assertEquals(100L, pool.cleanup(160L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation) RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Example 2 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class ConnectionPoolTest method connectionsEvictedWhenIdleLongEnough.

@Test
public void connectionsEvictedWhenIdleLongEnough() 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, 50L);
    // Running at time 50, the pool returns that nothing can be evicted until time 150.
    assertEquals(100L, pool.cleanup(50L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    // Running at time 60, the pool returns that nothing can be evicted until time 150.
    assertEquals(90L, pool.cleanup(60L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    // Running at time 149, the pool returns that nothing can be evicted until time 150.
    assertEquals(1L, pool.cleanup(149L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    // Running at time 150, the pool evicts.
    assertEquals(0, pool.cleanup(150L));
    assertEquals(0, pool.connectionCount());
    assertTrue(c1.socket().isClosed());
    // Running again, the pool reports that no further runs are necessary.
    assertEquals(-1, pool.cleanup(150L));
    assertEquals(0, pool.connectionCount());
    assertTrue(c1.socket().isClosed());
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Example 3 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class ConnectionPoolTest method leakedAllocation.

@Test
public void leakedAllocation() 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, 0L);
    allocateAndLeakAllocation(pool, c1);
    awaitGarbageCollection();
    assertEquals(0L, pool.cleanup(100L));
    assertEquals(Collections.emptyList(), c1.allocations);
    // Can't allocate once a leak has been detected.
    assertTrue(c1.noNewStreams);
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Example 4 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class StreamAllocation method findConnection.

/**
   * Returns a connection to host a new stream. This prefers the existing connection if it exists,
   * then the pool, finally building a new connection.
   */
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled) throws IOException {
    Route selectedRoute;
    synchronized (connectionPool) {
        if (released)
            throw new IllegalStateException("released");
        if (codec != null)
            throw new IllegalStateException("codec != null");
        if (canceled)
            throw new IOException("Canceled");
        // Attempt to use an already-allocated connection.
        RealConnection allocatedConnection = this.connection;
        if (allocatedConnection != null && !allocatedConnection.noNewStreams) {
            return allocatedConnection;
        }
        // Attempt to get a connection from the pool.
        Internal.instance.get(connectionPool, address, this, null);
        if (connection != null) {
            return connection;
        }
        selectedRoute = route;
    }
    // If we need a route, make one. This is a blocking operation.
    if (selectedRoute == null) {
        selectedRoute = routeSelector.next();
    }
    RealConnection result;
    synchronized (connectionPool) {
        if (canceled)
            throw new IOException("Canceled");
        // Now that we have an IP address, make another attempt at getting a connection from the pool.
        // This could match due to connection coalescing.
        Internal.instance.get(connectionPool, address, this, selectedRoute);
        if (connection != null)
            return connection;
        // Create a connection and assign it to this allocation immediately. This makes it possible
        // for an asynchronous cancel() to interrupt the handshake we're about to do.
        route = selectedRoute;
        refusedStreamCount = 0;
        result = new RealConnection(connectionPool, selectedRoute);
        acquire(result);
    }
    // Do TCP + TLS handshakes. This is a blocking operation.
    result.connect(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled);
    routeDatabase().connected(result.route());
    Socket socket = null;
    synchronized (connectionPool) {
        // Pool the connection.
        Internal.instance.put(connectionPool, result);
        // release this connection and acquire that one.
        if (result.isMultiplexed()) {
            socket = Internal.instance.deduplicate(connectionPool, address, this);
            result = connection;
        }
    }
    closeQuietly(socket);
    return result;
}
Also used : IOException(java.io.IOException) Route(okhttp3.Route) Socket(java.net.Socket)

Example 5 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class ConnectInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    StreamAllocation streamAllocation = realChain.streamAllocation();
    // We need the network to satisfy this request. Possibly for validating a conditional GET.
    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec) RealInterceptorChain(okhttp3.internal.http.RealInterceptorChain) Request(okhttp3.Request)

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