【发布时间】:2019-07-15 00:17:00
【问题描述】:
我有以下代码。它向目标服务器发送简单的 GET 请求。
但是,当没有网络服务器监听给定的 IP 地址时,节点会持续挂起大约 2 分钟+ - 完全不尊重超时值 - 应该是几秒钟。
我启动了 tcpdump,我注意到节点在这 2 分钟内不断向目标服务器发送 SYN 数据包。
为什么在 http 库上会发生这种情况?我测试了替代库(节点请求)并且超时在那里正常工作。但是由于某些原因,我不能使用它,需要坚持使用较低级别的库。
http = require('http');
const options = {
hostname: '52.178.167.109',
port: 80,
path: '/upload',
method: 'GET',
timeout: 5000, //5s timeout
};
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
req.end();
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.end();
root@abc:~# time node test.js
problem with request: connect ETIMEDOUT 52.178.167.109:80
real **2m15.474s** <-------
user 0m0.616s
sys 0m0.084s
【问题讨论】:
标签: node.js http timeout nodes