Node.js HTTP请求有巨大的延迟
这有点奇怪,我不确定到底发生了什么,但http请求有不同的延迟,有时会变得很大。我把它定为Node.js HTTP请求有巨大的延迟,node.js,Node.js,这有点奇怪,我不确定到底发生了什么,但http请求有不同的延迟,有时会变得很大。我把它定为 process.time = new Date().getTime(); console.log('voice ' + voice) var req = http.get(config.host_ip + ":" + config.host_port + "/sendSpeech/" + voice, function (response) { response.on('
process.time = new Date().getTime();
console.log('voice ' + voice)
var req = http.get(config.host_ip + ":" + config.host_port + "/sendSpeech/" + voice, function (response) {
response.on('data', function (d) {
console.log("TOTAL TIME " + (new Date().getTime() - process.time))
});
}).on('error', function (e) {
// Call callback function with the error object which comes from the request
});
req.end();
有时,总时间为亚秒,有时则超过6秒。我对实际运行它发送到的函数的进程进行了计时,它的运行速度不到100毫秒
如果我从服务器上通过浏览器进行呼叫,则呼叫时间通常为亚秒。比从单独的节点应用程序调用要快得多
有人知道发生了什么事吗?它处理的大部分连接大约是6-10个,因此我认为工作负载不是特别重,尽管它运行在raspberry pi上
编辑:我正在使用节点5.4.1
编辑2:我刚刚尝试通过socket.io切换到WebSocket,速度差不多。意思是我认为这个问题与网络有关
在节点0.10及更早版本中,HTTP代理将仅同时打开5个节点
默认情况下,连接到单个主机。您可以轻松地更改此设置:
(假设您需要HTTP模块作为HTTP)
您可以通过设置maxSockets来增加套接字的数量
http.globalAgent.maxSockets = 20; // or whatever
阅读更多
旁注
它可能不会影响性能,但您使用的是http.get
类似http.request
Http.get
返回带有响应数据的回调。像这样使用它
process.time = new Date().getTime();
console.log('voice ' + voice)
var url = config.host_ip + ":" + config.host_port + "/sendSpeech/" + voice;
http.get(url, function(res) {
console.log("TOTAL TIME " + (new Date().getTime() - process.time))
}).on('error', function(e) {
// Call callback function with the error object which comes from the request
});
// you don't need req.end since http.get automatically calls req.end
@MitchellIngram是否多次Sententialy调用http.get?是的,多个get请求将同时打开。多个请求可能导致此问题。您可以增加http代理的maxSockets。我更新了答案。我正在使用节点v5。我可能应该说我已经调查过了。可能没有足够的信息。您需要缩小导致问题的组件范围。如果同一台机器上的浏览器的速度从来都不慢,那么听起来请求节点或网络可能是罪魁祸首。是的,我切换到web套接字,速度也一样慢,所以我认为这是一个网络问题。