current position:Home>HTTP keep alive details

HTTP keep alive details

2022-04-29 04:25:26baoleilei6

1. Why would there be Connection: keep-alive?
Early HTTP/1.0 in , Every time http Request to create a connection , The process of creating a connection consumes resources and time , In order to reduce the consumption of resources , Reduced response time , You need to reuse connections . Later HTTP/1.0 And HTTP/1.1 in , Introduced the mechanism of reusing connection , Is in the http Add... To the request header Connection: keep-alive To tell the other party not to close the request after the response is completed , Next time we will continue to communicate with this request . Provisions of the agreement HTTP/1.0 If you want to keep a long connection , You need to add... To the request header Connection: keep-alive, and HTTP/1.1 Long connection is supported by default , It's OK to have this request header or not .

Yes, of course , The agreement stipulates that , As for support, it depends on the server ( such as tomcat) And the client ( For example, browser ) The concrete realization of . In practice, I found that Google browser uses HTTP/1.1 When signing an agreement, the request header always carries Connection: keep-alive, In addition, through httpclient Use HTTP/1.0 Agreement to request tomcat when , Even with Connection: keep-alive The request header can't keep a long connection . If HTTP/1.1 Version of http The request message does not want to use a long connection , Add... To the request header Connection: close, The peer service that receives the request header will actively close the connection .

however http Will the long connection be maintained ? Definitely not . Generally, the server will set keep-alive Timeout time . The specified time interval has been exceeded , The server will actively close the connection . At the same time, the server will also set a parameter called the maximum number of requests , For example, when the maximum number of requests is 300 when , As long as the number of requests exceeds 300 Time , Even before the timeout , The server will also actively close the connection .

2.Transfer-Encoding and Content-Length
talk about http A long connection , Can't get around these two requests / Response head . among Transfer-Encoding It is not recommended to use... In the request header , Because it is impossible to know whether the server can parse the request header , Instead, you should use... In the response header , Because the client browser can parse the response header .Content-Length When the request method is GET You can't use , When the request method is POST It needs to be used , It also often appears in the response header . For ease of description , The following only describes the occurrence of these two attributes in the response header .

It's easy to make long connections , As long as the client and server keep this http Long connection is enough . But the key to the problem is to keep the long connection , How does the browser know that the server has responded ? When using short connections , When the server finishes responding, it shuts down http Connect , In this way, the browser can know that all the responses have been received , Also close the connection (TCP The connection is two-way ). When using long connection , After the response is completed, the server cannot close the connection , Then it needs to add a special flag in the response header to tell the browser that the response has been completed .

In general, this special sign is Content-Length, To indicate the data size of the response body , such as Content-Length: 120 Indicates that the content of the response body has 120 Bytes , So the browser receives 120 After the response body of bytes, you will know that the response has been completed .

because Content-Length The field must truly reflect the length of the response body , But in practice , Sometimes the response length is not so good , For example, the response body comes from a network file , Or generated by dynamic language . At this time, we need to get the exact length , You can only open a large enough memory space first , Wait for the content to be generated before calculating . On the one hand, it needs more memory overhead , On the other hand, it will make clients wait longer . Now Transfer-Encoding: chunked The response head comes in handy , The response header indicates that the content of the response body is transmitted in blocks , At this time, the server can respond to the browser block by block instead of responding all at once , After the browser receives all blocks, it indicates that the response is over .

Transmit a piece of text in chunks :“ People's life is always in the pursuit of freedom So easy” To illustrate the process of block transmission , As shown in the figure below

The first line of each block in the figure is the size of the block content , Hexadecimal representation , Followed by CRLF(\r\n), The first line itself and at the end of the block content CRLF Not included in size . The second line is block content , Follow me, too CRLF. Although the size of the last block is zero , But absolutely , Indicates the end of the block , Follow me, too CRLF, At the same time, the content is empty . Last , The response body is represented by CRLF end . The response content that combines them is :

HTTP/1.1 200 OK 
Content-Type: text/plain;charset=utf-8
Connection: keep-alive
Transfer-Encoding: chunked

One's life is always in pursuit of freedom \r\n
My life So easy\r\n

The above debugging tools do not contain the format of the browser , The browser's own debugging tool displays the response body of block transmission and non block transmission in the same way , To see the difference , Need to use Wireshark、Fiddler Wait for the bag grabbing tool to check .

3.HTTP keep-alive and TCP keepalive The difference between
TCP keepalive refer to TCP Life timers (keepalive timer). Imagine such a situation : The client has established the server on their own initiative TCP Connect . But then the host of the client suddenly failed . obviously , The server can no longer receive the data sent by the customer . therefore , There should be measures to prevent the server from waiting for nothing . This is using the keep alive timer . Every time the server receives the customer's data , Just reset the life timer , The time setting is usually two hours . If you don't receive the customer's data in two hours , The server sends a detection message segment , Later, every 75 Seconds to send . If you send it in a row 10 There is still no response from the customer after detecting message segments , The server thinks the client is down , Then close the connection .

​ —— From Xie Xiren 《 computer network 》
Copyright notice : This paper is about CSDN Blogger 「 A Yu 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :

copyright notice
author[baoleilei6],Please bring the original link to reprint, thank you.

Random recommended