Connection
首先看一下okhttp如何定義連接配接,Connection接口可略知一二
public interface Connection {
/** Returns the route used by this connection. */
Route route();
/**
* Returns the socket that this connection is using. Returns an {@linkplain
* javax.net.ssl.SSLSocket SSL socket} if this connection is HTTPS. If this is an HTTP/2
* connection the socket may be shared by multiple concurrent calls.
*/
Socket socket();
/**
* Returns the TLS handshake used to establish this connection, or null if the connection is not
* HTTPS.
*/
@Nullable Handshake handshake();
/**
* Returns the protocol negotiated by this connection, or {@link Protocol#HTTP_1_1} if no protocol
* has been negotiated. This method returns {@link Protocol#HTTP_1_1} even if the remote peer is
* using {@link Protocol#HTTP_1_0}.
*/
Protocol protocol();
}
在okhttp中實作了Connection接口的類是RealConnection, 裡面涉及到了如何建立一次連接配接。
ConnectionPool
該類用于管理所有網絡請求的連接配接,對于滿足特定條件的連接配接進行複用。
- 構造函數:
// maxIdleConnections: 單個url最大的連接配接數
// keepAliveDuration: 單個連接配接的有效期 timeUnit: 有效期的時間機關
public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
this.maxIdleConnections = maxIdleConnections;
this.keepAliveDurationNs = timeUnit.toNanos(keepAliveDuration);
// Put a floor on the keep alive duration, otherwise cleanup will spin loop.
if (keepAliveDuration <= 0) {
throw new IllegalArgumentException("keepAliveDuration <= 0: " + keepAliveDuration);
}
}
// okhttp 使用如下預設值
public ConnectionPool() {
this(5, 5, TimeUnit.MINUTES);
}
ConnectionPool 的構造隻會在OkhttpClient的Builder裡面建立一次,其它所有類持有的ConnectionPool都是通過參數傳遞過去的。
重要成員:
// 用于存放連接配接的雙向隊列
private final Deque<RealConnection> connections = new ArrayDeque<>();
擷取可重用的連接配接
/**
* Returns a recycled connection to {@code address}, or null if no such connection exists. The
* route is null if the address has not yet been routed.
*/
@Nullable RealConnection get(Address address, StreamAllocation streamAllocation, Route route) {
assert (Thread.holdsLock(this));
for (RealConnection connection : connections) {
if (connection.isEligible(address, route)) {
streamAllocation.acquire(connection);
return connection;
}
}
return null;
}
夢想不是浮躁,而是沉澱和積累