node.js - 限制maxsockets请求包

标签 node.js request

我尝试限制请求包 maxsockets , 我使用 fiddler 来测试它使用并发的程度。

据我所知,它不适用我尝试设置的 10 个限制。

我不希望请求模块使用超过 10 个并发

我可能做错了什么?

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

最佳答案

如文档中所述:

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.

尝试更改 request.defaults 的池选项或创建一个池对象并在所有请求调用上使用它。

编辑:

我注意到您已经使用了 request.default,修复方法应该是简单地删除 r.get 调用中的池选项。

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

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

关于node.js - 限制maxsockets请求包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098094/

相关文章:

node.js - 将密码哈希脚本从 GO 转换为 Nodejs

javascript - 在mysql nodejs中插入对象数组

wcf - 如何拦截 WCF 中反序列化后的请求对象

ios - Alamofire 在接收所有数据之前验证 header (完整主体)

Python - 如何在第一次请求时出现 SSL 错误(URL 超出最大重试次数)?

node.js - Cloud Functions for Firebase 中的本地依赖项

node.js - botframework v4 主动消息不通过 ActivityHandler 事件处理程序

node.js - Loopback 3中刷新AccessToken

node.js - createWriteStream 与 writeFile?

javascript - 从字符串中删除字符