【KeepAlive】Http--Keep-Alive及Tcp--Keepalive

Keep-Alive mode:

Http protocol uses the "request - response" mode, when a normal mode, i.e. when a non Keep-Alive mode, each request / reply, the client and server must create a new connection is disconnected immediately after the completion; when used Keep- when Alive mode, Keep-Alive feature allows the client to server connection duration effective, when the subsequent request to the server occurs, Keep-Alive feature prevents the establishment or re-establish the connection.
http1.0 in is off by default, need to join in the http header "Connection: Keep-Alive", Alive-to enable the Keep;
http 1.1 is enabled by default Keep-Alive, If you add "Connection: close" was closed. At present, most browsers are used http1.1 agreement, that default will initiate a connection Keep-Alive requests, so if you can complete a full Keep- Alive connection depends on server settings case. FIG request is a comparison of the normal mode and the long connection mode:

 


The advantages and disadvantages of open Keep-Alive:

Advantage: Keep-Alive mode is more efficient, because avoiding the overhead of connection establishment and release.
Drawback: Tcp connection for a long time easily lead to invalid system resources consumption, waste of system resources.

When maintain a long connection, how to judge a request has been completed?

Content-Length 

Content-Length indicates the length of the substantial contents. With this field the browser to determine whether the current requested data have all been received.
So, when a browser request is a static resource, that the server can know exactly when the return length of the content, Content-Length can be set to control the end of the request. But when the server does not know the result of the length of the request, such as a page or dynamic data, Content-Length can not solve the above problem, this time need to use Transfer-Encoding field.

Transfer-Encoding 

Transfer-Encoding refers to the transmission coding, the above problem, when the server can not know the length of the content of an entity, it can specify Transfer-Encoding: chunked to inform the current code browser data into a one pass. Of course, can also specify Transfer-Encoding: gzip, chunked entity indicates that the content is not only gzip compression, or block transfer. Finally, when the browser receives a length chunked 0, knowing that the current request have been received content.

Keep-Alive timeout:

Httpd daemon, generally provide a keep-alive timeout time setting parameters. For example, the nginx keepalive_timeout, and Apache KeepAliveTimeout. This keepalive_timout time value means: tcp connection http generated after a transfer after the last one response, but also hold live keepalive_timeout seconds later, I began to close the connection.
When the httpd daemon after sending a response, should immediately take the initiative to close the corresponding tcp connection, after setting keepalive_timeout, httpd daemon will say: "wait, and see there is no browser requests come," and this and other , is keepalive_timeout time. If the daemon in the waiting time, never received the browser sent me http request, then close the http connection.

Tcp的Keepalive:

After the connection is established, if the client has not sent data, or across a very long time before sending the data to determine how the other side is also connected to a long line when there is no data packet transmission, in the end is dropped, or indeed no data transmission connection need not need to keep this situation in the TCP protocol design is the need to take into account.
TCP protocol to solve this problem through an ingenious way, when over a period of time, TCP automatically sends a data packet is empty (detection package) to each other, if the other party to respond to this message, the other party is also online, connection can remain, if the other party did not return messages, and retry several times after the link is considered lost, there is no need to stay connected.

tcp keep-alive mechanism is fresh TCP connection status detecting the TCP. tcp keep-alive timer preservation, supports three system kernel configuration parameters:

net.ipv4.tcp_keepalive_intvl = 15 
net.ipv4.tcp_keepalive_probes = 5 
net.ipv4.tcp_keepalive_time = 1800 

 

TCP keepalive is fresh timer, when both ends of the network established after the TCP connection is idle (the two sides do not have any data stream sent intercourse) the tcp_keepalive_time, the server will detect attempts to send packets to the client, to determine the TCP connection status (there the client may crash, force close the application, host unreachable, etc.). If no other answer (ack packets) will try again after tcp_keepalive_intvl send packets to detect until it receives each other's ack, if the other party has not received the ack, altogether attempts tcp_keepalive_probes times, each time interval time here are 15s, 30s, 45s, 60s, 75s. If you try to tcp_keepalive_probes, still does not receive the ack packet, it will drop the TCP connection. The default TCP connection idle time is 2 hours, usually 30 minutes is enough to set.


Transfer: https: //blog.csdn.net/weixin_37672169/article/details/80283935

Guess you like

Origin www.cnblogs.com/itplay/p/11371633.html