-
-
Save shuhei/4098a648a969deb38aad2164bc387148 to your computer and use it in GitHub Desktop.
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); | |
} |
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/
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.?
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.
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.