What is a keep-alive connection
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 connect Ion 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 MU Ltiplexed over a single connection.
Https://www.byvoid.com/blog/http-keep-alive-header
We know that the HTTP protocol uses "request-answer" mode, when using normal mode, that is, non-keepalive mode, each request/reply client and server to create a new connection, immediately after the completion of the connection (HTTP protocol is a non-connected protocol) When using Keep-alive mode (also known as persistent connection, connection reuse), the Keep-alive feature keeps the client-to-server connection active, and the keep-alive feature avoids establishing or re-establishing a connection when a subsequent request to the server occurs.
HTTP 1.0 is turned off by default, you need to add "connection:keep-alive" in the HTTP header to enable Keep-alive;http 1.1 by default enabling Keep-alive, if you join "Connection:close", Before closing. Most browsers now use the http1.1 protocol, which means that the Keep-alive connection request is initiated by default, so whether a full keep-alive connection can be completed depends on the server setup.
Advantages of keep-alive connections
https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.1
Persistent HTTP Connections has a number of advantages:
-By opening and closing fewer TCP connections, CPU time was saved in routers and hosts (clients, servers, proxies, GAT Eways, tunnels, or caches), and memory used for TCP protocol control blocks can is saved in hosts.
-HTTP requests and responses can is pipelined on a connection. Pipelining allows a client to make multiple requests without waiting for each response, allowing a single TCP connecti On to is 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 are no time spent in TCP ' s connection opening.
-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 s Erver, retry with the old semantics after a 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 is 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 is even more important for secure HTTPS connections, because establishing a secure connection needs much More CPU time and network round-trips.
1, lower CPU and memory consumption.
2. Enable the HTTP Pipe line feature
3. Reduce network congestion
4, reduce the delay of subsequent requests
5, encountered an error, you can report an error, do not forcibly shut down.
The effect is significant for HTTPS connections because the HTTPS handshake process is time-consuming!
Disadvantages of the Keep-alive connection
Https://en.wikipedia.org/wiki/HTTP_persistent_connection
Disadvantages
If the client does not close the connection then all of the data it needs have been received, the resources needed to keep The connection open on the server would be unavailable for other clients. How much this affects the server's availability and how long the resources is unavailable depend on the server ' s Architec Ture and configuration.
Browser support
Use in Web browsers
All modern web browsers use persistent connections, including Google Chrome, Firefox, Internet Explorer (since 4.01), Oper A (since 4.0) [8] and Safari.
By default, Internet Explorer versions 6 and 7 use the persistent connections while version 8 uses six. [9] Persistent connections time out after $ seconds of inactivity which is changeable via the Windows Registry. [10]
In Firefox, the number of simultaneous connections can is customized (Per-server, Per-proxy, total). Persistent connections time out after a seconds (1.92 minutes) of inactivity which is changeable via the configuration.[ 11]
THTTPD Experimental realization of keep-alive
Https://github.com/fanqingsong/thttpd_keepalive
Points:
1. Connection close in response message header modified to Keep-alive
2, Httpd_get_conn in the memory initialization of HC and set the initial value of the code to separate out the independent function
Httpd_con_initialize_content
Httpd_con_initialize_mem
3, Handle_linger transformation, no longer perform delay shutdown, modify the delay timer, and initialize the HC
4, Clear_connection, shutdown comment out, C->linger_timer initialization or delay.
-------Experimental results------
http://www.golaravel.com/
This page content experiment, the first time after loading, refresh the page multiple times
Before
1.22 1.46 1.68) 1.2 1.03
After
1.21 1.37 1.16) 1.43 1.25
The improvement is not very significant, should not be related to the concurrency of the browser, and thttpd for the static resources of the single-threaded mechanism, is already serial.
And this experiment does not require DNS resolution, and HTTPS handshake.
The corresponding code prints, a connection fd, which proves keep-alive effective:
Https://github.com/fanqingsong/thttpd_keepalive/blob/master/thttpd-2.27/logfile_fqs
Server Segment Capture Package:
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 protocol keep-alive connection with BS (FIREFOX-THTTPD) experiment