当前位置: 首页 > news >正文

OkHttp 源码浅析一

演进之路:原生Android框架不好用 ---- HttpUrlConnect   和 Apache HTTPClient  

第一版  底层使用HTTPURLConnect  

第二版 Square构建 从Android4.4开始

基本使用:

 val okhttp = OkHttpClient()val request = Request.Builder().url("http://www.baidu.com").build()okhttp.newCall(request).enqueue(object : okhttp3.Callback{override fun onFailure(call: okhttp3.Call, e: IOException) {Log.e("--->",e.message!!)}override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {Log.e("--->",response.body!!.string())}})//enqueue Call接口定义的抽象方法
//newCall RealCall创建出 有三个参数 1 okhttpclient  2.quest originnalRequest 3. forWebSocket //Booleaan
//WebSocket 是通过http创建连接
//

 override fun enqueue(responseCallback: Callback) {check(executed.compareAndSet(false, true)) { "Already Executed" }callStart()
//dispatcher 用来做线程调度 管理 Executor 线程池
//MaxRequests  = 64 最大64个线程  
//MaxRequestPerHost = 5 最多同一主机线程  5client.dispatcher.enqueue(AsyncCall(responseCallback))}override fun isExecuted(): Boolean = executed.get()private fun callStart() {
//跟踪错误分析和记录this.callStackTrace = Platform.get().getStackTraceForCloseable("response.body().close()")
//监听一系列事件 连接建立 断开连接 报文 head body 发送等eventListener.callStart(this)}
@get:Synchronized var maxRequests = 64 最多连接数量 64
@get:Synchronized var maxRequestsPerHost = 5 同一主机 5
internal fun enqueue(call: AsyncCall) {synchronized(this) {readyAsyncCalls.add(call)// Mutate the AsyncCall so that it shares the AtomicInteger of an existing running call to// the same host.if (!call.call.forWebSocket) {val existingCall = findExistingCallWithHost(call.host)if (existingCall != null) call.reuseCallsPerHostFrom(existingCall)}}promoteAndExecute()}

同步方法,Deque 双向队列

AsyncCall 共享变量 记录连接数等
if (!call.call.forWebSocket) {//记录数量val existingCall = findExistingCallWithHost(call.host)if (existingCall != null) call.reuseCallsPerHostFrom(existingCall)
}

private fun promoteAndExecute(): Boolean {this.assertThreadDoesntHoldLock()val executableCalls = mutableListOf<AsyncCall>()val isRunning: Booleansynchronized(this) {val i = readyAsyncCalls.iterator()while (i.hasNext()) {val asyncCall = i.next()if (runningAsyncCalls.size >= this.maxRequests) break // Max capacity.if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost) continue // Host max capacity.i.remove()asyncCall.callsPerHost.incrementAndGet()executableCalls.add(asyncCall)runningAsyncCalls.add(asyncCall)}isRunning = runningCallsCount() > 0}for (i in 0 until executableCalls.size) {val asyncCall = executableCalls[i]asyncCall.executeOn(executorService)}return isRunning}
promoteAndExecute 准备执行函数
readyAsyncCalls 没有执行过的请求 不会超出限制 64 or 5

executableCalls 添加到calls 然后取出遍历 执行 executeOn

val i = readyAsyncCalls.iterator() 遍历被执行的call
if (runningAsyncCalls.size >= this.maxRequests) break // Max capacity.
if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost) continue // Host max capacity. 超出连接限制的数量 终止请求
i.remove() //移除当前请求
asyncCall.callsPerHost.incrementAndGet()
executableCalls.add(asyncCall) 
runningAsyncCalls.add(asyncCall)

runningAsyncCalls 正在执行的Call

   for (i in 0 until executableCalls.size) {
      val asyncCall = executableCalls[i]

遍历准备执行的call  然后调用executeOn
      asyncCall.executeOn(executorService)
    }

Okhttp enqueue ----> newCall ---> RealCall enqueue ------>  执行 dispatcher . enqueue ---->  添加到readyAsyncCalls ----> promoteAndExecute ---executeOn 执行

  fun executeOn(executorService: ExecutorService) {client.dispatcher.assertThreadDoesntHoldLock()var success = falsetry {executorService.execute(this)success = true} catch (e: RejectedExecutionException) {val ioException = InterruptedIOException("executor rejected")ioException.initCause(e)noMoreExchanges(ioException)responseCallback.onFailure(this@RealCall, ioException)} finally {if (!success) {client.dispatcher.finished(this) // This call is no longer running!}}}

executorService dispatcher 内部类管理的对象 线程调度 参数是runnable

        executorService.execute(this) 线程切换到后台线程 Async 实现了Runnable

线程调度

 override fun run() {threadName("OkHttp ${redactedUrl()}") {var signalledCallback = falsetimeout.enter()try {val response = getResponseWithInterceptorChain()signalledCallback = trueresponseCallback.onResponse(this@RealCall, response)} catch (e: IOException) {if (signalledCallback) {// Do not signal the callback twice!Platform.get().log("Callback failure for ${toLoggableString()}", Platform.INFO, e)} else {responseCallback.onFailure(this@RealCall, e)}} catch (t: Throwable) {cancel()if (!signalledCallback) {val canceledException = IOException("canceled due to $t")canceledException.addSuppressed(t)responseCallback.onFailure(this@RealCall, canceledException)}throw t} finally {client.dispatcher.finished(this)}}}

    val response = getResponseWithInterceptorChain() 获取相应 

getResponseWithInterceptorChain 请求数据的方法封装

responseCallback 代码里声明传入的Callback

http://www.lryc.cn/news/131042.html

相关文章:

  • 【解决问题】远程仓库GitHub/GitLab添加了SSH Key之后依然无法clone的解决办法
  • 回归预测 | MATLAB实现SA-SVM模拟退火算法优化支持向量机多输入单输出回归预测(多指标,多图)
  • Spring事务和事务传播机制(1)
  • 如何快速在vscode中实现不同python文件的对比查看
  • 网络安全---Ring3下动态链接库.so函数劫持
  • leetcode283. 移动零
  • GuLi商城-前端基础Vue-生命周期和钩子函数
  • 输入输出+暴力模拟入门:魔法之树、染色の树、矩阵、字母加密、玫瑰鸭
  • ​Kubernetes的演变:从etcd到分布式SQL的过渡
  • 29、简单通过git把项目远程提交到gitee
  • 元宇宙之应用(04)沉浸式游戏
  • 浙大数据结构第八周之08-图7 公路村村通
  • SpringBoot 解决跨域问题
  • 2023 年牛客多校第十场题解
  • 韦东山老师 RTOS 入门课程(一)RTOS 介绍,熟悉裸机的汇编逻辑
  • WebRTC | SDP详解
  • Springboot 实践(9)springboot集成Oauth2.0授权包,5个接口文件配置详解
  • 最新AI系统ChatGPT程序源码/支持GPT4/自定义训练知识库/GPT联网/支持ai绘画(Midjourney)+Dall-E2绘画/支持MJ以图生图
  • 【高频面试题】 消息中间件
  • 物联网智慧安防实训综合实训基地建设方案
  • openGauss学习笔记-44 openGauss 高级数据管理-存储过程
  • 【Linux】进程信号篇Ⅲ:可重入函数、volatile关键字、SIGCHLD信号
  • 排序算法:冒泡排序
  • Spring事件监听源码解析
  • Cpp学习——list的模拟实现
  • 工具推荐:Chat2DB一款开源免费的多数据库客户端工具
  • C语言刷题指南(二)
  • [C++11]
  • 【MySQL系列】--初识数据库
  • Unity导入google.protobuf失败,无法找到google命名空间