I understand the rule: if the client and server maintain a constant connection, they can use it through the header Connection:keep-alivein the first request. After that, both the client and the server will still maintain an open TCP connection when they are executed with the first request / response, and then use the same connection in the following requests / responses.
What I don't understand is a programming model. Consider the following client code in go:
resp, _ := client.Get("http://www.stackoverflow.com")
// do some other things
resp, _ = client.Get("http://www.stackoverflow.com/questions")
As far as I know, keep-alivethis is the default strategy in HTTP / 1.1.
Q1: Do these two requests use the same TCP connection?
On the server side:
When a single request arrives, the Go HTTP infrastructure sends it to the handler, and then, due to maintenance, the environment must prepare for the next request in the same TCP connection. However, I do not see the block mode reader in the code handler. In this way,
Q2: Does the Go HTTP infrastructure use some non-blocking mode to work with keep-alive?
I mean, the handler will not block reading, but will simply return when it is done with the request, then the structure will check every unblocked TCP connection, if one of them has data, it sends it to the associated handler, and so on. Only when the request has a header does the Connection:Closeenvironment close the TCP connection when the handler returns.