How to manage node.js connection pool?

I am using the node.js Request module to create multiple posts.

Does this module have a connection pool?

Can we manage this connection pool?

can I close open connections?

How do we handle socket hang up error

+4
source share
1 answer

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

Node 0.5.3+ introduced a new implementation of the HTTP agent, which is used to combine the sockets used in HTTP client requests.

By default, there is a limit of 5 concurrent connections per host. There is a problem with the current agent implementation, which causes errors to hang when trying to open too many connections.

You can:

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

First of all, there are several reasons for having an HTTP agent:

  • it prevents the accidental opening of thousands of host connections (perceived as an attack).
  • Connections in the pool will be kept open for the HTTP 1.1 keepalive protocol.
  • in most cases, maxSockets really depends on which destination server you are on. node.js will be happy to open 1000 concurrent connections if another host processes it.

Agent behavior is explained in the node.js document:

The current HTTP agent also uses client requests by default to use Connection: keep-alive. If pending HTTP requests do not wait for the socket to free, the socket is closed. This means that the node pool has the advantage of maintaining uptime while loading, but it does not require developers to manually close HTTP clients using keep-alive.

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

+14
source

Source: https://habr.com/ru/post/1488792/


All Articles