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

I have a Nodejs application that is designed for simple and straightforward end-to-end testing of a large web application. This app uses mikeal / Request and Cheerio to move, request, move and check web pages in the app.

We are refactoring some tests and we are faced with an issue where several functions request

are called sequentially. I believe it might be due to the Node.js process hitting the MaxSockets limit, but I'm not entirely sure.

Some codes ...

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 one topPageRequest

and runs subMenuRequests

, but then hangs after the request completes for the third submenu item.

It seems that I can use the Max-Sockets constraint, either on Node or on my machine (?) - I am testing this on a standard Windows 8 machine running Node v0.10.26.

I tried using request({pool:{maxSockets:25}, url:link}, function(err, resp...

it but it doesn't seem to make any difference.

It also seems like there is a way to abort the request object if I create it first (as found here ). But I have no idea how to "parse" page

, similar to what happens in the above code. In other words, from the solution found in the link ...

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

      

... how would I rewrite my code to pipe

and "parse" the request?

+3


source to share


1 answer


You can make thousands of requests at the same time (for example, from one for loop

), and they will be queued and automatically completed one by one as soon as a specific request is submitted.

I think there are 5 sockets per domain by default, and this limit should be more than enough in your case.

Probably, your server is processing your requests incorrectly (for example, if an error occurs, they are not interrupted and do not hang indefinitely).



You can take three steps to find out what's going on:

  • check if you are submitting the correct request. As @mattyice pointed out, there are some bugs in your code.

  • investigate the server code and the way your requests are handled - it seems to me that the server does not serve / complete them in the first place.

  • try using setTimeout

    when submitting your request. 5000ms should be sufficient latency. If timed out, the request will be aborted with the corresponding error code.

As a tip: I would recommend using some more suitable, easier to use and more accurate tools for your testing: phantomjs for example.

+1


source







All Articles