简体   繁体   English

node.js HTTP Server,Request和Response Timeouts之间的区别

[英]Difference Between node.js HTTP Server, Request, and Response Timeouts

When it comes to timing out HTTP requests, it looks like node.js has three separate timeouts: 当谈到超时HTTP请求时,看起来node.js有三个单独的超时:

  1. 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
  2. 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
  3. 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. You are running a web server in your node.js app. 您正在node.js应用程序中运行Web服务器。 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. 您将此设置为一次,它将应用于您的服务器接收的所有客户端连接。
  2. This is for outgoing requests from your node program to a remote web server. 这适用于从节点程序到远程Web服务器的传出请求。 So you write a scraper to download a file and your Internet connection dies while downloading. 因此,您编写了一个刮刀来下载文件,并且您的Internet连接在下载时会死亡。 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. 这只会影响特定请求,因为底层TCP连接将被关闭,每个传出请求将获得不同的TCP连接。
  3. Since the HTTP request and corresponding response occur via the same underlying TCP socket, my understanding is req.setTimeout and res.setTimeout ultimately result in the same underlying system call that sets the timeout on the TCP socket itself using the corresponding libuv/os calls. 由于HTTP请求和相应的响应通过相同的底层TCP套接字发生,我的理解是req.setTimeoutres.setTimeout最终导致相同的底层系统调用,它使用相应的libuv / os调用设置TCP套接字本身的超时。 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. 我可能错了,尽管如此,如果有其他人知道肯定会随意纠正我。

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. 如果您知道客户端连接良好(如同一数据中心中的服务器),并且您希望更积极地释放资源,则可能需要将它们设置得更短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM