OkHttpClient.Builder默認(rèn)參數(shù)
public Builder() {
// dispatch。負(fù)責(zé)分配處理異步任務(wù)
this.dispatcher = new Dispatcher();
// 支持的協(xié)議鸟妙,默認(rèn)支持http2和http1.1
this.protocols = OkHttpClient.DEFAULT_PROTOCOLS;
// 指定socket鏈接配置昏鹃。默認(rèn):MODERN_TLS和CLEARTEXT
this.connectionSpecs = OkHttpClient.DEFAULT_CONNECTION_SPECS;
// 監(jiān)聽http請(qǐng)求過(guò)程中涉及的各種事件完沪。包括:開始請(qǐng)求、dns解析開始/結(jié)束咐旧、鏈接開始等等。默認(rèn)是的Listener
this.eventListenerFactory = EventListener.factory(EventListener.NONE);
// 設(shè)置代理
this.proxySelector = ProxySelector.getDefault();
// 設(shè)置cookie
this.cookieJar = CookieJar.NO_COOKIES;
// 設(shè)置創(chuàng)建Socket鏈接的Factory
this.socketFactory = SocketFactory.getDefault();
// 驗(yàn)證域名和證書的域名是否匹配
this.hostnameVerifier = OkHostnameVerifier.INSTANCE;
// 設(shè)置信任的證書,不信任之外的證書沛善,默認(rèn)是空的
this.certificatePinner = CertificatePinner.DEFAULT;
// 與身份認(rèn)證有關(guān)。詳見:https://square.github.io/okhttp/3.x/okhttp/okhttp3/Authenticator.html
this.proxyAuthenticator = Authenticator.NONE;
// 與身份認(rèn)證有關(guān)塞祈。詳見:https://square.github.io/okhttp/3.x/okhttp/okhttp3/Authenticator.html
this.authenticator = Authenticator.NONE;
// 連接池-緩存鏈接和清理空閑的連接
this.connectionPool = new ConnectionPool();
// 域名解析
this.dns = Dns.SYSTEM;
// 是否支持ssl重定向
this.followSslRedirects = true;
// 是否支持重定向
this.followRedirects = true;
// 失敗后是否重試
this.retryOnConnectionFailure = true;
// 鏈接超時(shí)時(shí)間
this.connectTimeout = 10000;
// 讀超時(shí)時(shí)間
this.readTimeout = 10000;
// 寫超時(shí)時(shí)間
this.writeTimeout = 10000;
// ping的時(shí)間間隔路呜。如果使用WebSocket請(qǐng)求設(shè)置pingInterval,以保活
this.pingInterval = 0;
}
Dispatcher-任務(wù)調(diào)度器
public final class Dispatcher {
// 最大的異步請(qǐng)求數(shù)
private int maxRequests = 64;
// 單個(gè)host最大的請(qǐng)求數(shù)
private int maxRequestsPerHost = 5;
@Nullable
private Runnable idleCallback;
@Nullable
// 執(zhí)行異步請(qǐng)求的線程池
private ExecutorService executorService;
// 尚未執(zhí)行的請(qǐng)求
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque();
// 正在執(zhí)行的異步請(qǐng)求
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque();
// 正在執(zhí)行的同步請(qǐng)求
private final Deque<RealCall> runningSyncCalls = new ArrayDeque();
// 設(shè)置異步線程池
public Dispatcher(ExecutorService executorService) {
this.executorService = executorService;
}
public Dispatcher() {
}
public synchronized ExecutorService executorService() {
if (this.executorService == null) {
// 默認(rèn)的異步線程池
this.executorService = new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false));
}
return this.executorService;
}
// 異步任務(wù)胀葱,判斷異步任務(wù)數(shù)和單個(gè)host的請(qǐng)求數(shù)
synchronized void enqueue(AsyncCall call) {
if (this.runningAsyncCalls.size() < this.maxRequests && this.runningCallsForHost(call) < this.maxRequestsPerHost) {
this.runningAsyncCalls.add(call);
this.executorService().execute(call);
} else {
this.readyAsyncCalls.add(call);
}
}
}
ConnectionTool-緩存鏈接和清理空閑的連接
public final class ConnectionPool {
// 負(fù)責(zé)清理空閑連接的線程池
private static final Executor executor;
// 最大空閑的連接數(shù)-默認(rèn)值:5
private final int maxIdleConnections;
// 最大的空閑時(shí)間-默認(rèn)值:5分鐘
private final long keepAliveDurationNs;
// 負(fù)責(zé)清理空閑連接的Runnable
private final Runnable cleanupRunnable;
// 所有的連接
private final Deque<RealConnection> connections;
final RouteDatabase routeDatabase;
boolean cleanupRunning;
public ConnectionPool() {
this(5, 5L, TimeUnit.MINUTES);
}
public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
////
}
static {
// 負(fù)責(zé)清理空閑連接的線程池
executor = new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp ConnectionPool", true));
}
}