The problem
The front end of the project is VUE, the Node end uses Express as the middle layer, and the back end tells us that there is an HTTP request that takes a long time, and it will be optimized later, so it should be dealt with in an emergency first (😅).
During the debug process, it is found that timeout is always occurring, and the setting of client request is checked. There is no special limit on request timeout. Then, I immediately check the Node API nodejs.org/dist/latest…
The Node server’s default connection duration for each client request is 2 minutes. During this time, the server does not send a response message and the client’s connection is reset.
To discuss
Let’s start by creating a Node server instance.
|
Http. createServer Create an HTTP server server, in which we add an HTTP client request listener function with two parameters: req and RES. Where req is a client request object containing the client request information, it is an HTTP.IncomingMessage instance. Res is the ServerResponse object, which is an HTTP.ServerResponse instance. After the server processes the user request, it returns the response information to the client through the access object
Timeout Settings for the server
For an HTTP server, we might set two types of timeouts:
The timeout of the user request is the http.IncomingMessage instance -req in the above example
If a user request takes a long time, the server stays in the receiving state of one user request and blocks the entry of other users’ requests.
The timeout for the ServerResponse is http.ServerResponse instance -res in the above example
If the response time of the server is too long, users cannot receive the response information from the server for a long time, which affects user experience and processing of other user requests
The solution
Node.js HTTP server setTimeout using setTimeout() method.
There is a setTimeout() method in http.server, http.incomingMessage, and http.serverResponse. Server.settimeout () sets the timeout time of all user requests and server responses, whereas req.settimeout () can be set only for this request timeout time, and res.settimeout () can be set only for this request server response timeout time. We can set the timeout on the HTTP server as follows:
Set the timeout for all user requests and server responses to 5 seconds:
|
|
For a time-consuming user request, we set the user request timeout to 20 seconds:
|
|
For a user request that takes a long time to process on the server, we set the timeout for the server response to 10 seconds:
|
|
Use express.js middleware to set timeouts
Express.js provides a powerful middleware mechanism that allows more flexibility in setting HTTP server timeouts.
For example, we could add the following handlers to the app.js file to set timeout times for requests and server responses respectively, or we could set timeout times only for requests or server responses:
|
For a time-consuming user request or request requiring more response time to complete processing, we can set the timeout time separately in its routing processing method or routing middleware:
|
Using Koa middleware to set timeouts, we can set the time for the server to respond.
|
expand
After handling the server request response Settings, the problem is not over. If the front-end project deployment uses Nginx as a proxy, it needs to be modified in nginx to change the connection timeout limit.
|
A quick note:
Proxy_connect_timeout The default value is 60s
This directive sets the connection timeout with the upstream server. If your upstream server is up but hanging (for example, there are not enough threads to process the request, so put your request into the pool for later processing), this declaration is not useful. The connection to the upstream server has already been established.
Proxy_read_timeout Default value: 60s
This directive sets the read timeout with the proxy server. This determines how long Nginx will wait to get a response to the request. This time is not the time to get the entire response, but the time for two reading operations.
Proxy_send_timeout The default value is 60s
This specifies the timeout for sending requests to the upstream server. The timeout is set not for the entire send period, but for both write operations. If upstream does not receive new data after a timeout, nginx will close the connection.
The last
Node servers are very flexible in handling connection timeouts, and such long response times should be avoided, despite having several instances open, which can consume a lot of network resources when there are many concurrent requests.
From: www.dazhuanlan.com/2020/04/28/… cf_chl_jschl_tk=e611af567dca04cf9d539db7b1c711726815020d-1599818733-0-AU4au0mzDey_Nfy0B48aqDwg1qbQsSU7GzIDrz3BA9UWW3mS2T R_Uf-xHtBd9CvDtpU_XeAAV7ZCAUU4TiEwVP77ykSDZpbm2_dQKsrmqaI6Wbf-akzDaA6WOuVniEjMyZ27niMdBuGhUuzG8NjYy9-2IY3GEIYOOCOT-C-Fc5 vW6YI0Sn0ZaP2Jte6j91jwHscqdxmpZqnbb67k-FgUkvxsdnQsxosefRvRiAeYb06QYoVayY7PNnE2L9EYIspGrQ-X1z3BSamtrvEzEltXLlxOQrsgKo5WoF 0tmkNPIo8ZT7cW594-POcQGOLQv5KeDw