Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: delete stream.md && supplement stream description to client #752

Merged
merged 8 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion content/en/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,15 @@ func main() {

## Streaming Read Response Content

Hertz's client supports streaming read HTTP response content. For more information, please refer to [Client](/docs/hertz/tutorials/basic-feature/stream/#client).
Hertz's client supports streaming read HTTP response content.

Since the client has the problem of multiplexing connections, if streaming is used, the connection will be handled by the user( `resp.BodyStream()` is encapsulated by connection) once streaming is used. There are some differences in the management of connections in the above case:

1. If the user doesn't close the connection, the connection will eventually be closed by the GC without causing a connection leak. However, due to the need to wait for 2 Round-Trip Time to close the connection, in the case of high concurrency, the consequence is that there will be too many open files and creating a new connection will be impossible.

2. Users can recycle the connection by calling the relevant interface. After recycling, the connection will be put into the connection pool for reuse, so as to achieve higher resource utilization and better performance. The following methods will recycle the connection. Warning: Recycling can only be done once.
1. Explicit call: `protocol.ReleaseResponse(), resp.Reset(), resp.ResetBody()`.
1. Implicit call: The server side will also recycle the response. Assign the client side response to the server side or pass the server side response to the client (eg: client uses reverse proxy), there is no need to display the method of calling the recovery.
rogerogers marked this conversation as resolved.
Show resolved Hide resolved

Sample Code:

Expand All @@ -539,6 +547,10 @@ func main() {
c, _ := client.NewClient(client.WithResponseBodyStream(true))
req := &protocol.Request{}
resp := &protocol.Response{}
defer func() {
protocol.ReleaseRequest(req)
protocol.ReleaseResponse(resp)
}()
req.SetMethod(consts.MethodGet)
req.SetRequestURI("http://127.0.0.1:8080/streamWrite")
err := c.Do(context.Background(), req, resp)
Expand Down
46 changes: 0 additions & 46 deletions content/en/docs/hertz/tutorials/basic-feature/stream.md

This file was deleted.

13 changes: 12 additions & 1 deletion content/zh/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,14 @@ func main() {

## 流式读响应内容

Hertz 客户端支持流式读取 HTTP 响应内容。(更多内容请参考 [Client](/zh/docs/hertz/tutorials/basic-feature/stream/#client))
Hertz 客户端支持流式读取 HTTP 响应内容。

client 有复用连接的问题,如果使用了流式,那连接就会交由用户处理 ( `resp.BodyStream()` 底层是对 connection 的封装),这个时候对连接的管理会有一些不同:

1. 如果用户不关闭连接,连接最终会被 GC 关掉,不会造成连接泄漏。但是,由于关闭连接需要等待 2RTT,在高并发情况下可能会出现 fd 被打满导致无法新建连接的情况。
2. 用户可以调用相关接口回收连接,回收后,该连接会放入连接池中复用,资源使用率更好,性能更高。以下几种方式都会回收连接,注意回收只能回收一次。
1. 显式调用 `protocol.ReleaseResponse(), resp.Reset(), resp.ResetBody()`。
2. 非显式调用:server 侧也会有回收 resp 的逻辑。如果将 client 的 response 赋值给 server 或者直接把 server 的 response 传递给 client 的情况下(如:client 作为反向代理)就不需要显示调用回收的方法了。
rogerogers marked this conversation as resolved.
Show resolved Hide resolved

示例代码:

Expand All @@ -537,6 +544,10 @@ func main() {
c, _ := client.NewClient(client.WithResponseBodyStream(true))
req := &protocol.Request{}
resp := &protocol.Response{}
defer func() {
protocol.ReleaseRequest(req)
protocol.ReleaseResponse(resp)
}()
req.SetMethod(consts.MethodGet)
req.SetRequestURI("http://127.0.0.1:8080/streamWrite")
err := c.Do(context.Background(), req, resp)
Expand Down
45 changes: 0 additions & 45 deletions content/zh/docs/hertz/tutorials/basic-feature/stream.md

This file was deleted.