简体   繁体   中英

limit maxsockets request package

I try to limit request package maxsockets , I use fiddler for testing how much it uses concurrent.

As I see ,it doesnt apply the 10 limit that i try to set.

i dont want request module use more than 10 concurrent

what may i be doing wrong?

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var loglar='idler2.txt';
var url = require('url');
var util = require('util');
var http = require('http');
var https = require('https');
var request = require('request');
var fs = require('fs');
var linkler = [];
var starttime = Math.round(new Date().getTime() / 1000);


http.globalAgent.maxSockets = 10;
https.globalAgent.maxSockets = 10;


var timeoutsure = 30 * 1000;
var success=0,fail=0;

process.on('exit', onExit);



function onExit() {
    console.log('\n%d secs,\n%d suc ,\n%d fail\n---------------------\n', 
    (Math.round(new Date().getTime() / 1000)) - starttime,success,fail);
}

function logla(data)
{
    var fd = fs.openSync(loglar, 'a+');
    fs.writeSync(fd, data);
    fs.closeSync(fd);
}

for(i=0;i<1000;i++)
{
    sorgutest();
}

var r = request.defaults({'proxy':'http://127.0.0.1:8888',pool: {maxSockets: 10}});

function sorgutest()
{

r.get({url:'https://freegeoip.net/json/',pool: {maxSockets: 10}}, function optionalCallback(err, httpResponse, body) {
  if (err) {
      fail++;
    return console.error('failed: 49', err);
  }
  else {
  try {bodyjson=JSON.parse(body);
  logla(body);
  success++;
  }
  catch(e){console.log("hamina 54");}
  }
});
}

fiddler nodejs test

As stated in the documentation:

Note that if you are sending multiple requests in a loop and creating multiple new pool objects, maxSockets will not work as intended. To work around this, either use request.defaults with your pool options or create the pool object with the maxSockets property outside of the loop.

try to change the pool option of request.defaults or create a pool object and use it on all request calls.

Edit:

I've noticed that you already used request.default, the fix should be to simply remove the pool option in the r.get call.

from: r.get({url:'https://freegeoip.net/json/',pool: {maxSockets: 10}}, function optionalCallback(err, httpResponse, body) {

to: r.get({url:'https://freegeoip.net/json/'}, function optionalCallback(err, httpResponse, body) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM