Go语言如何处理HTTP keep-alive?

huangapple go评论85阅读模式
英文:

How does Go deal with HTTP keep-alive?

问题

我理解的规则是,如果客户端和服务器都支持持久连接,它们可以通过第一个请求中的Connection:keep-alive头部来使用它。在完成第一个请求/响应后,客户端和服务器将保持底层TCP连接打开,并在后续的请求/响应中继续使用同一连接。

关于编程模型,我不太清楚。考虑以下Go语言中的客户端代码:

resp, _ := client.Get("http://www.stackoverflow.com")
// 做一些其他的事情
resp, _ = client.Get("http://www.stackoverflow.com/questions")

据我所知,keep-alive是HTTP/1.1中的默认策略。

问题1:这两个请求是否使用同一TCP连接?

在服务器端:

当一个请求到达时,Go HTTP框架将其分派给一个处理程序,然后由于keep-alive,框架应该在同一TCP连接上准备下一个请求。然而,我没有在处理程序中看到任何阻塞模式的读取代码。所以,

问题2:Go HTTP框架是否使用某种非阻塞模式来处理keep-alive?

我的意思是,处理程序不会阻塞等待读取,而只是在完成一个请求后返回,然后框架将轮询每个非阻塞的TCP连接,如果其中一个连接有数据到达,它将将其分派给关联的处理程序,依此类推。只有当请求的头部包含Connection:Close时,框架在处理程序返回时关闭TCP连接。

英文:

I understand the rule that, if client and server both support persistent connection, they can use it through a Connection:keep-alive header in the first request. After that, both client and server will still keep the underlying TCP connection open when they are done with the first request/response, and then use the same connection in the following requests/responses.

What I'm not clear about is the 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-alive is the default strategy in HTTP/1.1.

Q1: Do these two requests use the same TCP connection?

On the server side:

When one request comes, the Go HTTP framework dispatches it to a handler, then due to keep-alive, the framework should prepare for the next request on the same TCP connection. However I don't see any block-mode read code in a handler. So,

Q2: Does the Go HTTP framework use some kind of non-block mode to deal with keep-alive?

I mean, a handler will not block for reading, but just return when done with a request, then the framework will poll each non-block TCP connection, if one of them has data arrive, it dispatches it to an associated handler, and so on. Only when the request has a header of Connection:Close, the framework will close the TCP connection when the handler returns.

答案1

得分: 3

问题1:这两个请求使用同一个TCP连接吗?

是的,如果连接没有被服务器关闭,http.Transport可以在默认配置下重用连接。

问题2:Go的http框架是否使用某种非阻塞模式来处理keep-alive?

Go的HTTP服务器默认处理keepalive连接。HTTP没有所谓的“非阻塞模式”。它按照HTTP规范描述的方式,在单个连接上按顺序处理请求和响应。

英文:

> Q1: Do these two requests use the same TCP connection?

Yes, if the connection wasn't closed by the server, the http.Transport can re-use the connection in the default configuration.

> Q2: Does the go http framework use some kind of non-block mode to deal with keep-alive?

The go HTTP Server handles keepalive connections be default. There's no such thing as a HTTP "non-block mode". It handles requests and responses on a single connection in a serial manner as described by the HTTP specification.

huangapple
  • 本文由 发表于 2014年10月29日 22:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/26633220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定