英文:
Why should Go’s http.Client instances be reused?
问题
Go文档中提到:
> 客户端的传输通常具有内部状态(缓存的TCP连接),因此应该重复使用客户端而不是根据需要创建。客户端可以安全地供多个goroutine并发使用。
为什么重复使用客户端有助于保留传输的内部状态,即使我可以实例化一个传输并将其传递给单次使用的客户端实例,就像这样:
<!-- language-all: lang-go -->
client := &http.Client{Transport: transport}
英文:
The Go documentation says
> The Client's Transport typically has internal state (cached TCP connections), so Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.
Why does Client reuse help with preserving the Transport’s internal state, given that I can instantiate a transport and pass it to single-use Client instances like this:
<!-- language-all: lang-go -->
client := &http.Client{Transport: transport}
答案1
得分: 1
为什么应该重用Go的http.Client实例?
有很多原因。最普遍的原因是为了允许重用现有的HTTP连接。如果你为每个请求使用一个新的http.Client
实例,就无法利用现有的HTTP连接,这会导致更多的开销和较慢的性能。
另一个常见的原因是如果你调用使用cookie的HTTP服务器,可以使用cookie jar。
英文:
> Why should Go’s http.Client instances be reused?
There are many reasons. The most universal one is to allow reusing existing HTTP connections. If you use a new http.Client
instance for every request, you can never take advantage of existing HTTP connections, which will result in more overhead and slower performance for most common uses.
Another common reason would be to use a cookie jar, if you're calling an HTTP server that uses cookies.
答案2
得分: 0
consul-k8s项目实际上通过创建许多短暂的Client实例来实现这一点,所有这些实例共享同一个Transport。这是在https://github.com/hashicorp/consul-k8s/commit/5672d344中完成的,可能是偶然的,因为PR只是说:
> 对端点控制器+测试中的consul客户端处理方式进行了微小的更改。
在这个微小的更改之前,它每次都创建一个新的Transport(因此泄漏了大量的连接):
localConfig := api.DefaultConfig()
更改后,它重用了相同的Transport:
localConfig := r.ConsulClientCfg
有泄漏的版本从未进入任何实际发布,所以一切都很好。
英文:
The consul-k8s project actually does this, by creating many short-lived Client instances, all sharing the same Transport. This was done in https://github.com/hashicorp/consul-k8s/commit/5672d344, perhaps accidentally as the PR just said:
> Minor change to how we deal with consul clients in the endpoints controller+tests.
Prior to this minor change, it creates a new Transport every time (and thus leaks plenty of connections):
localConfig := api.DefaultConfig()
After the change, it reuses the same Transport:
localConfig := r.ConsulClientCfg
The leaky version never made it into any actual release, so it is all good.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论