Skip to content

Commit 71c7226

Browse files
authored
docs: update extWriter (#786)
1 parent 51eb134 commit 71c7226

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed
Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
11
---
22
title: "Response Writer Extension"
33
linkTitle: "Response Writer Extension"
4-
date: 2023-03-10
4+
date: 2023-09-22
55
weight: 6
6-
keywords: ["Response Writer Extension", "Response.HijackWriter"]
6+
keywords: [ "Response Writer Extension", "Response.HijackWriter" ]
77
description: "Response Writer Extension provided by Hertz."
88

99
---
1010

11-
Hertz provides response writer extension, if users need to hijack the writer of the response, they can implement the corresponding interfaces according to their needs.
11+
According to Hertz's [layered architecture](https://www.cloudwego.io/zh/docs/hertz/overview/), the actual write
12+
operation of the HTTP response is performed after the application layer user processing logic returns. Under this
13+
constraint, users cannot flexibly control the behavior of write operations on demand. This limitation is especially
14+
obvious in scenarios such as controlling chunked encoding write logic
15+
and [SSE](https://github.com/hertz-contrib/sse#hertz-sse).
1216

13-
## Interface Definition
17+
To solve this problem, Hertz provides an extension called "Response Writer Hijacking" that can vertically penetrate the
18+
limitations brought by the layered architecture in an orthogonal way. It allows users to freely customize the logic of
19+
writing responses in the application layer according to their own needs, improving the framework's ease of use.
20+
21+
## Core Design
22+
23+
### Interface Definition
1424

1525
interface is defined in `pkg/network/writer`.
1626

1727
```go
1828
type ExtWriter interface {
19-
io.Writer
20-
Flush() error
29+
io.Writer
30+
Flush() error
2131

22-
// Finalize will be called by framework before the writer is released.
23-
// Implementations must guarantee that Finalize is safe for multiple calls.
24-
Finalize() error
32+
// Finalize will be called by framework before the writer is released.
33+
// Implementations must guarantee that Finalize is safe for multiple calls.
34+
Finalize() error
2535
}
2636
```
2737

2838
### Hijack Your Own Response Writer
2939

30-
Hertz provides `Response.HijackWriter` in `app.RequestContext` to allow users to hijack their own response writer, which provides another way for response writing process.
40+
Hertz provides `Response.HijackWriter` in `app.RequestContext` to allow users to hijack their own response writer, which
41+
provides another way for response writing process.
3142

3243
Example:
3344

3445
```go
35-
h.GET("/hijack", func(c context.Context, ctx *app.RequestContext) {
36-
// Hijack the writer of response
37-
ctx.Response.HijackWriter(yourResponseWriter)
38-
})
46+
h.GET("/hijack", func (c context.Context, ctx *app.RequestContext) {
47+
// Hijack the writer of response
48+
ctx.Response.HijackWriter(**yourResponseWriter**)
49+
})
3950
```
4051

4152
## Supported Response Writer Extension
4253

43-
Hertz provides `NewChunkedBodyWriter` to create a response writer which allow users to flush chunk immediately during the handler process, it is defined under `pkg/protocol/http1/resp/writer`, and you can implement your own response writer.
54+
- `ChunkedBodyWriter`: Hertz provides `NewChunkedBodyWriter` to create a response writer which allow users to flush chunk immediately during
55+
the handler process, it is defined under `pkg/protocol/http1/resp/writer`, and you can implement your own response
56+
writer.
4457

4558
### ChunkedBodyWriter
4659

4760
Example:
4861

4962
```go
50-
h.GET("/flush/chunk", func(c context.Context, ctx *app.RequestContext) {
51-
// Hijack the writer of response
52-
ctx.Response.HijackWriter(resp.NewChunkedBodyWriter(&ctx.Response, ctx.GetWriter()))
53-
54-
for i := 0; i < 10; i++ {
55-
ctx.Write([]byte(fmt.Sprintf("chunk %d: %s", i, strings.Repeat("hi~", i)))) // nolint: errcheck
56-
ctx.Flush() // nolint: errcheck
57-
time.Sleep(200 * time.Millisecond)
58-
}
59-
})
63+
h.GET("/flush/chunk", func (c context.Context, ctx *app.RequestContext) {
64+
// Hijack the writer of response
65+
ctx.Response.HijackWriter(resp.NewChunkedBodyWriter(&ctx.Response, ctx.GetWriter()))
66+
67+
for i := 0; i < 10; i++ {
68+
ctx.Write([]byte(fmt.Sprintf("chunk %d: %s", i, strings.Repeat("hi~", i)))) // nolint: errcheck
69+
ctx.Flush() // nolint: errcheck
70+
time.Sleep(200 * time.Millisecond)
71+
}
72+
})
6073
```

content/zh/docs/hertz/tutorials/framework-exten/response_writer.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
---
22
title: "Response 的 Writer 扩展"
33
linkTitle: "Response 的 Writer 扩展"
4-
date: 2023-03-10
4+
date: 2023-09-22
55
weight: 6
66
keywords: ["Response 的 Writer 扩展", "Response.HijackWriter"]
77
description: "Hertz 提供的 Response 的 Writer 扩展。"
88

99
---
1010

11-
Hertz 提供了 response 的 writer 扩展,用户可以根据自己的需要实现相应的接口去劫持 response 的 writer
11+
按照 Hertz [分层架构](https://www.cloudwego.io/zh/docs/hertz/overview/)设计,HTTP 响应实际的写操作是在应用层用户处理逻辑返回之后进行的。用户在这个限制下是不能够灵活按需控制写操作的行为的,这个限制在类似控制 chunk 分块编码写逻辑、[SSE](https://github.com/hertz-contrib/sse#hertz-sse) 的场景下尤为明显
1212

13-
## 接口定义
13+
为了解决这个问题,Hertz 提供了一个叫做「Response Writer 劫持」扩展,它能够以正交的方式垂直打通分层架构所带来的写响应局限。让用户可以根据自己的需求在应用层自由的定制写响应的逻辑,提升框架易用性。
14+
15+
## 核心设计
16+
17+
### 接口定义
1418

1519
接口定义在 `pkg/network/writer`.
1620

@@ -27,28 +31,28 @@ type ExtWriter interface {
2731

2832
### 劫持 Response 的 Writer
2933

30-
Hertz 在 `app.RequestContext` 中提供了 `Response.HijackWriter` 方法让用户劫持 responsewriter.
34+
Hertz 在 `app.RequestContext` 中提供了 `Response.HijackWriter` 方法让用户劫持 ResponseWriter.
3135

3236
用法示例:
3337

3438
```go
3539
h.GET("/hijack", func(c context.Context, ctx *app.RequestContext) {
36-
// Hijack the writer of response
37-
ctx.Response.HijackWriter(yourResponseWriter)
40+
// Hijack the writer of Response
41+
ctx.Response.HijackWriter(**yourResponseWriter**)
3842
})
3943
```
4044

4145
## 已支持 Response 的 Writer 扩展
4246

43-
Hertz 在 `pkg/protocol/http1/resp/writer` 下提供了 `NewChunkedBodyWriter` 方法来创建一个 responsewriter,它允许用户在 handler 中立即刷新分块,用户也可以实现自己的 responsewriter
47+
- `ChunkedBodyWriter`Hertz 在 `pkg/protocol/http1/resp/writer` 下默认提供了 `NewChunkedBodyWriter` 方法来创建一个 ResponseWriter,它允许用户在 Handler 中立即刷新分块,用户也可以实现自己的 ResponseWriter
4448

4549
### ChunkedBodyWriter
4650

4751
用法示例:
4852

4953
```go
5054
h.GET("/flush/chunk", func(c context.Context, ctx *app.RequestContext) {
51-
// Hijack the writer of response
55+
// Hijack the writer of Response
5256
ctx.Response.HijackWriter(resp.NewChunkedBodyWriter(&ctx.Response, ctx.GetWriter()))
5357

5458
for i := 0; i < 10; i++ {

0 commit comments

Comments
 (0)