Search code examples
javascriptnode.jssocketsconnection-pooling

node.js what happens when maxSockets are reached?


I was wondering what happens when maxSockets is reached?

If I'm using a service that allows me to create a custom http.Agent and I specify maxSockets to 1. What happens when I try to issues multiple concurrent requests?

Will each request beyond the initial block until the initial is complete?? Then the socket would become available to the next request? and all others block? I would certainly assume so, but was not able to find anything in the docs specifically, and am brand new to socket programming.


Solution

  • After maxSockets are in use, additional requests get queued until an active request completes and a socket is freed up to be used, at which point a pending request from the queue would be sent on the newly-freed socket.

    Note this is technically queueing not blocking - the CPU and event loop continue to do useful work during this process.

    Note also there is a maxSockets and queue on a per-origin ("host:port") basis (similar to browsers).

    Also note that the default maxSockets is Infinity.