【问题标题】:Node HTTP library ignoring timeout argument during TCP handshake?节点 HTTP 库在 TCP 握手期间忽略超时参数?
【发布时间】: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


    【解决方案1】:

    超时属性仅适用于套接字不活动。您想使用 request.setTimeout 设置读取超时。 github 票 https://github.com/nodejs/node/issues/12005 澄清了这一点。

    【讨论】:

    • 谢谢。那里提到的一个 cmets 完成了这项工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2015-01-26
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多