TCP

TCP连接流程

网络请求工具 - 图1

OkHttp

连接池默认参数:最大空闲连接数 5, 最大空闲时间 5分钟

dispatcher.setMaxRequestsPerHost(MAX_REQUEST_PER_HOST); // 默认值5
dispatcher.setMaxRequests(MAX_REQUEST); // 默认值 64

连接流程:发起Connect(CONNECT_TIMEOUT) -》发送数据 Write(WRITE_TIMEOUT) -》 读取响应数据 Read(READ_TIMEOUT)

网络请求工具 - 图2

耗时操作 调用位置
DNS解析 「ConnectInterceptor」streamAllocation.newStream
「RouteSelector」address.dns().lookup()
连接时间 「ConnectInterceptor」streamAllocation.newStream
「Platform」socket.connect()
写入request 「CallServerInterceptor」httpCodec.writeRequestHeaders
服务器响应 「ConnectInterceptor」streamAllocation.newStream
「RealConnection」socket.connect()
读取response 「CallServerInterceptor」httpCodec.readResponseHeaders
api 简介 生效机制
callTimeout() 整个流程耗费的超时时间 RealCall.execute方法,设置进入
AsyncTimeout + WatchDog实现
connectTimeout() 三次握手 + SSL建立耗时 socket.connect(address, connectTimeout)
readTimeout() source读取耗时 source.timeout(readTimeout)
AsyncTimeout + WatchDog实现
rawSocket读取耗时 rawSocket.setSoTimeout(readTimeout)
writeTimeout() sink写入耗时 sink.timeout(writeTimeout)
AsyncTimeout + WatchDog实现
route.requiresTunnel() callTimeout = dns + connection + readTimeout + readTimeout + writeTimeout + 其它
callTimeout = dns + connectTime + readTimeout + 其它

Apache

Squid