zoukankan      html  css  js  c++  java
  • 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

    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

    image

    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

  • 相关阅读:
    C# winform窗体间传值(使用委托或事件)
    C# ListView用法详解
    C# ListView列表包含添加和删除,自动排序
    C#跨窗体传值的几种方法分析(很详细)
    c#listview控件的数据添加和常用事件的处理
    C#中结构体与字节流互相转换 [StructLayout(LayoutKind.Sequential)]
    C# winform 操作access常用类
    发行自己的区块链加密货币
    以太坊自助发币
    supervisor常用命令
  • 原文地址:https://www.cnblogs.com/lightsong/p/5540681.html
Copyright © 2011-2022 走看看