current position：Home>HTTP keep alive details
HTTP keep alive details
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
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 ：https://blog.csdn.net/xiaoduanayu/article/details/78386508
author[baoleilei6],Please bring the original link to reprint, thank you.
The sidebar is recommended
- How to realize the function of jQuery small ticket printing
- HTTP protocol
- I've taught myself Python for half a month. I feel it's useless. What should I do?
- Is it a blessing or a curse that the biggest customer of Ningde era changes ownership of Tesla?
- Element UI realizes the function of table radio selection
- Elementui realizes the function of table radio selection
guess what you like
Apache shardingsphere code formatting practice -- spotless
Vue console error information collection and reporting
Initialize the react project environment under mac
React dynamically adds elements to the list
React realizes form data collection
Vernacular react life cycle
vue2. How to register and login with 0 + koa2 + mongodb
vue. JS how to realize single page application
How to realize login registration and token verification in Vue
- How to set the background color for individual pages in Vue cli
- How does Vue router cooperate with elementui to realize navigation
- Vue2 how to load more data on the scroll bar
- How to use margin and padding in CSS
- How does vue2x implement the vueslidershow function of the responsive adaptive rotation component plug-in
- How does vue2 recursive component implement tree menu
- How to use the three selectors of CSS
- Same blog, different styles -- hexo alternative construction
- Optimize nginx HTTPS latency - see how I can speed up nginx by 30%?
- The development model in the era of consumer Internet only transfers industrial elements from offline to online
- Advanced features of Vue
- CSS shadow advanced, to achieve a more three-dimensional shadow effect!
- Differences between nginx forward and reverse proxy
- Front end schema form configuration generation scheme
- Units that can be used by the rotate method of transform in CSS
- Why CSS3 sticky doesn't work?
- [bug resolution] webpack error: module not found Did you mean xxx
- [bug resolution] can't perform a react state update on an unmounted component This is ＞ a no-op, but it...
- Remember to speed up the construction of a webpack
- Browser review hover element
- Vue insert iframe
- Vue realizes downloading pictures
- Some common problem-solving methods in Vue (sort out some truly effective methods that can be used in the project) (wait for continuous update and accumulation)
- About database data response to HTML page, Chinese appears? Solution to garbled code (multiple question marks)
- Ask a question about CSS selector
- When Vue jumps the route, the timer cannot be cleared in time
- Document is not defined
- Vue quill editor stepping on pit record -- the echo style of rich text content is wrong
- The multi selection box of table in element is combined with paging to echo the bug step hole
- Vue project optimizes the loading speed of the first screen
- Use / deep / to report an error CSS (CSS selected expected)
- Nginx configuration
- Vue cli reverse proxy
- Vuex persistedstate plug-in - vuex persistent storage
- Understanding and summary of CSS
- vue2. Implementation of X data response
- Props in react