gpt4 book ai didi

node.js - NPM 请求模块(REST 客户端)的默认超时是多少?

转载 作者:IT老高 更新时间:2023-10-28 23:11:08 26 4
gpt4 key购买 nike

接下来是我的 node.js 调用以检索一些数据,这需要超过 1 分钟。这将在 1 分钟(60 秒)时超时。我也为延迟放置了一个控制台日志。但是我已将超时配置为 120 秒,但它没有反射(reflect)。我知道默认级别的 nodejs 服务器超时是 120 秒,但我仍然从这个 request 获得超时(60 秒)此调用的模块。请提供您对此的见解。

var options = {
method: 'post',
url:url,
timeout: 120000,
json: true,
headers: {
"Content-Type": "application/json",
"X-Authorization": "abc",
"Accept-Encoding":"gzip"
}
}
var startTime = new Date();
request(options, function(e, r, body) {
var endTime = new Date();
var latencyTime = endTime - startTime;
console.log("Ended. latencyTime:"+latencyTime/1000);
res.status(200).send(body);

});

最佳答案

来自 request options docs ,向下滚动到 timeout 条目:

timeout - Integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds).

注意最后一部分“如果无法建立底层 TCP 连接,则 OS 范围的 TCP 连接超时将覆盖超时选项”。

Timeouts 上还有一整节.基于此,以及您的代码示例,我们可以这样修改请求示例

request(options, function(e, r, body) {
if (e.code === 'ETIMEDOUT' && e.connect === true){
// when there's a timeout and connect is true, we're meeting the
// conditions described for the timeout option where the OS governs
console.log('bummer');
}
});

如果这是真的,您需要确定更改操作系统设置是否可行且可接受(这超出了此答案的范围,这样的问题在服务器故障上会更好)。

关于node.js - NPM 请求模块(REST 客户端)的默认超时是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39727972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com