When it comes to timing out HTTP requests, it looks like node.js has three separate timeouts:
当谈到超时HTTP请求时,看起来node.js有三个独立的超时:
- server.setTimeout http://nodejs.org/api/http.html#http_server_settimeout_msecs_callback
- server.setTimeout http://nodejs.org/api/http.html#http_server_settimeout_msecs_callback
- request.setTimeout http://nodejs.org/api/http.html#http_request_settimeout_timeout_callback
- request.setTimeout http://nodejs.org/api/http.html#http_request_settimeout_timeout_callback
- response.setTimeout http://nodejs.org/api/http.html#http_response_settimeout_msecs_callback
- response.setTimeout http://nodejs.org/api/http.html#http_response_settimeout_msecs_callback
Can anyone clarify what the difference is between each of these methods and why someone would want to use each one?
任何人都可以澄清每种方法之间的区别以及为什么有人想要使用每种方法?
1 个解决方案
#1
13
- You are running a web server in your node.js app. This determines how long node will leave a client request connection open with no traffic before closing it due to idle timeout. An example is a user loses power in their house while downloading a large file from your app. You set this once and it will apply to all client connections your server receives.
- 您正在node.js应用程序中运行Web服务器。这决定了节点在关闭之前将客户端请求连接打开而没有流量的时间长度,这是因为空闲超时。一个例子是用户在从您的应用程序下载大文件时失去了他们家中的电量。您设置一次,它将应用于您的服务器收到的所有客户端连接。
- This is for outgoing requests from your node program to a remote web server. So you write a scraper to download a file and your Internet connection dies while downloading. This determines when node finally gives up on waiting for data from the remote side. This will only affect a specific request as the underlying TCP connection will be closed and each outgoing request will get a distinct TCP connection.
- 这适用于从节点程序到远程Web服务器的传出请求。因此,您编写了一个刮刀来下载文件,并且您的Internet连接在下载时死亡。这确定了节点何时最终放弃等待来自远程端的数据。这只会影响特定请求,因为底层TCP连接将被关闭,每个传出请求将获得不同的TCP连接。
- Since the HTTP request and corresponding response occur via the same underlying TCP socket, my understanding is
req.setTimeout
andres.setTimeout
ultimately result in the same underlying system call that sets the timeout on the TCP socket itself using the corresponding libuv/os calls. So I think both are equivalent and you can you whichever one is more convenient or whichever one feels semantically clearer to you. I could be wrong about this though so if anyone else knows for certainly feel free to correct me. - 由于HTTP请求和相应的响应通过相同的底层TCP套接字发生,我的理解是req.setTimeout和res.setTimeout最终导致相同的底层系统调用,它使用相应的libuv / os调用设置TCP套接字本身的超时。所以我认为两者都是等价的,你可以选择哪一个更方便,或者你觉得语义更清晰。我可能错了,但如果有其他人知道的话肯定会随意纠正我。
Generally the defaults are reasonable. However, you might want to set these longer if you knew you had a lot of clients on very slow or flakey connections (you serve mobile phones in remote areas or satellites or whatever), and connections that were actually still viable we being closed due to timeout. You might want to set them shorter if you knew your clients were well-connected (like servers in the same datacenter), and you wanted to free up resources more aggressively.
通常默认值是合理的。但是,如果您知道很多客户端在非常慢或无聊的连接上(您在偏远地区或卫星或其他任何地方提供移动电话),并且实际上仍然可行的连接由于时间到。如果您知道客户端连接良好(如同一数据中心中的服务器),并且您希望更积极地释放资源,则可能需要将它们设置得更短。
#1
13
- You are running a web server in your node.js app. This determines how long node will leave a client request connection open with no traffic before closing it due to idle timeout. An example is a user loses power in their house while downloading a large file from your app. You set this once and it will apply to all client connections your server receives.
- 您正在node.js应用程序中运行Web服务器。这决定了节点在关闭之前将客户端请求连接打开而没有流量的时间长度,这是因为空闲超时。一个例子是用户在从您的应用程序下载大文件时失去了他们家中的电量。您设置一次,它将应用于您的服务器收到的所有客户端连接。
- This is for outgoing requests from your node program to a remote web server. So you write a scraper to download a file and your Internet connection dies while downloading. This determines when node finally gives up on waiting for data from the remote side. This will only affect a specific request as the underlying TCP connection will be closed and each outgoing request will get a distinct TCP connection.
- 这适用于从节点程序到远程Web服务器的传出请求。因此,您编写了一个刮刀来下载文件,并且您的Internet连接在下载时死亡。这确定了节点何时最终放弃等待来自远程端的数据。这只会影响特定请求,因为底层TCP连接将被关闭,每个传出请求将获得不同的TCP连接。
- Since the HTTP request and corresponding response occur via the same underlying TCP socket, my understanding is
req.setTimeout
andres.setTimeout
ultimately result in the same underlying system call that sets the timeout on the TCP socket itself using the corresponding libuv/os calls. So I think both are equivalent and you can you whichever one is more convenient or whichever one feels semantically clearer to you. I could be wrong about this though so if anyone else knows for certainly feel free to correct me. - 由于HTTP请求和相应的响应通过相同的底层TCP套接字发生,我的理解是req.setTimeout和res.setTimeout最终导致相同的底层系统调用,它使用相应的libuv / os调用设置TCP套接字本身的超时。所以我认为两者都是等价的,你可以选择哪一个更方便,或者你觉得语义更清晰。我可能错了,但如果有其他人知道的话肯定会随意纠正我。
Generally the defaults are reasonable. However, you might want to set these longer if you knew you had a lot of clients on very slow or flakey connections (you serve mobile phones in remote areas or satellites or whatever), and connections that were actually still viable we being closed due to timeout. You might want to set them shorter if you knew your clients were well-connected (like servers in the same datacenter), and you wanted to free up resources more aggressively.
通常默认值是合理的。但是,如果您知道很多客户端在非常慢或无聊的连接上(您在偏远地区或卫星或其他任何地方提供移动电话),并且实际上仍然可行的连接由于时间到。如果您知道客户端连接良好(如同一数据中心中的服务器),并且您希望更积极地释放资源,则可能需要将它们设置得更短。