简体   繁体   English

限制maxsockets请求包

[英]limit maxsockets request package

I try to limit request package maxsockets , I use fiddler for testing how much it uses concurrent. 我尝试限制请求包maxsockets,我使用fiddler测试它使用并发多少。

As I see ,it doesnt apply the 10 limit that i try to set. 正如我所看到的,它不适用于我尝试设置的10限制。

i dont want request module use more than 10 concurrent 我不希望请求模块使用超过10个并发

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. 请注意,如果要在循环中发送多个请求并创建多个新池对象,则maxSockets将无法按预期工作。 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. 要解决此问题,请将request.defaults与池选项一起使用,或者使用循环外部的maxSockets属性创建池对象。

try to change the pool option of request.defaults or create a pool object and use it on all request calls. 尝试更改request.defaults的池选项或创建池对象并在所有请求调用上使用它。

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. 我注意到你已经使用了request.default,修复应该只是删除r.get调用中的pool选项。

from: r.get({url:'https://freegeoip.net/json/',pool: {maxSockets: 10}}, function optionalCallback(err, httpResponse, body) { 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) { to: r.get({url:'https://freegeoip.net/json/'}, function optionalCallback(err, httpResponse, body) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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