如何在 node.js 中为客户端 http 连接设置超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3586760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:40:20  来源:igfitidea点击:

How do I set a timeout for client http connections in node.js

node.js

提问by Dan List

I'm writing a node.js application that needs to talk to a server. It establishes an http connection with the following code:

我正在编写一个需要与服务器通信的 node.js 应用程序。它使用以下代码建立 http 连接:

var client = http.createClient(u.port, u.hostname, u.secure);
client.on("error", function(exception) {
    logger.error("error from client");
});
var request = client.request(method, u.path, headers);

I don't see any option in the node.js documentation for setting a timeout on the connection, and it seems to be set to 20 seconds by default. The problem I'm having is that I have users in China on what appears to be a slow or flaky network, who sometimes hit the timeout connecting to our datacenter in the US. I'd like to increase the timeout to 1 minute, to see if that fixes it for them.

我在 node.js 文档中没有看到任何用于设置连接超时的选项,默认情况下似乎设置为 20 秒。我遇到的问题是,我在 CN 的用户使用的网络似乎很慢或不稳定,他们有时会超时连接到我们在美国的数据中心。我想将超时增加到 1 分钟,看看是否可以为他们解决这个问题。

Is there a way to do that in node.js?

有没有办法在 node.js 中做到这一点?

回答by Matjaz Lipus

Try

尝试

request.socket.setTimeout(60000); // 60 sec

request.socket.setTimeout(60000); // 60 秒

回答by Toby Hede

I think you can do something like:

我认为您可以执行以下操作:

request.connection.setTimeout(60000)

request.connection returns the net.Stream object associated with the connection. and net.Stream has a setTimeout method.

request.connection 返回与连接关联的 net.Stream 对象。和 net.Stream 有一个 setTimeout 方法。

回答by ruvim

There is no capability in Node to increaseconnect timeout. Since usually connect timeout (i.e. connection establishing timeout) is OS-wide setting for all applications (e.g., 21 seconds in Windows, from 20 to 120 seconds in Linux). See also Timouts in Request package.

Node 中没有增加连接超时的能力。由于通常连接超时(即连接建立超时)是所有应用程序的操作系统范围设置(例如,Windows中为21秒,Linux 中为 20 到 120 秒)。另请参阅请求包中的超时

In contrast, Node allows to set decreased timeout and abort connecting even in case when the connection is not yet established.

相比之下,即使在连接尚未建立的情况下,Node 也允许设置减少的超时和中止连接。

The further timeouts (in case of connection has been established) can be controlled according to the documentation (see request.setTimeout, socket.setTimeout).

可以根据文档(请参阅request.setTimeoutsocket.setTimeout)控制进一步的超时(如果已建立连接)。

回答by Pavel

You have to wait for the client socket connection to be established first, before setting the timeout. To do this, add a callback for the 'socket' event:

在设置超时之前,您必须先等待客户端套接字连接建立。为此,请为 'socket' 事件添加一个回调:

req.on('socket', function (socket) {
    myTimeout = 500; // millis
    socket.setTimeout(myTimeout);  
    socket.on('timeout', function() {
        console.log("Timeout, aborting request")
        req.abort();
    });
}).on('error', function(e) {
    console.log("Got error: " + e.message);
    // error callback will receive a "socket hang up" on timeout
});

See this answer.

看到这个答案