什么是 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

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初始化或者 延迟。

------- 实验效果 ------

http://www.golaravel.com/

以此网页内容实验, 第一次加载后, 多次刷新页面

之前

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)实验的更多相关文章

  1. MQTT 协议学习:007-Keep Alive 连接保活 与 对应报文(PINGREQ、PINGRESP)

    背景 keep alive 是 CONNECT 报文中可变头的一部分. 我们提到过 Broker 需要知道 Client 是否非正常地断开了和它的连接,以发送遗愿消息.实际上 Client 也需要能够 ...

  2. (转)对Http协议的长连接和短连接新的认识

    转载来自:http://www.cnblogs.com/zuoxiaolong/p/life49.html一直对长连接短连接模模糊糊,看着该博主的文章,豁然开朗~ 引言 最近刚到公司不到一个月,正处于 ...

  3. http协议及长连接和短连接

    1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议. IP协议主要解决网络路由和寻址 ...

  4. MQTT协议笔记之连接和心跳

    前言 本篇会把连接(CONNECT).心跳(PINGREQ/PINGRESP).确认(CONNACK).断开连接(DISCONNECT)和在一起. CONNECT 像前面所说,MQTT有关字符串部分采 ...

  5. Websocket协议之握手连接

    Websocket协议是为了解决web即时应用中服务器与客户端浏览器全双工通信的问题而设计的,是完全意义上的Web应用端的双向通信技术,可以取代之前使用半双工HTTP协议而模拟全双工通信,同时克服了带 ...

  6. OSI七层协议与TCP连接

    概述 为了追求效率,我们写代码,不可能去关注底层知识,但往往到出了问题,或者性能调优.我们就会速手无策,仔细为自己查缺补漏,总结知识点. 网络协议 互联网的本质就是一系列的网络协议,让不同计算机能够互 ...

  7. TCP连接三次握手协议,释放连接四次挥手,以及使用 awl伪造mac地址进行多线程syn洪泛攻击。

    这个TCP连接就是一次追女生-谈恋爱-分手,追求比分手简单,但是分手比追求复杂.哥,谈了半年的女朋友,在就快要成功了的时候分了,原因是因为有人在后面该老子背后搞SYN洪泛攻击,最后女朋友丢失了.学会T ...

  8. TCP/IP协议的建立连接与关闭连接过程

    一.建立连接(三次握手) 第一次握手:建立连接时,客户端发送SYN(seq=x)包到服务器,并进入SYN_SENT状态,等待服务器的确认.SYN:同步序列编号(Synchronize Sequence ...

  9. ip端口协议,SSHj加密连接

    SSH协议----一旦linux服务器开机,SSH协议就不断的侦听22端口,一旦有连接到达,就校验连接用户名和密码: 可以理解为连接ip端口为找到一栋大楼,发送用户名和密码为向大楼保安出示证件.SSH ...

随机推荐

  1. topcoder SRM 610 DIV2 DivideByZero

    题目的意思是给你一组数,然后不断的进行除法(注意是大数除以小数),然后将得到的结果加入这组数种然后继续进行除法, 直到没有新添加的数为止 此题按照提议模拟即可 注意要保持元素的不同 int Count ...

  2. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...

  3. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误

    解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...

  4. MYSQL加锁的测验

     存储引擎  支持的锁定级别 myisam      表级别 memory     表级别   inndb       行级别  bdb:        页级别 lock锁定类型 锁定方式       ...

  5. 李洪强iOS经典面试题上

    李洪强iOS经典面试题上     1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...

  6. 重写(Override)与重载(Overload)

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...

  7. Oracle中建表和指定表空间

    --建一个表create table HH2( tid number primary key ,--主键设定 tname varchar2(20) ); --删除表drop table HH; --表 ...

  8. ajax测试Demo以及json简单的转化

    Ajax是局部刷新,并不影响页面其他的操作 实例1:本测试是演示利用Ajax在一个页面播放视频,点击赞和踩按钮,视频不会受影响, 新建一个ajaxTest.html页面 <!DOCTYPE ht ...

  9. form表单提交controller层接收到的值为乱码的问题

    今天遇到个中文乱码问题,大体情况是这样的:前台有一个form表单,其中有几个input的控件,值是带中文的,form表单只设置了id='form1' method='post'  action='xx ...

  10. 5_STL设计理念_迭代器

    他山之石,可以攻玉. http://blog.csdn.net/jxh_123/article/details/30793397?utm_source=tuicool&utm_medium=r ...