OKHttp源码解析(流程分析)

对于OKHttp的源码解析,我一共分了三章

OkHttp源码解析(使用与基本类介绍)

OKHttp源码解析(流程分析)

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
    }
}
相关推荐
uhakadotcom39 分钟前
Loguru 全面教程:常用 API 串联与实战指南
后端·面试·github
培风图南以星河揽胜3 小时前
Java实习模拟面试|离散数学|概率论|金融英语|数据库实战|职业规划|期末冲刺|今日本科计科要闻速递:技术分享与学习指南
java·面试·概率论
艾斯比的日常4 小时前
JVM 内存结构:全面解析与面试重点
jvm·面试·职场和发展
gadiaola5 小时前
【计算机网络面试篇】HTTP
java·后端·网络协议·计算机网络·http·面试
鹏北海5 小时前
多标签页登录状态同步:一个简单而有效的解决方案
前端·面试·架构
程序员小白条6 小时前
你面试时吹过最大的牛是什么?
java·开发语言·数据库·阿里云·面试·职场和发展·毕设
孟陬7 小时前
【译+注】我用 10 种框架开发了同款应用:移动端性能框架评估
面试·前端框架
无敌最俊朗@7 小时前
Qt面试题day01
java·数据库·面试
Warren9817 小时前
Python自动化测试全栈面试
服务器·网络·数据库·mysql·ubuntu·面试·职场和发展
Dream it possible!20 小时前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树的最小绝对差(85_530_C++_简单)
c++·leetcode·面试