以下将是我的node.js调用以检索一些数据,这需要超过1分钟。这将是1分钟(60秒)的超时。我还为延迟设置了一个控制台日志。但是我已将超时配置为120秒,但它没有反映出来。我知道默认级别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);
});
答案 0 :(得分:9)
从request options docs,向下滚动到timeout
条目:
timeout - 包含在中止请求之前等待服务器发送响应头(并启动响应主体)的毫秒数的整数。请注意,如果无法建立基础TCP连接,则操作系统范围的TCP连接超时将取代超时选项(Linux中的默认值可以是20-120秒)。
请注意最后一部分“如果无法建立基础TCP连接,则操作系统范围的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');
}
});
如果这是真的,您需要决定是否可以更改操作系统设置(这超出了本答案的范围,并且在服务器故障上这样的问题会更好)。