What is the best way to limit concurrency when using ES6's Promise.all()?

前端 未结 17 750
执念已碎
执念已碎 2020-11-29 21:28

I have some code that is iterating over a list that was queried out of a database and making an HTTP request for each element in that list. That list can sometimes be a rea

17条回答
  •  离开以前
    2020-11-29 22:06

    Instead of using promises for limiting http requests, use node's built-in http.Agent.maxSockets. This removes the requirement of using a library or writing your own pooling code, and has the added advantage more control over what you're limiting.

    agent.maxSockets

    By default set to Infinity. Determines how many concurrent sockets the agent can have open per origin. Origin is either a 'host:port' or 'host:port:localAddress' combination.

    For example:

    var http = require('http');
    var agent = new http.Agent({maxSockets: 5}); // 5 concurrent connections per origin
    var request = http.request({..., agent: agent}, ...);
    

    If making multiple requests to the same origin, it might also benefit you to set keepAlive to true (see docs above for more info).

提交回复
热议问题