类:http.Agent


🌐 Class: http.Agent

Agent 负责管理 HTTP 客户端的连接持久性和重用。它维护一个针对特定主机和端口的待处理请求队列,为每个请求队列复用单个套接字连接,直到队列为空,此时套接字要么被销毁,要么被放入池中,以便再次用于对相同主机和端口的请求。套接字是被销毁还是放入池中取决于 keepAlive 选项

🌐 An Agent is responsible for managing connection persistence and reuse for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty, at which time the socket is either destroyed or put into a pool where it is kept to be used again for requests to the same host and port. Whether it is destroyed or pooled depends on the keepAlive option.

连接池中的连接已启用 TCP Keep-Alive,但服务器仍可能关闭空闲连接,在这种情况下,它们将被从连接池中移除,并且在对该主机和端口发起新的 HTTP 请求时会建立新的连接。服务器也可能拒绝在同一连接上处理多个请求,那么每个请求都必须重新建立连接,无法进行连接复用。Agent 仍然会向该服务器发起请求,但每个请求都将在新的连接上进行。

🌐 Pooled connections have TCP Keep-Alive enabled for them, but servers may still close idle connections, in which case they will be removed from the pool and a new connection will be made when a new HTTP request is made for that host and port. Servers may also refuse to allow multiple requests over the same connection, in which case the connection will have to be remade for every request and cannot be pooled. The Agent will still make the requests to that server, but each one will occur over a new connection.