HTTP协议 keep-alive连接 与 BS(firefox-thttpd)实验
什么是 keep-alive 连接
https://en.wikipedia.org/wiki/HTTP_persistent_connection
HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer HTTP/2 protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection.
https://www.byvoid.com/blog/http-keep-alive-header
我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成 之后立即断开连接(HTTP协议为无连接的协议);当使用Keep-Alive模式(又称持久连接、连接重用)时,Keep-Alive功能使客户端到服 务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接。
http 1.0中默认是关闭的,需要在http头加入"Connection: Keep-Alive",才能启用Keep-Alive;http 1.1中默认启用Keep-Alive,如果加入"Connection: close ",才关闭。目前大部分浏览器都是用http1.1协议,也就是说默认都会发起Keep-Alive的连接请求了,所以是否能完成一个完整的Keep- Alive连接就看服务器设置情况。
keep-alive 连接 的 优点
https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.1
Persistent HTTP connections have a number of advantages:
- By opening and closing fewer TCP connections, CPU time is saved
in routers and hosts (clients, servers, proxies, gateways,
tunnels, or caches), and memory used for TCP protocol control
blocks can be saved in hosts.- HTTP requests and responses can be pipelined on a connection.
Pipelining allows a client to make multiple requests without
waiting for each response, allowing a single TCP connection to
be used much more efficiently, with much lower elapsed time.- Network congestion is reduced by reducing the number of packets
caused by TCP opens, and by allowing TCP sufficient time to
determine the congestion state of the network.- Latency on subsequent requests is reduced since there is no time
spent in TCP's connection opening handshake.- HTTP can evolve more gracefully, since errors can be reported
without the penalty of closing the TCP connection. Clients using
future versions of HTTP might optimistically try a new feature,
but if communicating with an older server, retry with old
semantics after an error is reported.HTTP implementations SHOULD implement persistent connections.
https://en.wikipedia.org/wiki/HTTP_persistent_connection
Advantages
- Lower CPU and memory usage (because fewer connections are open simultaneously).
- Enables HTTP pipelining of requests and responses.
- Reduced network congestion (fewer TCP connections).
- Reduced latency in subsequent requests (no handshaking).
- Errors can be reported without the penalty of closing the TCP connection.
These advantages are even more important for secure HTTPS connections, because establishing a secure connection needs much more CPU time and network round-trips.
1、 较低的CPU和内存消耗。
2、 启用HTTP管道线功能
3、 减少网络拥塞
4、 减少后续请求的延迟
5、 遇到错误,可以报告错误, 不用强行关闭。
对于 https 连接效果显著, 因为https的握手流程很是耗时!
keep-alive 连接 的 缺点
https://en.wikipedia.org/wiki/HTTP_persistent_connection
Disadvantages
If the client does not close the connection when all of the data it needs has been received, the resources needed to keep the connection open on the server will be unavailable for other clients. How much this affects the server's availability and how long the resources are unavailable depend on the server's architecture and configuration.
浏览器支持性
Use in web browsers
All modern web browsers use persistent connections, including Google Chrome, Firefox, Internet Explorer (since 4.01), Opera (since 4.0)[8] and Safari.
By default, Internet Explorer versions 6 and 7 use two persistent connections while version 8 uses six.[9] Persistent connections time out after 60 seconds of inactivity which is changeable via the Windows Registry.[10]
In Firefox, the number of simultaneous connections can be customized (per-server, per-proxy, total). Persistent connections time out after 115 seconds (1.92 minutes) of inactivity which is changeable via the configuration.[11]
thttpd实验性实现 keep-alive
https://github.com/fanqingsong/thttpd_keepalive
要点:
1、 响应报文头中 connection close 修改为 keep-alive
2、 httpd_get_conn 中 对hc进行 内存初始化 和 设置初始值的 代码分离出独立 函数
httpd_con_initialize_content
httpd_con_initialize_mem
3、 handle_linger 改造, 不再执行延迟关闭了, 修改为 延迟定时器, 并初始化 hc
4、 clear_connection 中, shutdown注释掉, c->linger_timer初始化或者 延迟。
------- 实验效果 ------
以此网页内容实验, 第一次加载后, 多次刷新页面
之前
1.22 1.46 1.68 1.2 1.03
之后
1.21 1.37 1.16 1.43 1.25
改进不是很显著, 应该与 浏览器的并发无关, 与 thttpd的对于静态资源的单线程机制有关, 已经是串行。
且 本实验不需要DNS解析, 和https握手。
对应的代码打印,一个连接fd,证明keep-alive生效:
https://github.com/fanqingsong/thttpd_keepalive/blob/master/thttpd-2.27/logfile_fqs
服务器段抓包:
https://holmesian.org/Ubuntu_tcpdump
tcpdump -X -s 0 -w aaa host 192.9.200.59 and tcp port 8000
https://raw.githubusercontent.com/fanqingsong/thttpd_keepalive/master/thttpd-2.27/tcpdump_result.txt
HTTP协议 keep-alive连接 与 BS(firefox-thttpd)实验的更多相关文章
- MQTT 协议学习:007-Keep Alive 连接保活 与 对应报文(PINGREQ、PINGRESP)
背景 keep alive 是 CONNECT 报文中可变头的一部分. 我们提到过 Broker 需要知道 Client 是否非正常地断开了和它的连接,以发送遗愿消息.实际上 Client 也需要能够 ...
- (转)对Http协议的长连接和短连接新的认识
转载来自:http://www.cnblogs.com/zuoxiaolong/p/life49.html一直对长连接短连接模模糊糊,看着该博主的文章,豁然开朗~ 引言 最近刚到公司不到一个月,正处于 ...
- http协议及长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议. IP协议主要解决网络路由和寻址 ...
- MQTT协议笔记之连接和心跳
前言 本篇会把连接(CONNECT).心跳(PINGREQ/PINGRESP).确认(CONNACK).断开连接(DISCONNECT)和在一起. CONNECT 像前面所说,MQTT有关字符串部分采 ...
- Websocket协议之握手连接
Websocket协议是为了解决web即时应用中服务器与客户端浏览器全双工通信的问题而设计的,是完全意义上的Web应用端的双向通信技术,可以取代之前使用半双工HTTP协议而模拟全双工通信,同时克服了带 ...
- OSI七层协议与TCP连接
概述 为了追求效率,我们写代码,不可能去关注底层知识,但往往到出了问题,或者性能调优.我们就会速手无策,仔细为自己查缺补漏,总结知识点. 网络协议 互联网的本质就是一系列的网络协议,让不同计算机能够互 ...
- TCP连接三次握手协议,释放连接四次挥手,以及使用 awl伪造mac地址进行多线程syn洪泛攻击。
这个TCP连接就是一次追女生-谈恋爱-分手,追求比分手简单,但是分手比追求复杂.哥,谈了半年的女朋友,在就快要成功了的时候分了,原因是因为有人在后面该老子背后搞SYN洪泛攻击,最后女朋友丢失了.学会T ...
- TCP/IP协议的建立连接与关闭连接过程
一.建立连接(三次握手) 第一次握手:建立连接时,客户端发送SYN(seq=x)包到服务器,并进入SYN_SENT状态,等待服务器的确认.SYN:同步序列编号(Synchronize Sequence ...
- ip端口协议,SSHj加密连接
SSH协议----一旦linux服务器开机,SSH协议就不断的侦听22端口,一旦有连接到达,就校验连接用户名和密码: 可以理解为连接ip端口为找到一栋大楼,发送用户名和密码为向大楼保安出示证件.SSH ...
随机推荐
- Leetcode ReorderList
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【HDU】1814 Peaceful Commission
http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选 ...
- 升级到EF6 两个注意事项
1.依据MSDN的官方描述: In previous versions of EF the code was split between core libraries (primarily Syste ...
- JavaScript放置位置区别
JavaScript放置位置区别 页面中的脚本会在页面载入浏览器后立即执行.我们并不总希望这样.有时,我们希望当页面载入时执行脚本,而另外的时候,我们则希望当用户触发事件时才执行脚本. 位于 head ...
- ECLIPSE下SVN的创建分支/合并/切换使用
最近接项目要求,要在svn主干上创建分支,用分支来进行程序的bug修改,而主干上进行新功能的开发.分支上的bug修改完,发布后,可以合并到主干上.项目程序可以在主干和分支之间进行切换,来实现主干和分支 ...
- silverlight Canvas、StackPanel、Grid三者之间的关系
学习 silverlight 首先Canvas.StackPanel.Grid 博客园里看到jailu的这篇文章整理得很好 贴下来: Silverlight提供了非常灵活的布局管理系统,让程序员和 ...
- 优化MySchool总结习题
1) 在SQL Server 中,为数据库表建立索引能够( C ). A. 防止非法的删除操作 B. 防止非法的插入操作 C. 提高查询性能 D. 节约数据库的磁盘空间 解析:索引的作用是通过使用索引 ...
- vim 基础命令
第一部份:一般指令模式可用的按鈕說明,游標移動.複製貼上.搜尋取代等 移動游標的方法 h 或 向左方向鍵(←) 游標向左移動一個字元 j 或 向下方 ...
- Daily Scrum 10.25
今天我们在分析数据库的时候发现还有一些这方面知识上的不足,花费一些额外的时间做功课.这说明当时我们的plan没有做好,但是我们基本还处在进度上. 下面是今天的Task统计:
- hdu Prime Ring Problem
简单的dfs,貌似这道题用暴力枚举就可以了,毕竟数据开的是比较小的. #include"iostream" #include"algorithm" #inclu ...