如何调整nodejs(6.1.0)和expressjs(4.13.4)以处理大量并发连接。
为了证明express可以处理许多连接,我写了以下javascript:
var Q = require('q');
var needle = require('needle');
var logger = require('log4js').getLogger("deleteme");
var batches = 2;
var batchSize = 200;
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
var server = app.listen(8001);
var requestBatches = [];
for(var batchIndex = 0; batchIndex < batches; ++batchIndex) {
const thisBatch = batchIndex;
requestBatches.push(Q.delay(batchIndex * 1000 + 1).then(function() {
var requests = [];
var errors = 0;
for( var queryIndex = 0; queryIndex < batchSize; ++queryIndex) {
requests.push( Q.nfcall(needle.get, "http://localhost:8001/", {timeout: 1000}).catch(function(error) {
logger.error("Error: ", error.message);
++errors;
throw error;
}))
}
return Q.allSettled(requests).then(function() {
if(errors) {
logger.error("Batch: ", thisBatch, " failed ", errors, "/", batchSize)
} else {
logger.info("Batch: ", thisBatch, " all success")
}
})
}));
}
Q.allSettled(requestBatches).finally(function() {
logger.info("all done");
server.close();
});
所有这一切基本上都是在端口8001上启动服务器,在其上发送几批连接,计算批次的成功率,并报告结果。
现在,我正在服务器上进行200个连接,我希望nodejs减慢响应速度,但我会断开连接。
运行变化不一,有时我有2次连接重置,有时我有一吨。有时我有套接字挂断。
如何调整expressjs和nodejs以处理更多并发连接?
示例结果:
[2016-11-14 14:07:58.186] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:07:58.385] [ERROR] deleteme - Batch: 0 failed 1 / 200
[2016-11-14 14:07:59.280] [INFO] deleteme - Batch: 1 all success
[2016-11-14 14:07:59.281] [INFO] deleteme - all done
有很多失败:
[2016-11-14 14:09:54.544] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.547] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.548] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.548] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.548] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.549] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.550] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.550] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.551] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.551] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.551] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.551] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.552] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.552] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.552] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.552] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.553] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.553] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.553] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.554] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.554] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.554] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:54.745] [ERROR] deleteme - Batch: 0 failed 22 / 200
[2016-11-14 14:09:55.501] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.501] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.502] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.502] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.502] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.503] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.503] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.503] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.504] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.505] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.505] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.506] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.506] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.506] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.506] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.507] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.510] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.510] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.510] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.510] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.510] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.511] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.511] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.511] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.512] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.512] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.512] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.512] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.513] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.513] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.514] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.514] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.514] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.514] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.515] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.515] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.515] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.516] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.516] [ERROR] deleteme - Error: connect ECONNRESET 127.0.0.1:8001
[2016-11-14 14:09:55.627] [ERROR] deleteme - Batch: 1 failed 39 / 200
[2016-11-14 14:09:55.627] [INFO] deleteme - all done
答案 0 :(得分:0)
我建议您将服务器作为单独的进程运行,这可能会修复ECONNRESET错误。
我不确定你要在这里实现什么,但我尝试的是循环运行10,000个请求而没有任何错误。
server.js
<textarea id="textArea" rows="10" cols="50"></textarea>
<br/>
<input type="button" value="Convert" onclick="convert()"/>
test.js
var express = require('express');
var app = express();
var i = 0;
app.get('/', function(req, res){
res.send('hello world');
i++;
console.log(i);
});
var server = app.listen(8001);
我的电脑上大约需要9956ms而且没有任何错误
答案 1 :(得分:0)
尽可能多地使用Async调用,除非确实需要,否则不要使用Sync。