Class ConnectionPool

java.lang.Object
com.squareup.okhttp.ConnectionPool

public class ConnectionPool
extends Object
Manages reuse of HTTP and SPDY connections for reduced network latency. HTTP requests that share the same Address may share a Connection. This class implements the policy of which connections to keep open for future use.

The system-wide default uses system properties for tuning parameters:

  • http.keepAlive true if HTTP and SPDY connections should be pooled at all. Default is true.
  • http.maxConnections maximum number of idle connections to each to keep in the pool. Default is 5.
  • http.keepAliveDuration Time in milliseconds to keep the connection alive in the pool before closing it. Default is 5 minutes. This property isn't used by HttpURLConnection.

The default instance doesn't adjust its configuration as system properties are changed. This assumes that the applications that set these parameters do so before making HTTP connections, and that this class is initialized lazily.

  • Constructor Details

    • ConnectionPool

      public ConnectionPool​(int maxIdleConnections, long keepAliveDurationMs)
  • Method Details

    • getDefault

      public static ConnectionPool getDefault()
    • getConnectionCount

      public int getConnectionCount()
      Returns total number of connections in the pool.
    • getSpdyConnectionCount

      public int getSpdyConnectionCount()
      Returns total number of spdy connections in the pool.
    • getHttpConnectionCount

      public int getHttpConnectionCount()
      Returns total number of http connections in the pool.
    • get

      public Connection get​(Address address)
      Returns a recycled connection to address, or null if no such connection exists.
    • recycle

      public void recycle​(Connection connection)
      Gives connection to the pool. The pool may store the connection, or close it, as its policy describes.

      It is an error to use connection after calling this method.

    • maybeShare

      public void maybeShare​(Connection connection)
      Shares the SPDY connection with the pool. Callers to this method may continue to use connection.
    • evictAll

      public void evictAll()
      Close and remove all connections in the pool.