Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created April 28, 2019 18:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shuhei/4098a648a969deb38aad2164bc387148 to your computer and use it in GitHub Desktop.
Save shuhei/4098a648a969deb38aad2164bc387148 to your computer and use it in GitHub Desktop.
A test case for server.headersTimeout + keep alive (fails on Node v10.15.2 and newer)
const http = require("http");
const net = require("net");
const server = http.createServer((req, res) => {
req.resume();
res.end("hello");
});
server.keepAliveTimeout = 6 * 1000;
server.headersTimeout = 4 * 1000;
server.listen(0, () => {
const { address, port } = server.address();
console.log("Listening on %s:%d", address, port);
startClient(port);
});
function startClient(port) {
const socket = net.createConnection({ port });
let responseCount = 0;
socket.on("data", (chunk) => {
console.log("client data:", chunk.toString("utf8").split("\r\n")[0]);
responseCount += 1;
if (responseCount === 2) {
process.exit(0);
}
});
socket.on("error", (err) => {
console.log("client error:", err);
process.exit(1);
});
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
socket.write("\r\n");
setTimeout(() => {
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
// `headersTimeout` doesn't seem to fire if request headers
// are sent in one packet.
setTimeout(() => {
socket.write("\r\n");
}, 10);
}, 5000);
}
@dattp
Copy link

dattp commented Feb 2, 2023

Thank you for this example, how can I call 2 APIs to throw an error instead of running setTimeout?

@shuhei
Copy link
Author

shuhei commented Feb 2, 2023

Hi, this is an old example for reproducing a problematic behavior of old Node versions. I'm not sure but the same issue may not happen on recent Node versions. What do you want to do with it?

@dattp
Copy link

dattp commented Feb 2, 2023

Hi, this is an old example for reproducing a problematic behavior of old Node versions. I'm not sure but the same issue may not happen on recent Node versions. What do you want to do with it?

I am encountering 502 error when making a call from Kong gateway to a service running on Node.js, but it only appears intermittently in the production environment, and I am trying to reproduce it on localhost or the development environment.

@shuhei
Copy link
Author

shuhei commented Feb 2, 2023

I see. Did you find this gist from nodejs/node#27363? If so, the issue should have been fixed (even though I haven't verified it by myself). server.keepAliveTimeout longer than your gateway's idle TCP connection timeout might be enough to fix it. I wrote a blog post about it but haven't updated it about the fix on headersTimeout. https://shuheikagawa.com/blog/2019/04/25/keep-alive-timeout/

@dattp
Copy link

dattp commented Feb 2, 2023

I see. Did you find this gist from nodejs/node#27363? If so, the issue should have been fixed (even though I haven't verified it by myself). server.keepAliveTimeout longer than your gateway's idle TCP connection timeout might be enough to fix it. I wrote a blog post about it but haven't updated it about the fix on headersTimeout. https://shuheikagawa.com/blog/2019/04/25/keep-alive-timeout/

yep, but i want to run your example on localhost by request api not run function startClient . You know how about.?

@shuhei
Copy link
Author

shuhei commented Feb 2, 2023

If you want to reproduce the intermittent 502 with your server localhost, you could make a lot of requests on the same TCP connection (TCP keep alive; see documentation of your HTTP client) with the interval of your gateway's TCP idle timeout. You might be able to reproduce it after many attempts (hint: you can do it concurrently with multiple connections). If you need to see more concrete code, you can formulate your problem and ask on stackoverflow.com or somewhere.

I don't have much free time now. What I could offer was writing a few comments off the top of my head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment