有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    HTTP是基于TCP的,它管理以正确的顺序发送和接收包,并在包中途丢失时请求重新传输。TCP连接是通过由SYNSYN-ACKACK消息组成的TCP握手开始的,而它是通过FINACK-FINACK序列结束的,这可以从Wikipediaenter image description here拍摄的图像中看到

    虽然HTTP是一种请求-响应协议,但打开和关闭连接的成本相当高,因此HTTP/1.1允许重用现有连接。使用头Connection: keep-alive,即告诉客户机(即浏览器)保持与服务器的连接打开。一台服务器可以同时打开成千上万个连接。为了避免耗尽服务器的资源,通常会及时限制连接。通过套接字超时空闲连接或存在某些连接问题的连接(断开的互联网访问…)在一段预定义的时间后,服务器会自动关闭

    很多HTTP实现,如Apaches HTTP client 4.4及更高版本,只在连接即将使用时才检查连接的状态

    The handling of stale connections was changed in version 4.4. Previously, the code would check every connection by default before re-using it. The code now only checks the connection if the elapsed time since the last use of the connection exceeds the timeout that has been set. The default timeout is set to 2000ms (Source)

    因此,如果一个连接可能有一段时间没有被使用,那么客户端可能没有从服务器读取ACK-FIN,因此当服务器在一段时间前实际上已经关闭连接时,仍然认为该连接是打开的。这种连接已过期,通常称为半关闭。因此,可以由池收集

    请注意,如果发送包含Connection: closeHTTP头的请求,则应在客户端收到响应后立即关闭连接

    打开连接的状态可以通过netstat检查,这应该出现在大多数现代操作系统上。我最近不得不检查我们的一个HTTP客户机,该客户机是通过第三方库管理的,该库没有正确传播Connection: Close头,因此导致大量半封闭连接

  2. # 2 楼答案

    根据:https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/connmgmt.html#d5e418

    HttpClient tries to mitigate the problem by testing whether the connection is 'stale', that is no longer valid because it was closed on the server side, prior to using the connection for executing an HTTP request. The stale connection check is not 100% reliable. The only feasible solution that does not involve a one thread per socket model for idle connections is a dedicated monitor thread used to evict connections that are considered expired due to a long period of inactivity. The monitor thread can periodically call ClientConnectionManager#closeExpiredConnections() method to close all expired connections and evict closed connections from the pool. It can also optionally call ClientConnectionManager#closeIdleConnections() method to close all connections that have been idle over a given period of time.

    过期和空闲之间的区别在于,过期连接在服务器端已关闭,而空闲连接在服务器端不一定关闭,但在一段时间内处于空闲状态。当连接关闭时,它在要使用的池中再次可用