【问题标题】:Node.js concurrent https.requests ECONNREFUSEDNode.js 并发 https.requests ECONNREFUSED
【发布时间】:2012-07-23 01:56:45
【问题描述】:

我正在尝试循环访问用户并在远程服务器上为每个用户执行请求。许多请求因ECONNREFUSED 错误而失败。我试过设置globalAgent.maxSockets 和惊人的请求,但问题仍然存在。这是来自远程服务器的响应还是我的节点应用程序中的错误?我该如何解决这个问题?

来源:

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);
});

【问题讨论】:

  • 连接被拒绝是在服务器端完成的。但是,您确定这是您经常遇到的错误吗?你已经掩盖了你的实际错误......最好输出真正的错误,以防万一你得到别的东西。
  • @Brad 我改变了它,但它们仍然是ECONNREFUSED
  • 你的问题出在服务器端。看那里。
  • 可能是你让服务器拒绝了你的请求。请记住,普通的网络浏览器只会连接到服务器两次...(每个 URL 2 个 TCP 连接,一个同步,一个异步)
  • 那么我应该在每个请求之间花点时间吗?服务器是否最有可能对请求设置上限?

标签: node.js http-request


【解决方案1】:

如果你连接到同一台服务器,实际上最好降低套接字池(globalAgent.maxSockets),默认5值应该足够了,但这取决于你的负载,当然还有远程服务器的响应时间。因此,您的 node.js 客户端将重新使用 keep-alive 连接,而不是创建新连接。这也可以解决远程服务器拒绝接受更多连接的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 2014-02-08
    • 2022-10-15
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2012-02-06
    • 2015-07-27
    相关资源
    最近更新 更多