use of okhttp3.ConnectionPool 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);
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class ConnectionReuseTest method connectionsReusedWithRedirectEvenIfPoolIsSizeZero.
@Test
public void connectionsReusedWithRedirectEvenIfPoolIsSizeZero() throws Exception {
client = client.newBuilder().connectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS)).build();
server.enqueue(new MockResponse().setResponseCode(301).addHeader("Location: /b").setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertEquals("b", response.body().string());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class RealConnection method connect.
public void connect(int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled) {
if (protocol != null)
throw new IllegalStateException("already connected");
RouteException routeException = null;
List<ConnectionSpec> connectionSpecs = route.address().connectionSpecs();
ConnectionSpecSelector connectionSpecSelector = new ConnectionSpecSelector(connectionSpecs);
if (route.address().sslSocketFactory() == null) {
if (!connectionSpecs.contains(ConnectionSpec.CLEARTEXT)) {
throw new RouteException(new UnknownServiceException("CLEARTEXT communication not enabled for client"));
}
String host = route.address().url().host();
if (!Platform.get().isCleartextTrafficPermitted(host)) {
throw new RouteException(new UnknownServiceException("CLEARTEXT communication to " + host + " not permitted by network security policy"));
}
}
while (true) {
try {
if (route.requiresTunnel()) {
connectTunnel(connectTimeout, readTimeout, writeTimeout);
} else {
connectSocket(connectTimeout, readTimeout);
}
establishProtocol(connectionSpecSelector);
break;
} catch (IOException e) {
closeQuietly(socket);
closeQuietly(rawSocket);
socket = null;
rawSocket = null;
source = null;
sink = null;
handshake = null;
protocol = null;
http2Connection = null;
if (routeException == null) {
routeException = new RouteException(e);
} else {
routeException.addConnectException(e);
}
if (!connectionRetryEnabled || !connectionSpecSelector.connectionFailed(e)) {
throw routeException;
}
}
}
if (http2Connection != null) {
synchronized (connectionPool) {
allocationLimit = http2Connection.maxConcurrentStreams();
}
}
}
use of okhttp3.ConnectionPool 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;
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class CallTest method asyncLeakedResponseBodyLogsStackTrace.
@Test
public void asyncLeakedResponseBodyLogsStackTrace() throws Exception {
server.enqueue(new MockResponse().setBody("This gets leaked."));
client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
Request request = new Request.Builder().url(server.url("/")).build();
Level original = logger.getLevel();
logger.setLevel(Level.FINE);
logHandler.setFormatter(new SimpleFormatter());
try {
final CountDownLatch latch = new CountDownLatch(1);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
fail();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Ignore the response so it gets leaked then GC'd.
latch.countDown();
}
});
latch.await();
// There's some flakiness when triggering a GC for objects in a separate thread. Adding a
// small delay appears to ensure the objects will get GC'd.
Thread.sleep(200);
awaitGarbageCollection();
String message = logHandler.take();
assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
assertTrue(message.contains("okhttp3.RealCall.enqueue("));
assertTrue(message.contains("okhttp3.CallTest.asyncLeakedResponseBodyLogsStackTrace("));
} finally {
logger.setLevel(original);
}
}
Aggregations