对于许多请求,nodeJS 会挂起并且 Promise 无法解析 - 特别是在低带宽情况下

2024-03-09

具体代码 - 循环中的多个请求挂出节点,尤其是在低带宽连接上。

const baseRequest = request.defaults();
const specialRequest = baseRequest.defaults( {
    agent : false //// MUCH better but still errors sometimes happened (on high bandwidth connection - 1mB/s)
    //agent : false, pool : {maxSockets: 100} //// WORKS! - no errors on high bandwidth 1mB/s //// ERRORS - on low 50kB/s(down) 10kB/s(up) bandwidth connection - but moust of the requests are corectly served
    //agent : false, pool : {maxSockets: 500} //// ERRORS on high bandwidth 1mB/s //// ERRORS - on low 50kB/s(down) 10kB/s(up) bandwidth connection
    //agent : false, pool : {maxSockets: 500}, timeout : 5000 //// ERRORS on high bandwidth 1mB/s //// ERRORS - on low 50kB/s(down) 10kB/s(up) bandwidth connection
});

process.env.UV_THREADPOOL_SIZE = 128;

// promises array to store mutations (data)
var urlDataPromiseArray = [];
var sizeDataPromiseArray = [];

// here is the function (request) to grab the data:
function getData(url, i) {
    return new Promise(function (resolve) {
        var serverWork = 'serverWork/dataSave_' + i + '_.txt';
        var fs_cws_serverWork = fs.createWriteStream(serverWork, 'utf8');

        var requestData = specialRequest({url : url}, function(err, resp, data) {
            if (err || resp.statusCode >= 400) {
                if (err) {
                    console.log('something goes bad! - ' + err + ' --- ' + i);
                    sizeDataPromiseArray.push(getSize(i, 0));
                    return resolve(i + ' bad');
                }
                else {
                    console.log('something goes bad! - ' + i);
                    sizeDataPromiseArray.push(getSize(i, 0));
                    return resolve(i + ' bad');
                }
            }
            else if (!err && resp.statusCode === 200) {
                var dataLength = data.length;
                console.log('ok! - ' + i + ' - ' + dataLength);
                sizeDataPromiseArray.push(getSize(i, dataLength));
                return resolve(i + ' good - ' + dataLength);
            }
            else {
                console.log('need to check - ' + i);
                sizeDataPromiseArray.push(getSize(i, 0));
                return resolve(i + ' shit happens');
            }
        }).pipe(fs_cws_serverWork);
    })
}

// another function to calculate size of downloaded data
function getSize(i, size) {
    return new Promise(function(resolve) {
        resolve({
            [i]: size
        });
    });
}

// here we prepare the loop:
var loop = 1000;
for (var i = 0; i < loop; i++) {
    urlDataPromiseArray.push(getData('https://www.google.com', i));
    sizeDataPromiseArray.push(getSize(i, 0));
}

Promise.all(urlDataPromiseArray).then(function(url) {
    console.log();
    console.log('===============');
    console.log('===============');
    console.log('===============');
    console.log();
}).then(function() {
    return Promise.all(sizeDataPromiseArray).then(function(size) {
        size.forEach(function(item) {
            for (var key in item) {
                var value = item[key];
                console.log('--------');
                console.log(value + ' - size of request number: ' + key);
                console.log('--------');
            };
        });
    });
}).catch(function(err) {
    console.log('ERROR: ' + err);
});

console.log();
console.log('===============');
console.log('loop is finished - now we wait for async work to be done');
console.log('===============');
console.log();

我得到的错误:

套接字挂起

读取经济重置

连接 ETIMEDOUT IP:端口

超时时间

ESocket超时

当我设置agent : false这好多了,但在高带宽连接上有时仍然会发生错误 - 1mB/s,并且在低带宽 [50kB/s(down) 10kB/s(up)] 连接上也会出现很多错误,例如: -socket hang up and read ECONNRESET并且在并且所有都是connect ETIMEDOUT IP:PORT并且->承诺不起作用

当我设置agent : false, pool : {maxSockets: 100}它可以工作 - 在高带宽 1mB/s 上没有错误,但在低 50kB/s(下行)10kB/s(上行)带宽连接上有错误(但它比仅使用“代理:假”更好 - 大多数请求得到正确处理),例如: -socket hang up and read ECONNRESET但承诺解决!

当我设置agent : false, pool : {maxSockets: 500}它有效 - 在高带宽 1mB/s 上没有错误,但在低 50kB/s(向下)10kB/s(向上)带宽连接上有错误(实际上这比“pool : {maxSockets: 100}”选项更糟糕)喜欢: - 主要是socket hang up但是也read ECONNRESET并在“连接 ETIMEDOUT IP:PORT”处并且 -> 承诺不会解决

当我设置agent : false, pool : {maxSockets: 500}, timeout : 5000- 高带宽 (1mB/s) 上有错误 - 低 50kB/s(下行)10kB/s(上行)带宽连接错误如下所示: - 许多ESOCKETTIMEDOUT还有很多ETIMEDOUT但承诺正确解决

所以我想要的就是承诺始终正确解决。所有请求必须快速服务(导致多个客户端连接到服务器),但它们可以传递错误 - 我只需要它们快速 - 在低带宽上,只能传递错误,但客户端必须知道这是低带宽的原因服务器的,并且必须尽快得到此响应。

agent : false, pool : {maxSockets: 100}- 这在高带宽上运行得很好(一切都很好,承诺通过)

agent : false, pool : {maxSockets: 500}, timeout : 5000- 这在低带宽上效果最好(快速错误,承诺得到解决)

如何合并它们,以便它在高带宽和低带宽上都能正常工作 - 最后一部分像上面描述的那样被接受(错误) - 但也许我没有什么可以做得更好 - 为什么承诺不起作用?

类似主题:

  • 当大量请求非常快地发出时,如何修复“套接字挂断错误” https://stackoverflow.com/questions/55626343/how-to-fix-the-socket-hangup-error-when-large-number-of-requests-are-made-rea

  • web socket挂起获取多请求nodejs https://stackoverflow.com/questions/54766557/web-socket-hang-up-get-multi-request-nodejs

  • Nodejs 请求、循环和承诺 https://stackoverflow.com/questions/37102490/nodejs-request-loop-promise

  • Node JS https请求socket挂断(mikeal/request模块) https://stackoverflow.com/questions/52107851/node-js-https-request-socket-hang-up-mikeal-request-module

  • https://github.com/request/request/issues/3167 https://github.com/request/request/issues/3167

  • node.js http.get 在对远程站点发出 5 次请求后挂起 https://stackoverflow.com/questions/16965582/node-js-http-get-hangs-after-5-requests-to-remote-site

  • Node js - http.request() 连接池问题 https://stackoverflow.com/questions/15533448/node-js-http-request-problems-with-connection-pooling#

  • NodeJS 在多次请求后无限期挂起 https://stackoverflow.com/questions/21217743/nodejs-hangs-indefinitely-after-multiple-requests#

  • 从 Node.js 向 API 发送大量请求会导致错误 https://stackoverflow.com/questions/44908726/sending-many-requests-from-node-js-to-an-api-causes-error

  • NodeJS - 大量请求关闭后“套接字挂起” https://stackoverflow.com/questions/27116405/nodejs-socket-hang-up-after-large-amount-of-requests-closing

  • 并发发送大量请求时修复node.js https库的解决方案 https://stackoverflow.com/questions/42432213/solutions-for-fixing-node-js-https-library-when-huge-amount-of-request-need-be-s

  • node 和 socket.io 多个 API 调用在一段时间后挂起 https://stackoverflow.com/questions/48876048/node-and-socket-io-multiple-api-calls-hang-after-a-while


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对于许多请求,nodeJS 会挂起并且 Promise 无法解析 - 特别是在低带宽情况下 的相关文章

随机推荐