![]() If your target has a 5 second timeout, and your client has a 4 second timeout, the client will never try to send a request down a dead socket. The solution to the dead socket issue is for your client to have a shorter socket timeout than the target. The proxy responds to the client with a 502 Bad Gateway error.The proxy sends the requests down a dead socket and Node.js throws an ECONNRESET.It re-uses a reference to the same TCP socket as before, but the socket on the end of the target application is actually closed. The proxy receives another request and believes it has an open connection that it can use.More than 5 seconds later, no requests have hit the target application, so the application closes the socket.Request is sent down the socket to the target application, and a successful response comes back.Proxy opens a reusable TCP socket to the target application with a 15 second timeout.Client requests a resource from the proxy.How does this look in practice? Failed HTTP Keep-Alive connection What happens in the real world (and for our product) is that the default timeout of the widely used agentkeepalive was 15 seconds – much longer than the Node.js timeout of 5 seconds. 502 Errors and their sources in your application Node.js also has a headers timeout (which should be ~1s greater than the keep alive timeout), which contributes to the persistent connection timeout behavior. In Node.js, the keep alive timeout is 5 seconds by default. On the other end of the client is the target server that the connection is calling, and the target server may have its own keep alive timeout. If nothing gets sent down the socket for the duration of the timeout, the socket for the connection is closed. In general, clients will configure a limited number of sockets and a connection timeout. Under the hood, HTTP persistent connections utilize OS level sockets. HTTP persistent connections in a nutshell ![]() Reproducing these elusive errors was nearly impossible, and we only really understood how they were being thrown once we did a deep dive into how HTTP persistent connections work. The ECONNRESET errors were fairly rare, occurring in about 1/1000 requests. We utilize agentkeepalive in our proxy service to reduce latency in connections from the proxy to the targets, but have periodically experienced ECONNRESET errors thrown by our proxy service. In ConnectReport, we have a proxy service that acts as the gateway to all requests across our different services, with targets including our management API and our core server. In Node.js clients, you can use a module like agentkeepalive to tell your HTTP/HTTPS clients to use persistent HTTP connections. HTTP keep-alive allows HTTP clients to re-use connections for multiple requests, and relies on timeout configurations on both the client and target server to decide when to close open TCP sockets. We suffered some difficult to track down ECONNRESET errors in our Node.js proxy service until we learned a lot about HTTP persistent connections and Node.js HTTP defaults.īy default, HTTP creates a new TCP connection for every request. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |