node.js - 在许多请求中,nodeJS 挂断,promise 无法解决——尤其是在低带宽的情况下
问题描述
特定代码 - 循环中的多个请求挂出节点,尤其是在低带宽连接上。
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();
我得到的错误:
插座挂断
阅读 ECONNRESET
连接 ETIMEDOUT IP:PORT
超时
套接字超时
当我设置agent : false
这更好但仍然有时会在高带宽连接上发生错误 - 1mB/s 并且在低带宽 [50kB/s(down) 10kB/s(up)] 连接上有很多错误,例如:-socket hang up
和read ECONNRESET
和所有都是connect ETIMEDOUT IP:PORT
AND -> 承诺不起作用<-
当我设置agent : false, pool : {maxSockets: 100}
它工作时 - 高带宽 1mB/s 没有错误但有错误 - 在低 50kB/s(down) 10kB/s(up) 带宽连接上(但它比使用 'agent : false' 工作得更好 -大多数请求都得到了正确的服务),例如:-socket hang up
但read ECONNRESET
承诺会解决!
当我将其设置agent : false, pool : {maxSockets: 500}
为 WORKS - HIGH 带宽 1mB/s 没有错误但有错误 - LOW 50kB/s(down) 10kB/s(up) 带宽连接(实际上这比 'pool : {maxSockets: 100} ' 选项)喜欢: - 主要socket hang up
但也read ECONNRESET
和在 'connect ETIMEDOUT IP:PORT' AND ->promises won't resolve<-
当我设置时agent : false, pool : {maxSockets: 500}, timeout : 5000
- 高带宽 (1mB/s) 存在错误 - 低 50kB/s(down) 10kB/s(up) 带宽连接错误看起来像: -ESOCKETTIMEDOUT
许多ETIMEDOUT
但承诺正确解决
所以我想要的是承诺总是正确解决。所有请求必须快速服务(导致多个客户端连接到服务器)但它们可以传递错误 - 我只需要它们快速 - 在低带宽上,只能传递错误,但客户端必须知道这是低带宽的原因服务器的,必须尽快得到这个响应。
agent : false, pool : {maxSockets: 100}
- 这在高带宽上运行良好(一切都好,承诺通过)
agent : false, pool : {maxSockets: 500}, timeout : 5000
- 这在低带宽上效果最好(快速错误,承诺得到解决)
如何合并它们,使其在高带宽和低带宽下都能正常工作 - 最后一部分被接受,如上所述(错误) - 但也许我没有什么可以做得更好 - 为什么承诺不起作用?
类似的话题:
解决方案
推荐阅读
- java - Spring Boot 注释和导入无法在 Intellij IDE 上解析
- generics - Serde_json 使用泛型序列化 to_string
- php - 两个如何使用两个具有相同刀片名称的降价主题?
- python-3.x - ModuleNotFoundError:没有名为“rfc822”的模块
- css - 是否有 Grid align-content: center; 对于 IE11?
- apache-kafka - Kafka 主题记录保留政策不明确
- c# - 如何在不使用 Builders 的情况下使用 .net(c#) 驱动程序更新 mongo db 中的文档?
- excel - 用户表单输入只能工作一次
- python - 如何使用python创建比特币地址
- mips - MIPS 中的递归函数