1

I am trying to cycle through users and perform a request for each on a remote server. A lot of the requests fail with an ECONNREFUSED error. I've tried setting globalAgent.maxSockets and staggering requests but the issue persists. Is this a response from the remote server or an error in my node app? How could I resolve this?

Source:

var https = require("https");       
https.globalAgent.maxSockets = 1024;

var get = function(username, password, cb, failed) {
    var post_data = 'username='+user+'&password='+password;

    var post_req = https.request({
        host:__HOST__, 
        port:443, 
        path:'/Controls/CredentialsUI.ashx', 
        method: "POST",
        agent:false,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
    }), function(r) {
        // get cookie if received 
        if( r.headers["set-cookie"] ) 
            cb(r.headers["set-cookie"][0].split(";")[0].split("=")[1])
        else failed();
    });

    post_req.on('error', function(error) {
        // Error handling here
        console.log(error, "for "+username);
        });

    post_req.write(post_data);
    post_req.end();
};

var success = function(){ /*...*/ };
var failure = function(){ /*...*/ };

var users = [/* array about 300 in size */];
users.map(function(user) {
    get(user.name, user.password, success, failure);
});
matt3141
  • 4,303
  • 1
  • 19
  • 24
  • Connection refused is something that is done on the server end. However, are you sure that's the error you are always getting? You have masked your actual error... better to output the real error, just in case you're getting something else. – Brad Jul 23 '12 at 02:00
  • @Brad I changed it from being constant but they are all still `ECONNREFUSED` – matt3141 Jul 23 '12 at 02:03
  • Your problem is on the server end. Look there. – Brad Jul 23 '12 at 02:03
  • It's possible your making the server reject you with so many requests. Remember, a normal web browser will only connect to the server twice... (2 TCP conn's per URL, one sync, one async) – EdH Jul 23 '12 at 02:04
  • Should I just take time in between each request then? Does the server most likely have a cap on requests? – matt3141 Jul 23 '12 at 02:07

1 Answers1

0

If you're connecting to the same server, it's actually better to lower socket pool(globalAgent.maxSockets), default 5 value should be enough, but it depends on your load, and response time from remote server of course. So your node.js client will re-use keep-alive connections, instead of creating new ones. This also may resolve the problem with remote server refusing to accept more connections.

bbbonthemoon
  • 1,760
  • 3
  • 19
  • 31