I am creating an application that will make about a million calls on a remote api server. Will I be able to limit the number of connections, for example, 10? Can I set maximum sockets to 10?
I am trying to understand what these options do:
keepAlive: false, maxSockets: 999, maxFreeSockets: 1
In node http get function, in the following code:
var inputData = []; for(i=1; i<=5000;i++){ inputData.push('number' + i); } var options = { host: "localhost", port: 80, path: "/text.txt", keepAlive: false, maxSockets: 999, maxFreeSockets: 1 } var limit = inputData.length; var counter = 0; function fetchData(number){ return new Promise(function(resolve, reject){ var http = require('http'); fetch = function(resp){ var body = ''; resp.on('data',function(chunk){ body += chunk; }) resp.on('end',function(){ console.log(resp) resolve() }) resp.on('error',function(err){ console.log('error'); }) } var req = http.request(options, fetch); req.end(); }) } Promise.all(inputData.map(number => fetchData(number))).then(function(results) { console.log('finished'); connection.end(); }) .catch(function(error) { console.log('there wa an error'); console.log(error); });
source share