百度App网络深度优化系列《二》连接优化
<div style="color: black; text-align: left; margin-bottom: 10px;">
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>前言</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<a style="color: black;">系列《一》</a>里<span style="color: black;">大众</span><span style="color: black;">认识</span>到网络优化<span style="color: black;">通常</span>会<strong style="color: blue;">首选优化DNS</strong>,而接下来的HTTP协议<span style="color: black;">作为</span>优化的重点,<span style="color: black;">通常</span>优化者会<span style="color: black;">选取</span>协议切换,合并请求,精简数据包<span style="color: black;">体积</span>等手段来对HTTP协议进行优化,严谨的说这都不属于网络优化的范畴。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">HTTP协议的<span style="color: black;">基本</span>是连接,<span style="color: black;">因此</span><span style="color: black;">咱们</span>的系列《二》连接优化应运而生</strong>,<span style="color: black;">期盼</span>对<span style="color: black;">大众</span>在网络方向的学习和实践有所<span style="color: black;">帮忙</span>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>背景</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接优化需要<span style="color: black;">处理</span>两个核心问题</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. <strong style="color: blue;">连接<span style="color: black;">创立</span>耗时较长,<span style="color: black;">引起</span>请求的总时长变长,<span style="color: black;">从而</span>影响用户体验</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. <strong style="color: blue;">在多变的网络环境下,连接<span style="color: black;">创立</span>的过程可能会失败,<span style="color: black;">引起</span>成功率下降,<span style="color: black;">从而</span>影响用户体验</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App承载着亿级流量,<span style="color: black;">针对</span>每一个请求都需要追求耗时短,成功率高的体验。从协议<span style="color: black;">方向</span>出发,<span style="color: black;">怎样</span>才能做到这一点呢?<span style="color: black;">首要</span><span style="color: black;">咱们</span>来看下<span style="color: black;">创立</span>连接耗时的原理。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9895baded7a048759e103ad8db6bbe9a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=Ycyy8ivigwz2WQopGjlNrM36E%2FU%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">创立</span>连接耗时的原理</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从上图<span style="color: black;">咱们</span>能清晰的看出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. DNS Query需要1个RTT(Round-Trip Time,即往返时间),百度App都是基于HTTPDNS服务的,<span style="color: black;">因此</span>大部分会命中缓存,<span style="color: black;">倘若</span>降级走了系统DNS,<span style="color: black;">亦</span>会命中缓存,命中不了的<span style="color: black;">因为</span>是基于UDP协议,<span style="color: black;">因此</span>在连接耗时上<span style="color: black;">无</span>太大的影响,线上的数据<span style="color: black;">亦</span>能说明这点。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. TCP要经历SYN,SYN/ACK,ACK三次握手的1.5个RTT,<span style="color: black;">不外</span>ACK和ClientHello合并了,<span style="color: black;">因此</span><span style="color: black;">便是</span>1个RTT。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. TLS(Transport Layer Security,即传输层安全性协议)需要经过握手和密钥交换2个RTT。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">综上所述,DNS,TLS,TCP握手<span style="color: black;">周期</span>用了4个RTT才到了ApplicationData<span style="color: black;">周期</span>,<span style="color: black;">亦</span><span style="color: black;">便是</span>数据<span style="color: black;">起始</span>传输<span style="color: black;">周期</span></strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>上面的分析<span style="color: black;">能够</span>总结出,<span style="color: black;">倘若</span><span style="color: black;">咱们</span>能<span style="color: black;">尽可能</span>的将TLS和TCP的RTT减少,将会大大降低连接耗时的时间。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>连接优化<span style="color: black;">咱们</span>都能做什么</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的优化<span style="color: black;">目的</span>分为两类,<strong style="color: blue;">一类是TLS的连接优化,一类是TCP的连接优化</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">TLS的连接优化</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TLS的连接优化,需要服务端和客户端都需要支持,<span style="color: black;">一起</span>完成优化手段,<span style="color: black;">包含</span>Session Resumption和False Start。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Session Resumption</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Session Resumption中文意思是会话复用,下图讲解了Session Resumption的协议原理。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9cf2804ed0474e5eae921c1fb71a161d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=iOSuNOzbgM%2FY3T6kgaReFUWbWHs%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Session Resumption的协议原理</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>上图<span style="color: black;">能够</span>看出TLS密钥协商交换的过程<span style="color: black;">无</span>了,但<span style="color: black;">详细</span>是<span style="color: black;">怎样</span>实现的呢?<span style="color: black;">包括</span>两种方式,<strong style="color: blue;">一种是Sesssion Identifier,一种是Session Ticket</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1)Session Identifier</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Session Identifier中文为会话标识符,<strong style="color: blue;">更像<span style="color: black;">咱们</span>熟知的session的概念</strong>。是 TLS 握手中生成的 Session ID。服务端会将Session ID<span style="color: black;">保留</span>起来,客户端<span style="color: black;">亦</span>会存储Session ID,在后续的ClientHello中带上它,服务端<span style="color: black;">倘若</span>能找到匹配的信息,就<span style="color: black;">能够</span>完成一次快速握手。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2)Session Ticket</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Session Identifier存在<span style="color: black;">有些</span><span style="color: black;">坏处</span>端,<span style="color: black;">例如</span>客户端多次请求<span style="color: black;">倘若</span><span style="color: black;">无</span>落在同一台<span style="color: black;">设备</span>上就<span style="color: black;">没法</span>找到匹配的信息,但Session Ticket<span style="color: black;">能够</span>。<strong style="color: blue;">Session Ticket更像<span style="color: black;">咱们</span>熟知的cookie的概念</strong>,Session Ticket用<span style="color: black;">仅有</span>服务端<span style="color: black;">晓得</span>的安全密钥加密过的会话信息,<span style="color: black;">保留</span>在客户端上。客户端在ClientHello时带上了Session Ticket,服务器<span style="color: black;">倘若</span>能成功解密就<span style="color: black;">能够</span>完成快速握手。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不管是Session Identifier还是Session Ticket都存在时效性问题,不是永久生效,<span style="color: black;">针对</span>这两种方式<span style="color: black;">大众</span><span style="color: black;">能够</span>查看参考资料【4】。<strong style="color: blue;">百度App的网络协议层对这两种方式都是支持的</strong>,省去了TLS握手过程中证书下载,密钥协商交换的环节,节省了1个RTT的时间。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">False Start</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">False Start的中文意思是抢跑,下图讲解了False Start的协议原理。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/137c2e045b314bf5979c203afad9f28d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=vvoO2bZGImUtdJHMyn8FL6HGFLk%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">False Start的协议原理</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上图很清晰的说明在TLS<span style="color: black;">第1</span>步握手成功后,客户端在发送Change Cipher Spec Finished的<span style="color: black;">同期</span><span style="color: black;">起始</span>数据传输,服务端在TLS握手完成时直接返<span style="color: black;">回复</span>用数据。<strong style="color: blue;">应用数据的发送<span style="color: black;">实质</span>上并未等到握手<span style="color: black;">所有</span>完成,<span style="color: black;">因此</span><span style="color: black;">叫作</span>之为抢跑</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从结果看省去了1个RTT的时间。False Start有两个前提<span style="color: black;">要求</span>,一是要<span style="color: black;">经过</span>应用层协议协商ALPN(Application Layer Protocol Negotiation)握手,二是要支持前向安全的加密算法。False Start在未完成握手的<span style="color: black;">状况</span>下就发送了数据,前向安全<span style="color: black;">能够</span><span style="color: black;">加强</span>安全性,<span style="color: black;">详细</span>协议实现,<span style="color: black;">大众</span><span style="color: black;">能够</span>查看参考资料【3】。百度App的网络协议层对False Start是支持的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>说句题外话,其实TCP层有个类似的连接优化手段叫Fast Open,感兴趣的<span style="color: black;">朋友</span>,<span style="color: black;">能够</span>查看参考资料【5】。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Session Resumption和False Start的区别</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">两者<span style="color: black;">针对</span>TLS<span style="color: black;">来讲</span>都是节省一个RTT,Session Resumption在<span style="color: black;">第1</span>次握手时还是需要2个RTT,在第二次握手时<span style="color: black;">才可</span>复用减少到1个RTT。False Start是端上的<span style="color: black;">行径</span>,故每次都会减少到1个RTT。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">TCP的连接优化</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TCP的连接优化,<span style="color: black;">咱们</span>先从连接池说起,<span style="color: black;">首要</span>让<span style="color: black;">咱们</span>来认识下连接池都有<span style="color: black;">那些</span>类型。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. 连接池</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/99937092bcf847b9b6175f98beeb0d15~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=M7zAZ%2B8UpDz6S3dG4I59L6Lly98%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接池的类型</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上图展示了连接池的<span style="color: black;">区别</span>类型,都是<span style="color: black;">大众</span>耳熟能详的协议连接池,有低级连接池,<span style="color: black;">包括</span>TCP连接池(管理HTTP请求的连接)和WebSocket连接池(管理WebSocket连接)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有高级连接池,<span style="color: black;">包含</span>HTTP代理连接池(管理HTTP代理请求的连接),SpdySession连接池(管理SPDY和HTTP/2请求的连接),SOCKS连接池(管理SOCKS和SOCKS5代理的连接),SSL连接池(管理HTTPS请求的连接)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区别</span>类型的连接池以组合的形式互相复用能力。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)SSL连接池管理的是SSLSocket,但SSLSocket又依赖于TCP连接池<span style="color: black;">供给</span>的TCPSocket。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)HTTP代理连接池<span style="color: black;">倘若</span>走HTTP协议,<span style="color: black;">那样</span>就需要TCP连接池<span style="color: black;">供给</span>TCPSocket,<span style="color: black;">倘若</span>走HTTPS协议,<span style="color: black;">那样</span>就需要SSL连接池<span style="color: black;">供给</span>SSLSocket。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)SpdySession连接池依赖SSL连接池<span style="color: black;">供给</span>SSLSocket,<span style="color: black;">这儿</span>需要说明下,虽然HTTP/2协议<span style="color: black;">无</span>强制绑定HTTPS,<span style="color: black;">然则</span>在<span style="color: black;">实质</span><span style="color: black;">研发</span>中确实都是绑定HTTPS,百度App<span style="color: black;">运用</span>ALPN来协商HTTP/2。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4)SOCKS连接池管理的SOCKSSocket和SOCKS5Socket都需要依赖TCP连接池<span style="color: black;">供给</span>的TCPSocket,虽然SOCKS5支持UDP,但cronet网络库暂时<span style="color: black;">无</span>实现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5)WebSocket连接池依赖TCP连接池<span style="color: black;">供给</span>的TCPSocket,声明下<span style="color: black;">这儿</span><span style="color: black;">无</span>说明WSS(Web Socket Secure)的<span style="color: black;">状况</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TCP连接优化是一个比较<span style="color: black;">繁杂</span>的内容,<strong style="color: blue;">百度App做了针对性场景优化,包括预连接,连接重建,备用连接,复合连接</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. 预连接</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/6daac888e76546ff92f84635ebe5caeb~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=V3VM5Dn5i2QH%2Bcd1U3BraPqfxqk%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">预连接和连接重建</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">预连接,预先创建好的连接。<strong style="color: blue;">它<span style="color: black;">处理</span>的场景是在App<span style="color: black;">运用</span><span style="color: black;">周期</span><span style="color: black;">能够</span>无耗时的获取连接</strong>。下面用四个问答来解释预连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题一:预连接<span style="color: black;">是不是</span>能<span style="color: black;">处理</span>所有网络请求的提前连接<span style="color: black;">创立</span>?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:答案<span style="color: black;">是不是</span>定的,预连接需要业务方进行核心业务的<span style="color: black;">评定</span>,针对核心的域名进行预连接的<span style="color: black;">创立</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题二:预连接既然针对的是特定的域名,<span style="color: black;">那样</span>是<span style="color: black;">怎样</span>配置的呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:采用域名+连接数的方式进行配置,<span style="color: black;">例如</span>https://a.baidu.com|2,<span style="color: black;">暗示</span>给a.baidu.com这个域名配置两条预连接,<span style="color: black;">这儿</span>要说明下,在HTTP/1.x协议下,网络库的实现都会<span style="color: black;">针对</span>单域名有最大连接数的限制,<span style="color: black;">区别</span>网络库的个数限制不<span style="color: black;">同样</span>,有5个<span style="color: black;">亦</span>有6个,但<span style="color: black;">针对</span>HTTP/2协议,这个连接数就只能是1个。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题三:预连接是<span style="color: black;">怎样</span><span style="color: black;">创立</span>的?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:在网络库初始化的时候,会<span style="color: black;">按照</span><span style="color: black;">运用</span>者的配置延迟5s进行预连接的<span style="color: black;">创立</span>,<span style="color: black;">重点</span>是<span style="color: black;">思虑</span>网络库在冷<span style="color: black;">起步</span>下<span style="color: black;">针对</span><span style="color: black;">起步</span>性能的影响,为了<span style="color: black;">保准</span>网络库的整体性能,预连接的总个数限制在20个。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题四:预连接是如何保持的?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:在网络库初始化的时候,除了进行预连接的<span style="color: black;">创立</span>,还会创建一个预连接的<span style="color: black;">按时</span>器,这个<span style="color: black;">按时</span>器会每隔31s,这个值的设定取决于BFE(Baidu Front End,是七层流量的统一接入系统)和BGW(Baidu Gate Way,百度自主<span style="color: black;">开发</span>的四层负载<span style="color: black;">平衡</span>平台)对超时的最小值设定,<span style="color: black;">按照</span><span style="color: black;">运用</span>者的配置重新<span style="color: black;">创立</span>连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3. 连接重建</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接重建,将连接重新<span style="color: black;">创立</span>。<strong style="color: blue;">它<span style="color: black;">处理</span>的场景是App网络状态<span style="color: black;">出现</span>变化,IP<span style="color: black;">位置</span>变化,<span style="color: black;">引起</span>连接不可用</strong>。下面用三个问答来解释连接重建。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题一:连接重建<span style="color: black;">是不是</span>针对连接池里的所有连接?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:答案是肯定的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题二:连接重建的过程是什么样的?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:在网络状态变化的时候,<span style="color: black;">第1</span>步会清除掉连接池里的idle socket,何为idle socket?即空闲socket,<span style="color: black;">针对</span>从未<span style="color: black;">运用</span>过的空闲socket超过60秒清除,<span style="color: black;">针对</span><span style="color: black;">运用</span>过的空闲socket超过90秒清除。第二步重建连接需要等待200ms,目的是等待DNS先重建完成。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题三:连接重建<span style="color: black;">针对</span>性能有影响吗?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:出于性能<span style="color: black;">思虑</span>,连接重建的连接个数限制是100个。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4. 备用连接</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c292694e06ee4f29a53fcf7d813cfb8f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=uFAdIZKRYPMEYbOhH3TzUS1BZVA%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">备用连接和复合连接</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">备用连接,预备的连接。<strong style="color: blue;">它<span style="color: black;">处理</span>的场景是正常发送一个请求当group内无连接可用的时候</strong>(何为group?group是管理socket的最小单元,内部<span style="color: black;">包括</span>活跃socket,空闲socket,连接任务,等待请求)。下面用三个问答来解释备用连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题一:备用连接<span style="color: black;">是不是</span>针对所有请求?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:答案是肯定的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题二:备用连接的过程是什么样的?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:当有请求来临时,连接池内无连接可用,会<span style="color: black;">起步</span>一个<span style="color: black;">按时</span>器开启备用连接,<span style="color: black;">按时</span>器的间隔时间是250ms,与主连接进行竞争,<span style="color: black;">倘若</span>主连接<span style="color: black;">由于</span>网络抖动<span style="color: black;">或</span>网络状态<span style="color: black;">欠好</span>,<span style="color: black;">引起</span>连接失败,<span style="color: black;">那样</span>备用连接就直接发送请求。<span style="color: black;">倘若</span>主连接成功,<span style="color: black;">那样</span>备用连接就被取消掉。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题三:备用连接的目的是什么?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:在连接池无连接的<span style="color: black;">状况</span>下,务必是要创建连接的,在主连接之外加一个备用连接,会大大<span style="color: black;">提高</span>创建连接的成功率,从而<span style="color: black;">提高</span>用户体验。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5. 复合连接:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">复合连接,即多条连接。<strong style="color: blue;">它<span style="color: black;">处理</span>的场景是为了多个IP<span style="color: black;">位置</span>的连接<span style="color: black;">选择</span>问题</strong>。下面用三个问答来解释复合连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题一:复合连接<span style="color: black;">是不是</span>针对所有请求?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:答案是肯定的。复合连接<span style="color: black;">能够</span>全局开关,百度App现<span style="color: black;">周期</span>暂时<span style="color: black;">无</span>开启复合连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题二:复合连接的过程是什么样的?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:众所周知域名DNS<span style="color: black;">查找</span><span style="color: black;">通常</span><span style="color: black;">状况</span>下会返回多个IP,<span style="color: black;">咱们</span>以域名<span style="color: black;">查找</span>返回两个IP为例</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)<span style="color: black;">倘若</span>结果中存在IPv6的<span style="color: black;">位置</span>,<span style="color: black;">那样</span>会优先<span style="color: black;">选择</span>IPv6的<span style="color: black;">位置</span>,这个规则follow HappyEyeBall机制(可参考系列一<span style="color: black;">针对</span>HappyEyeBall的介绍)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2) 接下来这两个IP会<span style="color: black;">根据</span><span style="color: black;">次序</span>尝试<span style="color: black;">创立</span>连接,<span style="color: black;">倘若</span><span style="color: black;">第1</span>个IP返回失败,将立即<span style="color: black;">起始</span>连接第二个IP。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)<span style="color: black;">倘若</span><span style="color: black;">第1</span>个IP率先成功返回,<span style="color: black;">那样</span>第二个IP将被加入连接尝试列表并停止所有尝试连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4)<span style="color: black;">倘若</span><span style="color: black;">第1</span>个IP失败,会立刻<span style="color: black;">起始</span>第二个IP的连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5)<span style="color: black;">倘若</span><span style="color: black;">第1</span>个IP<span style="color: black;">处在</span>pending状态,<span style="color: black;">那样</span>会<span style="color: black;">起步</span>一个<span style="color: black;">按时</span>器,默认延迟2s会发起第二个IP的连接,<span style="color: black;">倘若</span>是多个IP将会递归连接,需要<span style="color: black;">尤其</span>说明下,<span style="color: black;">区别</span>的网络制式延迟时间会不<span style="color: black;">同样</span>,<span style="color: black;">这般</span>体验<span style="color: black;">亦</span>会更好。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题三:复合连接的目的是什么?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答:复合连接的好处是<span style="color: black;">供给</span>最优的IP<span style="color: black;">选择</span>机制,但<span style="color: black;">亦</span>会带来服务端的高负载,<span style="color: black;">因此</span>使用的时候需要进行综合<span style="color: black;">评定</span>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4、</span>连接优化的最佳实践</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App<span style="color: black;">日前</span>客户端网络架构<span style="color: black;">因为</span>历史<span style="color: black;">原由</span>还未统一,<span style="color: black;">不外</span><span style="color: black;">咱们</span>正朝着这个<span style="color: black;">目的</span><span style="color: black;">奋斗</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">咱们</span>的中心思想是以系统网络库的API调用接口为中心,上层<span style="color: black;">创立</span>网络门面,供<span style="color: black;">外边</span><span style="color: black;">方便</span>调用,底层<span style="color: black;">经过</span>系统机制以AOP的方式将cronet(chromium的net模块)注入进系统网路库,达到双端网络架构统一,能力复用</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面着重介绍下连接优化在Android和iOS网络架构中的位置及实践。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 连接优化在Android网络架构的位置及实践</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/444d2fc133904ae18289f78f6279dc7f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=bKzxV3G7vYfPR1Yh8tBjSLk4kmU%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接优化在Android网络架构的位置</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的Android网络流量<span style="color: black;">日前</span>都在okhttp之上,上层进行了网络门面的封装,封装内部的实现细节和对外友好的API,<span style="color: black;">日前</span><span style="color: black;">咱们</span>正在进行重构,默认采用Android标准的网络接口HttpURLConnection,它的底层由系统<span style="color: black;">供给</span>的okhttp的实现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">订制方面利用URL Stream Protocol机制将HttpURLConnection底层网络协议栈接管为cronet,供各个业务和<span style="color: black;">基本</span>模块使用,连接优化的所有内容在cronet网络库内部实现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 连接优化在iOS网络架构的位置及实践</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c22d652019fb47d4a814f9adb2f76cad~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728454549&x-signature=2zU3UurGEuzaYL8GXktat2NVuU0%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接优化在iOS网络架构的位置</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的iOS网络流量<span style="color: black;">日前</span>都在cronet之上,上层<span style="color: black;">咱们</span><span style="color: black;">运用</span>iOS的URL Loading System机制将cronet stack注入进URLSession里,<span style="color: black;">这般</span><span style="color: black;">咱们</span>就<span style="color: black;">能够</span>直接<span style="color: black;">运用</span>URLSession的API进行网络的操作<span style="color: black;">况且</span>更易于系统<span style="color: black;">守护</span>,在上层封装了网络门面,供各个业务和<span style="color: black;">基本</span>模块<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">在cronet内部实现了预连接(<span style="color: black;">重点</span>针对百度App的几个核心域名进行预连和保活),连接重建(针对所有请求),备用连接(针对所有请求),复合连接(iOS上暂时<span style="color: black;">无</span>开启),Session Resumption(针对所有请求),False Start(针对所有请求)</strong>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">5、</span>收益</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接优化的收益<span style="color: black;">重点</span><span style="color: black;">表现</span>在网络时延和网络成功率上,这两点收益需要结合业务<span style="color: black;">来讲</span>,以百度App Feed刷新这个典型业务场景为例。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Feed刷新文本请求网络时延<strong style="color: blue;">降低16%</strong>,Feed刷新<span style="color: black;">照片</span>请求网络时延<strong style="color: blue;">降低12%</strong>,可谓收益相当<span style="color: black;">显著</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">成功率方面,Feed刷新文本请求成功率<strong style="color: blue;"><span style="color: black;">提高</span>0.29%</strong>,Feed刷新<span style="color: black;">照片</span>请求成功率<strong style="color: blue;"><span style="color: black;">提高</span>0.23%</strong>,<span style="color: black;">亦</span>是非常不错的收益。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">6、</span>结语</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">连接优化是个<span style="color: black;">连续</span>性的<span style="color: black;">专题</span>,<span style="color: black;">无</span>最优<span style="color: black;">仅有</span>更优。上面介绍的百度App的<span style="color: black;">有些</span>经验和做法并不见得完美,但<span style="color: black;">咱们</span>会继续深入的优化下去,<span style="color: black;">连续</span><span style="color: black;">提高</span>百度App的网络性能。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上优化由百度App团队,内核团队,OP团队共建完成。最后感谢<span style="color: black;">大众</span>的辛苦阅读,<span style="color: black;">期盼</span>对你有所<span style="color: black;">帮忙</span>,后面会继续推出-百度App网络深度优化系列《三》弱网优化,敬请期待。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">7、</span>参考资料</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. https://chromium.googlesource.com/chromium/src/+/HEAD/docs/android_build_instructions.md</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ios/build_instructions.md</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. https://tools.ietf.org/html/rfc7918 False Start</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. https://tools.ietf.org/html/rfc5077 Session Resumption</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. https://tools.ietf.org/html/rfc7413 TCP Fast Open</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原创: 蔡锐 百度App技术</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">拓展阅读:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">百度App网络深度优</a>化系列《一》DNS优化</p>
</div>
回顾历史,我们不难发现:无数先辈用鲜血和生命铺就了中华民族复兴的康庄大道。 我完全同意你的看法,期待我们能深入探讨这个问题。 外链发布社区 http://www.fok120.com/
页:
[1]