简体   繁体   中英

How to manage node.js request connection pool?

I am using node.js Request module to make multiple post requests.

Does this module have a connection pool ?

Can we manage this connection pool ?

can we close open connections ?

How do we handle the socket hang up error

Request does not have a connection pool. However, the http module (which request uses) does:

In node 0.5.3+ there is a new implementation of the HTTP Agent which is used for pooling sockets used in HTTP client requests.

By default, there is a limit of 5 concurrent connections per host. There is an issue with the current agent implementation that causes hang up errors to occur when you try to open too many connections.

You can either:

  • increase the maximum number of connections: http.globalAgent.maxSockets .
  • disable the agent entirely: pass {pool: false} to request.

There are several reasons for having an HTTP agent in the first place:

  • it prevents you from accidentally opening thousands of connections to a host (would be perceived as an attack).
  • connections in the pool will be kept opened for HTTP 1.1 keepalive.
  • most of the time, maxSockets really depends on the host you're targetting. node.js will be perfectly happy to open 1000 concurrent connections if the other host handles it.

The behavior of the agent is explained in the node.js doc:

The current HTTP Agent also defaults client requests to using Connection:keep-alive. If no pending HTTP requests are waiting on a socket to become free the socket is closed. This means that node's pool has the benefit of keep-alive when under load but still does not require developers to manually close the HTTP clients using keep-alive.

The asynchronous architecture of node.js is what makes it very cheap to open new connections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM