Created
April 28, 2019 18:08
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
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
yep, but i want to run your example on localhost by request api not run function
startClient
. You know how about.?