Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 652
Anand Sunderraman Avatar asked Jun 28 '13 23:06

Anand Sunderraman


1 Answers

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.

like image 124
Laurent Perrin Avatar answered Nov 20 '22 01:11

Laurent Perrin