简体   繁体   English

Nodejs + mikeal / Request模块,如何关闭请求或增加MaxSockets

[英]Nodejs + mikeal/Request module, how to close request or increase MaxSockets

I have a Nodejs app that's designed to perform simple end-to-end testing of a large web application. 我有一个Nodejs应用程序,旨在执行大型Web应用程序的简单端到端测试。 This app uses the mikeal/Request and Cheerio modules to navigate, request, traverse and inspect web pages across the application. 该应用程序使用mikeal /请求Cheerio模块导航,请求,遍历和检查整个应用程序的网页。

We are refactoring some tests, and are hitting a problem when multiple request functions are called in series. 我们正在重构一些测试,并且在串行调用多个request函数时遇到问题。 I believe this may be due to the Node.js process hitting the MaxSockets limit, but am not entirely sure. 我相信这可能是由于Node.js进程达到MaxSockets限制,但我不完全确定。

Some code... 一些代码......

var request = require('request');
var cheerio = require('cheerio);
var async = require('async');

var getPages_FromMenuLinks = function() {
    var pageUrl = 'http://www.example.com/app';
    async.waterfall([
        function topPageRequest(cb1) {
            var menuLinks = [];
            request(pageUrl, function(err, resp, page) {
                var $ = cheerio.load(page);
                $('div[class*="sub-menu"]').each(function (i, elem) {
                    menuLinks.push($(this).find('a').attr('href');
                });
                cb1(null, menuLinks);
            });
        }, function subMenuRequests(menuLinks, cb2) {
            async.eachSeries(menuLinks, functionv(link, callback) {
                request(link, function(err, resp, page) {
                    var $ = cheerio.load(page);
                    // do some quick validation testing of elements on the expected page
                    callback();
                });
            }, function() { cb2(null) } );
        }
    ], function () { });
};
module.export = getPages_FromMenuLinks;

Now, if I run this Node script, it runs through the first topPageRequest and starts the subMenuRequests , but then freezes after completing the request for the third sub-menu item. 现在,如果我运行此Node脚本,它将运行第一个topPageRequest并启动subMenuRequests ,但在完成第三个子菜单项的请求后冻结。

It seems that I might be hitting a Max-Sockets limit, either on Node or my machine (?) -- I'm testing this on standard Windows 8 machine, running Node v0.10.26. 似乎我可能在Node或我的机器上达到Max-Sockets限制(?) - 我在标准的Windows 8机器上测试它,运行Node v0.10.26。

I've tried using request({pool:{maxSockets:25}, url:link}, function(err, resp... , but it does not seem to make any difference. 我尝试过使用request({pool:{maxSockets:25}, url:link}, function(err, resp... ,但似乎没有任何区别)。

It also seems there's a way to abort the request object, if I first instantiate it (as found here ). 如果我首先实例化它(如此处所示 ),似乎还有一种方法可以中止请求对象。 But I have no idea how I would "parse" the page , similar to what's happening in the above code. 但我不知道如何“解析” page ,类似于上面代码中发生的情况。 In other words, from the solution found in the link ... 换句话说,从链接中找到的解决方案......

var theRequest = request({ ... });
theRequest.pipe(parser);
theRequest.abort();

..., how would I re-write my code to pipe and "parse" the request? ...,我如何重新编写代码以pipe并“解析”请求?

You can make easily thousands requests at the same time (eg from a single for loop ) and they will be queued and terminate automatically one by one, once a particular request is served. 您可以同时轻松地生成数千个请求(例如,从单个for loop ),并且一旦提供特定请求,它们将排队并逐个自动终止。

I think by default there are 5 sockets per domain and this limit in your case should be more than enough. 我认为默认情况下每个域有5个套接字,在你的情况下这个限制应该足够了。

It is highly probably that your server does not handle your requests properly (eg on error they are not terminated and hung up indefinitely). 您的服务器很可能无法正确处理您的请求(例如,错误时它们不会被终止并无限期挂起)。

There are three steps you can make to find out what is going on: 您可以通过三个步骤找出发生的情况:

  1. check if you are sending proper request -- as @mattyice observed there are some bugs in your code. 检查您是否发送了正确的请求 - 因为@mattyice观察到您的代码中存在一些错误。

  2. investigate server code and the way your requests are handled there -- for me it seems that the server does not serve/terminate them in first place. 调查服务器代码以及在那里处理请求的方式 - 对我而言,似乎服务器不首先服务/终止它们。

  3. try to use setTimeout when sending the request. 尝试在发送请求时使用setTimeout 5000ms should be a reasonable amount of time to wait. 5000ms应该是合理的等待时间。 On timeout the request will be aborted with appropriate error code. 超时时,请求将以适当的错误代码中止。

As an advice: I would recommend to use some more suitable, easier in use and more accurate tools to do your testing: eg phantomjs. 作为建议:我建议使用一些更合适,更容易使用和更准确的工具来进行测试:例如phantomjs。

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

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