How to release http.Client in Go?

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

How to release http.Client in Go?

问题

你好!要释放http.Client和使用的资源,你需要执行以下步骤:

  1. 在你不再需要使用http.Client之后,确保调用client.CloseIdleConnections()方法。这将关闭所有空闲的持久连接并将其返回到连接池中。

  2. 如果你使用了自定义的Transport,则需要调用transport.CloseIdleConnections()方法来关闭所有空闲的持久连接。

  3. 如果你使用了自定义的Transport,还需要调用transport.CancelRequest(req *http.Request)方法来取消所有正在进行的请求。

  4. 最后,确保在你的代码中适当地处理错误和关闭连接的情况,以避免资源泄漏。

通过执行以上步骤,你可以释放http.Client和相关资源。希望对你有帮助!如果你有任何其他问题,请随时提问。

英文:

I built a http.Client for HTTP2 connection, what do I need to do to release the client and resource used?

答案1

得分: 32

http.Client不需要任何特殊的方式来释放“已使用”的资源。当它变得不可达时,由它使用的内存将被垃圾收集器回收。

http.Client不存储连接或状态信息。文档甚至指出应该重用http.Client

> 客户端的传输通常具有内部状态(缓存的TCP连接),因此应该重用客户端而不是根据需要创建。客户端可以安全地被多个goroutine并发使用。

如果您使用(例如嵌入)http.Client构建自己的客户端,并且分配了必须显式释放的资源,请在其上提供自己的Close()方法,并记录任何使用您自己的实现的人必须在不再需要时调用Close()

注意:

您可能会混淆的是,如果您使用http.Client执行HTTP操作(如Client.Do()Client.Get()Client.Post()等),它们会返回*http.Response的值,而该响应确实保存了连接、状态和其他资源,这些资源需要被释放,通常通过Response.Body.Close()来实现。引用自http包的文档:

> 客户端在使用完响应体后必须关闭它:
>
> resp, err := http.Get("http://example.com/")
> if err != nil {
> // 处理错误
> }
> defer resp.Body.Close()
> body, err := ioutil.ReadAll(resp.Body)
> // ...

Client.Get()中也有文档记录:

> 当err为nil时,resp始终包含非nil的resp.Body。调用者在读取完毕后应关闭resp.Body。

英文:

http.Client does not require any special way to free "used" resources. When it becomes unreachable, memory used by it will be reclaimed by the garbage collector.

http.Client does not store connection or state information. The documentation even states that http.Client should be reused:

> 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.

If you build your own client using (e.g. embedding) http.Client and you allocate resources that must be released explicitly, provide your own Close() method on it and document that anyone who uses your own implementation must call Close() if it is not needed anymore.

Note:

What you might confuse it with is that if you use an http.Client to do HTTP operations (like Client.Do(), Client.Get(), Client.Post() etc.), they return a value of *http.Response, and that response does hold a connection, state and other resources, which does need to be freed, typically via Response.Body.Close(). Quoting from the package doc of http:

> The client must close the response body when finished with it:
>
> resp, err := http.Get("http://example.com/")
> if err != nil {
> // handle error
> }
> defer resp.Body.Close()
> body, err := ioutil.ReadAll(resp.Body)
> // ...

It is also documented at Client.Get():

> When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

答案2

得分: 5

CloseIdleConnections()方法是在Go 1.12中添加的。

https://golang.org/pkg/net/http/#Client.CloseIdleConnections

CloseIdleConnections方法会关闭Transport中的任何连接,这些连接之前是从先前的请求中连接的,但现在处于空闲的“keep-alive”状态。它不会中断当前正在使用的任何连接。

英文:

The CloseIdleConnections() method was added in Go 1.12.

https://golang.org/pkg/net/http/#Client.CloseIdleConnections

> CloseIdleConnections closes any connections on its Transport which were previously connected from previous requests but are now sitting idle in a "keep-alive" state. It does not interrupt any connections currently in use.

huangapple
  • 本文由 发表于 2016年4月18日 16:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/36688633.html
匿名

发表评论

匿名网友

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

确定