Thursday, 29 October 2020

TCP Keep Alive, Connection Timeout (Client side) and Request Timeout (Server Side) explanation && fixes

 TCP keep alive

There is overhead in establishing a new TCP connection (DNS lookups, TCP handshake, SSL/TLS handshake, etc). Without a keep-alive, every HTTP request has to establish a new TCP connection, and then close the connection once the response has been sent/received. A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding all of that overhead. That is what makes the connection "persistent".


Close TCP  Keep alive


In HTTP 0.9 and 1.0, by default the server closes its end of a TCP connection after sending a response to a client. The client must close its end of the TCP connection after receiving the response. In HTTP 1.0 (but not in 0.9), a client can explicitly ask the server not to close its end of the connection by including a Connection: keep-alive header in the request. If the server agrees, it includes a Connection: keep-alive header in the response, and does not close its end of the connection. The client may then re-use the same TCP connection to send its next request.


In HTTP 1.1, keep-alive is the default behavior, unless the client explicitly asks the server to close the connection by including a Connection: close header in its request, or the server decides to includes a Connection: close header in its response.


https://stackoverflow.com/questions/20763999/explain-http-keep-alive-mechanism


---------------------------------------------------------------------------------------------------------------------------------


https://serverfault.com/questions/735515/tcp-timeout-for-established-connections-in-windows


By default an established TCP connection does not time out (but may do so because of hardware restrictions).

An application can use the TCP keepalive mechanism to check for broken connections. In Firebird (>1.5) TCP keepalives are enabled.

The keepalive "timeout" has to be set on the server. The default TCP keepalive time interval in Windows is 2 hours (but recommended are 5 minutes). It can be set in the HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\KeepAliveTime registry key.

More details from above links:


-----------------------------------------------------------------------------------------------

Timeouts 


Connection timeout - is a time period within which a connection between a client and a server must be established. Suppose that you navigate your browser (client) to some website (server). What happens is that your browser starts to listen for a response message from that server but this response may never arrive for various reasons (e.g. server is offline). So if there is still no response from the server after X seconds, your browser will 'give up' on waiting, otherwise it might get stuck waiting for eternity.


(connection timeout is different via browser :https://superuser.com/questions/1012176/chrome-standard-timeout)



Request timeout - as in the previous case where client wasn't willing to wait for response from server for too long, server is not willing to keep unused connection alive for too long either. Once the connection between server and client has been established, client must periodically inform server that it is still there by sending information to that server. If client fails to send any information to server in a specified time, server simply drops this connection as it thinks that client is no longer there to communicate with it (why wasting resources meaninglessly).


(request time out is different via server appliation: I.E apache server has different request timeout than Nginx, and there is also script exectuion time I.E PHP vs python)



Time to live (TTL) - is a value specified inside of a packet that is set when the packet is created (usually to 255) that tells how long the packet can be left alive in a network. As this packet goes through the network, it arrives at routers that sit on the path between the packet's origin and its destination. Each time the router resends the packet, it also decrements its TTL value by 1 and if that value drops to 0, instead of resending the packet, router simply drops it as the packet is not supposed to live any longer. This mechanism is used to prevent network from flooding by data as each packet can live inside of it for only limited amount of 'time'.


https://stackoverflow.com/questions/49704708/what-is-a-connection-timeout-during-a-http-request


------------------------------------------------------------------------------------------------------------------------------



Connection time out(Client side) can not be adjusted as most browser like CHROME prevent user adjusting it.


Request time out fixes

Solution 1(DONE through location header with new url)

If you know that it will take a long time to create the resource tell it immediately to the client. Don't block him for minutes.

A common way is to answer with the status code 202 (Accepted). Add a Location header with a URI that points to a second resource the client can poll to get more information about the current status.

This second resource should answer with a 200 (OK) and the current status ("still pending, please try again in 30 seconds"). If the creation of the first resource is finished the second one should answer with a 303 (See Other) and the URI of the first resource in the Location header.


https://stackoverflow.com/questions/24221660/how-to-avoid-request-time-out-when-creating-resources-with-rest


Solution 2:

Increase script execution time, and server application timeout (for example php and apache):

Based on CGI/Server side engine increase timeout there

PHP : http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time - default is 30 seconds

In php.ini:

max_execution_time 60

Increase apache timeout - default is 300 (in version 2.4 it is 60).

In your httpd.conf (in server config or vhost config)

TimeOut 600

Note that first setting allows your PHP script to run longer, it will not interferre with network timeout.

Second setting modify maximum amount of time the server will wait for certain events before failing a request

Sorry, I'm not sure if you are using PHP as server side processing, but if you provide more info I will be more accurate.


https://stackoverflow.com/questions/9629566/how-to-increase-apache-timeout-directive-in-htaccess






No comments:

Post a Comment