对于OKHttp的源码解析,我一共分了三章
前一章我们了解了OkHttp的基本使用以及部分类的方法,接下来我们分析一下OkHttp的请求流程
流程分析
同步请求
同步请求的方法是
java
client.newCall(request).execute();
RealCall.kt
kotlin
override fun execute(): Response {
//CAS判断是否已经被执行了, 确保只能执行一次,如果已经执行过,则抛出异常
check(executed.compareAndSet(false, true)) { "Already Executed" }
// 请求超时开始计时
timeout.enter()
// 开启请求监听
callStart()
try {
// 调用调度器的 executed 方法,这个方法只是将 call 放入到了 runningSyncCalls 中
client.dispatcher.executed(this)
// 调用 getResponseWithInterceptorChain 方法拿到 response
return getResponseWithInterceptorChain()
} finally {
// 执行完毕,调度器将这个 call 从 runningSyncCalls 中移除
client.dispatcher.finished(this)
}
}
异步请求
kotlin
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
}
});
RealCall.kt
java
override fun enqueue(responseCallback: Callback) {
// CAS判断是否已经被执行了, 确保只能执行一次,如果已经执行过,则抛出异常
check(executed.compareAndSet(false, true)) { "Already Executed" }
// 开启请求监听
callStart()
// 新建一个 AsyncCall 对象, 通过 dispathcer 的 enqueue 方法加入到 readyAsyncCalls 队列中
client.dispatcher.enqueue(AsyncCall(responseCallback))
}
Dispatcher.kt
java
internal fun enqueue(call: AsyncCall) {
synchronized(this) {
// 将请求加入到 readyAsyncCalls 队列中
readyAsyncCalls.add(call)
if (!call.call.forWebSocket) {
// 通过findExistingCallWithHost查找在runningAsyncCalls和readyAsyncCalls是否存在相同host的AsyncCall
// 如果存在则调用call.reuseCallsPerHostFrom()进行复用
val existingCall = findExistingCallWithHost(call.host)
if (existingCall != null)
call.reuseCallsPerHostFrom(existingCall)
}
}
// 通过线程池执行队列中的AsyncCall对象
promoteAndExecute()
}
// 查找在runningAsyncCalls和readyAsyncCalls是否存在相同host的AsyncCall
private fun findExistingCallWithHost(host: String): AsyncCall? {
for (existingCall in runningAsyncCalls) {
if (existingCall.host == host)
return existingCall
}
for (existingCall in readyAsyncCalls) {
if (existingCall.host == host)
return existingCall
}
return null
}
private fun promoteAndExecute(): Boolean {
this.assertThreadDoesntHoldLock()
val executableCalls = mutableListOf<AsyncCall>()
// 判断是否有请求正在执行
val isRunning: Boolean
//加锁,保证线程安全
synchronized(this) {
// 遍历 readyAsyncCalls 队列
val i = readyAsyncCalls.iterator()
while (i.hasNext()) {
val asyncCall = i.next()
// runningAsyncCalls 的数量不能超过最大请求并发数 64
if (runningAsyncCalls.size >= this.maxRequests)
break // Max capacity.
// 同域名最大请求书不能超过 5
if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost)
continue // Host max capacity.
// 从 readyAsyncCalls 队列中移除 并加入到 executableCalls 和 runningAsyncCalls 中
i.remove()
// asyncCall.callsPerHost() +1
asyncCall.callsPerHost.incrementAndGet()
executableCalls.add(asyncCall)
runningAsyncCalls.add(asyncCall)
}
// 通过正在运行的队列中的请求数量来判断是否有请求正在执行
// 包含 runningAsyncCalls 和 runningSyncCalls 两个队列中的请求
isRunning = runningCallsCount() > 0
}
// 遍历可执行队列,通过 AsyncCall 的 executeOn()方法 调用线程池来执行 asyncCall
for (i in 0 until executableCalls.size) {
val asyncCall = executableCalls[i]
asyncCall.executeOn(executorService)
}
return isRunning
}
- 首先将AsyncCall加入readyAsyncCalls队列中.
- 然后通过findExistingCallWithHost查找在runningAsyncCalls和readyAsyncCalls是否存在相同host的AsyncCall,如果存在则调用call.reuseCallsPerHostFrom()进行复用
- 最后调用 promoteAndExecute() 通过线程池执行队列中的AsyncCall对象
获取Response
AsyncCall 对象本质上就是一个Runnable 对象,线程执行就会调用该对象的run方法,而executeOn方法就是执行runable对象. 在run方法中主要执行了以下几步:
- 调用getResponseWithInterceptorChain()执行OkHttp拦截器,获取response对象
- 调用responseCallback的onResponse方法将Response对象回调出去
- 如果遇见IOException异常则调用responseCallback的onFailure方法将异常回调出去
- 如果遇到其他异常,调用cancel()方法取消请求,调用responseCallback的onFailure方法将异常回调出去
- 调用Dispatcher的finished方法结束执行
先看 getResponseWithInterceptorChain() 方法
java
internal fun getResponseWithInterceptorChain(): Response {
//拦截器列表
val interceptors = mutableListOf<Interceptor>()
interceptors += client.interceptors
interceptors += RetryAndFollowUpInterceptor(client)
interceptors += BridgeInterceptor(client.cookieJar)
interceptors += CacheInterceptor(client.cache)
interceptors += ConnectInterceptor
if (!forWebSocket) {
interceptors += client.networkInterceptors
}
interceptors += CallServerInterceptor(forWebSocket)
//构建拦截器责任链
val chain = RealInterceptorChain(
call = this,
interceptors = interceptors,
index = 0,
exchange = null,
request = originalRequest,
connectTimeoutMillis = client.connectTimeoutMillis,
readTimeoutMillis = client.readTimeoutMillis,
writeTimeoutMillis = client.writeTimeoutMillis
)
//如果call请求完成,那就意味着交互完成了,没有更多的东西来交换了
var calledNoMoreExchanges = false
try {
//执行拦截器责任链来获取 response
val response = chain.proceed(originalRequest)
//如果被取消,关闭响应,抛出异常
if (isCanceled()) {
response.closeQuietly()
throw IOException("Canceled")
}
return response
} catch (e: IOException) {
calledNoMoreExchanges = true
throw noMoreExchanges(e) as Throwable
} finally {
if (!calledNoMoreExchanges) {
noMoreExchanges(null)
}
}
}
从上方代码我们可以看出:
- 首先构建一个可变interceptor集合,将所有拦截器添加进去,这里如果是websocket则不添加networkInterceptor拦截器,这个interceptor集合的添加顺序也就是OkHttp拦截器的执行顺序
- 构建一个RealInterceptorChain对象,将所有的拦截器包裹
- 调用RealInterceptorChain的proceed的方法,获得Response对象
这里采用的是 责任链设计模式,构建RealInterceptorChain对象,然后执行proceed方法获取response对象
拦截器 Interceptor 接口
kotlin
fun interface Interceptor {
/** 拦截方法 */
@Throws(IOException::class)
fun intercept(chain: Chain): Response
interface Chain {
/** 原始请求数据 */
fun request(): Request
/** 核心方法,处理请求,获取response */
@Throws(IOException::class)
fun proceed(request: Request): Response
fun connection(): Connection?
fun call(): Call
fun connectTimeoutMillis(): Int
fun withConnectTimeout(timeout: Int, unit: TimeUnit): Chain
fun readTimeoutMillis(): Int
fun withReadTimeout(timeout: Int, unit: TimeUnit): Chain
fun writeTimeoutMillis(): Int
fun withWriteTimeout(timeout: Int, unit: TimeUnit): Chain
}
}
RealInterceptorChain 拦截器链
实现了Interceptor.Chain 接口, 核心逻辑是复写了 proceed 方法
kotlin
class RealInterceptorChain(
internal val call: RealCall,
private val interceptors: List<Interceptor>,
private val index: Int,
internal val exchange: Exchange?,
internal val request: Request,
internal val connectTimeoutMillis: Int,
internal val readTimeoutMillis: Int,
internal val writeTimeoutMillis: Int
) : Interceptor.Chain {
// 创建了一个RealInterceptorChain()对象
internal fun copy(
index: Int = this.index,
exchange: Exchange? = this.exchange,
request: Request = this.request,
connectTimeoutMillis: Int = this.connectTimeoutMillis,
readTimeoutMillis: Int = this.readTimeoutMillis,
writeTimeoutMillis: Int = this.writeTimeoutMillis
) = RealInterceptorChain(call, interceptors, index, exchange, request, connectTimeoutMillis,
readTimeoutMillis, writeTimeoutMillis)
···省略代码···
private var calls: Int = 0
override fun call(): Call = call
override fun request(): Request = request
@Throws(IOException::class)
override fun proceed(request: Request): Response {
check(index < interceptors.size)
calls++
if (exchange != null) {
check(exchange.finder.sameHostAndPort(request.url)) {
"network interceptor ${interceptors[index - 1]} must retain the same host and port"
}
check(calls == 1) {
"network interceptor ${interceptors[index - 1]} must call proceed() exactly once"
}
}
//index+1, 复制创建新的责任链,也就意味着调用责任链中的下一个处理者,也就是下一个拦截器
val next = copy(index = index + 1, request = request)
//取出当前拦截器
val interceptor = interceptors[index]
//执行当前拦截器的拦截方法
@Suppress("USELESS_ELVIS")
val response = interceptor.intercept(next) ?: throw NullPointerException(
"interceptor $interceptor returned null")
if (exchange != null) {
check(index + 1 >= interceptors.size || next.calls == 1) {
"network interceptor $interceptor must call proceed() exactly once"
}
}
check(response.body != null) { "interceptor $interceptor returned a response with no body" }
return response
}
}