Skip to content

Commit bebcbaf

Browse files
committed
fix: weight and translate
1 parent b2210e3 commit bebcbaf

6 files changed

Lines changed: 79 additions & 51 deletions

File tree

content/en/docs/hertz/tutorials/basic-feature/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ keywords:
2323
"Constants",
2424
"Render",
2525
"JSON Marshal Library",
26+
"SSE",
27+
"http.Handler adaptor",
2628
]
2729
description: "Basic feature of Hertz."
2830
---

content/en/docs/hertz/tutorials/basic-feature/http-adaptor.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
---
2-
title: "Hertz http.Handler adaptor"
2+
title: "http.Handler adaptor"
33
date: 2025-05-21
4-
weight: 12
4+
weight: 20
55
keywords: ["adaptor"]
6-
description: "Hertz http.Handler adaptor"
6+
description: ""
77
---
88

99
## Background
10+
1011
- We will deprecate the Hertz's native file system
1112
- Provide an fs implementation that is both compatible and performs well, using the official net/http ecosystem.
1213
- Reduce the implementation of a large number of extensions by expanding the hertz function through the official net/http ecosystem
1314

1415
## What is adaptor.HertzHandler
16+
1517
- You are allowed to directly convert your existing http.HandlerFunc method into a HertzHandler.
1618
- You can directly use http.FileServer, embed.FS, and other official ecosystems
1719
- You can even use this directly at Hertz github.com/gorilla/websocket
1820

1921
## Example
22+
2023
```go
2124
package main
2225

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
---
22
title: "SSE"
33
date: 2025-05-21
4-
weight: 12
4+
weight: 19
55
keywords: ["SSE"]
6-
description: "Hertz Support SSE。"
6+
description: "SSE in Hertz"
77
---
88
```Code:https://github.com/cloudwego/hertz/tree/develop/pkg/protocol/sse```
99

1010
**Changelog**
11+
1112
- 2025/05/15 Has been released to the open-source version v0.10.0
1213
- 2025/05/13 [d3c68463] Added Get / SetLastEventID.
1314
- 2025/05/13 [3952e956] The Reader ForEach interface supports passing in ctx.
1415
- 2025/04/24 [cf38573ce] First version completed
1516

16-
## 什么是Server-Sent Events (SSE)
17+
## What is Server-Sent Events (SSE)
18+
1719
- SSE is an HTML standard used to describe the implementation of the EventSource API in browsers. Strictly speaking, it is not an HTTP protocol.
1820
- Both the popular Model Context Protocol (MCP) and Agent2Agent (A2A) protocols are based to some extent on SSE
19-
- https://www.anthropic.com/news/model-context-protocol
20-
- https://developers.googleblog.com/en/a2a-a-new-era-of-agent-interoperability/
21+
- https://www.anthropic.com/news/model-context-protocol
22+
- https://developers.googleblog.com/en/a2a-a-new-era-of-agent-interoperability/
2123

2224
## Server Implementation
25+
2326
### Return data
27+
2428
```go
2529
import "github.com/cloudwego/hertz/pkg/protocol/sse"
2630

@@ -31,73 +35,78 @@ func HandleSSE(ctx context.Context, c *app.RequestContext) {
3135
w.WriteEvent("id-x", "message", []byte("hello\n\nworld"))
3236
time.Sleep(10 * time.Millisecond)
3337
}
34-
w.Close() // 发送最后的chunk数据,确保优雅退出。可选,Hertz Handler 返回后会自动调用。
38+
w.Close() // Send the last chunk of data to ensure graceful exit. Optional, Hertz will automatically call this after the Handler returns.
3539

36-
// 请确保 writer 的生命周期和handler一致。不要go异步后台使用。
40+
// Please make sure that the lifecycle of the writer is consistent with that of the handler. Do not use it in asynchronous background.
3741
}
3842
```
3943

40-
4144
### FAQ
45+
4246
#### connection has been closed when flush
43-
Reason: The client connection has been disconnected, possibly due to a long time without response data. Please check the corresponding configuration.
4447

48+
Reason: The client connection has been disconnected, possibly due to a long time without response data. Please check the corresponding configuration.
4549

4650
## Client Implementation
4751

4852
### Initiate a request
53+
4954
It's exactly the same as making a normal HTTP request to Hertz.
5055
Note: The new Hertz version will automatically recognize SSE streams, so you don't need to explicitly set `WithResponseBodyStream(true)`
5156

5257
**Some optional headers**
58+
5359
```go
5460
import "github.com/cloudwego/hertz/pkg/protocol/sse"
5561

56-
sse.AddAcceptMIME(req) // 部分SSE Server可能会要求显式增加 Accept: text/event-stream
57-
sse.SetLastEventID(req, "id-123") // 对于有状态服务,需要通过 SetLastEventID 告诉 Server
62+
sse.AddAcceptMIME(req) // Some SSE Servers may require explicit addition of Accept: text/event-stream
63+
sse.SetLastEventID(req, "id-123") // For stateful services, you need to tell the Server via SetLastEventID
5864
```
5965

6066
### Handle response
67+
6168
```go
6269
import "github.com/cloudwego/hertz/pkg/protocol/sse"
6370

6471
func HandleSSE(ctx context.Context, resp *protocol.Response) error {
6572
r, err := sse.NewReader(resp)
6673
if err != nil {
67-
return err
74+
return err
6875
}
69-
70-
// 也可以手动调用 r.Read 方法
76+
77+
// You can also call the r.Read method manually
7178
err = r.ForEach(ctx, func(e *Event) error {
7279
println("Event:", e.String())
7380
return nil
7481
})
75-
if err != nil { // 如果 Server 正常断开,这里 err == nil,不会报错
76-
// 其他io错误 或 ctx cancelled
82+
if err != nil { // If the Server is disconnected normally, err == nil here, no error will be reported
83+
// Other io errors or ctx cancelled
7784
return err
7885
}
79-
println("Client LastEventID", r.LastEventID()) // 可用于保存最后接收的 Event ID
86+
println("Client LastEventID", r.LastEventID()) // Can be used to save the last received Event ID
8087
return nil
8188
}
8289
```
8390

8491
### FAQ
8592

8693
#### How to implement multi-level SSE/SSE Proxy?
94+
8795
- You can refer to the implementations on both the Client and Server sides simultaneously
8896
- Enable `WithSenseClientDisconnection` in the server and pass the context to the next hop's Reader ForEach
89-
- In this way, when the Client disconnects, it will be automatically recognized, and the entire stream will be interrupted
97+
- In this way, when the Client disconnects, it will be automatically recognized, and the entire stream will be interrupted
9098

9199
## Event Struct
100+
92101
```go
93102
// Event represents a Server-Sent Event (SSE).
94103
type Event struct {
95-
ID string // Event IDsse.Reader 会自动记录最后的 Event ID,可使用 LastEventID() 获取
96-
Type string // Event Type,常见的如 "message"
97-
Data []byte // 为了方便使用 Unmarshal/Marshal,这里使用 []byte,但是按spec这个字段必须要 utf8 string
104+
ID string // Event ID, sse.Reader will automatically record the last Event ID, which can be obtained using LastEventID()
105+
Type string // Event Type, such as "message"
106+
Data []byte // []byte is used here for the convenience of Unmarshal/Marshal, but according to spec, this field must be utf8 string
98107

99-
// 不建议使用,主要是针对浏览器的 SourceEvent 返回该字段控制其重试策略。
100-
// 如果使用Hertz作为Client可以参考:https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/retry/
108+
// Not recommended, mainly for the browser's SourceEvent to return this field to control its retry strategy.
109+
// If you use Hertz as a client, you can refer to: https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/retry/
101110
Retry time.Duration
102111
}
103112
```

content/zh/docs/hertz/tutorials/basic-feature/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ keywords:
2323
"常量",
2424
"渲染",
2525
"JSON Marshal 库",
26+
"SSE",
27+
"http.Handler adaptor",
2628
]
2729
description: "Hertz 基本特性。"
2830
---

content/zh/docs/hertz/tutorials/basic-feature/http-adaptor.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
---
2-
title: "Hertz http.Handler adaptor"
2+
title: "http.Handler adaptor"
33
date: 2025-05-21
4-
weight: 12
4+
weight: 20
55
keywords: ["adaptor"]
6-
description: "Hertz http.Handler adaptor"
6+
description: ""
77
---
88

99
## 背景
10-
- 主淘汰长期没有良好维护的 hertz 原生 fs 实现;
10+
11+
- 淘汰长期没有良好维护的 hertz 原生 fs 实现;
1112
- 通过官方 net/http 生态提供一个兼容性较好,性能也不差的 fs 实现。
1213
- 通过官方 net/http 生态扩展 hertz 功能,减少大量扩展实现
1314

1415
## 什么是 adaptor.HertzHandler
16+
1517
- 允许你将现有的 http.HandlerFunc 方法直接转为 HertzHandler
1618
- 可以直接使用 http.FileServer embed.FS 等官方生态
1719
- 甚至可以直接在 Hertz 使用 github.com/gorilla/websocket
1820

1921
## 具体例子
22+
2023
```go
2124
package main
2225

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
---
22
title: "SSE"
33
date: 2025-05-21
4-
weight: 12
4+
weight: 19
55
keywords: ["SSE"]
6-
description: "Hertz 提供的 SSE 能力"
6+
description: "Hertz 提供的 SSE 能力"
77
---
88
```代码:https://github.com/cloudwego/hertz/tree/develop/pkg/protocol/sse```
99

1010
**Changelog**
11+
1112
- 2025/05/15 已发布到开源版本 v0.10.0
1213
- 2025/05/13 [d3c68463] 增加 Get / SetLastEventID
1314
- 2025/05/13 [3952e956] Reader ForEach 接口支持传入 ctx
1415
- 2025/04/24 [cf38573ce] 第一版本完成
1516

16-
## 什么是Server-Sent Events (SSE)
17-
- SSE是一个HTML标准,用于描述浏览器 EventSource API 的实现。严格说,不是http协议。
17+
## 什么是 Server-Sent Events (SSE)
18+
19+
- SSE 是一个 HTML 标准,用于描述浏览器 EventSource API 的实现。严格说,不是 http 协议。
1820
- 流行的 Model Context Protocol (MCP) 或 Agent2Agent (A2A) 协议都一定程度基于 SSE
19-
- https://www.anthropic.com/news/model-context-protocol
20-
- https://developers.googleblog.com/en/a2a-a-new-era-of-agent-interoperability/
21+
- https://www.anthropic.com/news/model-context-protocol
22+
- https://developers.googleblog.com/en/a2a-a-new-era-of-agent-interoperability/
23+
24+
## Server 端实现
2125

22-
## Server端实现
2326
### 返回数据
27+
2428
```go
2529
import "github.com/cloudwego/hertz/pkg/protocol/sse"
2630

@@ -31,34 +35,37 @@ func HandleSSE(ctx context.Context, c *app.RequestContext) {
3135
w.WriteEvent("id-x", "message", []byte("hello\n\nworld"))
3236
time.Sleep(10 * time.Millisecond)
3337
}
34-
w.Close() // 发送最后的chunk数据,确保优雅退出。可选,Hertz 在 Handler 返回后会自动调用。
38+
w.Close() // 发送最后的 chunk 数据,确保优雅退出。可选,Hertz 在 Handler 返回后会自动调用。
3539

36-
// 请确保 writer 的生命周期和handler一致。不要go异步后台使用
40+
// 请确保 writer 的生命周期和 handler 一致。不要 go 异步后台使用
3741
}
3842
```
3943

40-
4144
### 常见问题
45+
4246
#### connection has been closed when flush
43-
原因:
44-
Client连接断开,可能是太久没有响应数据,请检查相应配置。
4547

48+
原因:
49+
Client 连接断开,可能是太久没有响应数据,请检查相应配置。
4650

47-
## Client端实现
51+
## Client 端实现
4852

4953
### 发起请求
50-
与正常 Hertz 发起一个普通的http请求完全一致。
51-
注意:新hertz版本会自动识别SSE流,不用显式设置 WithResponseBodyStream(true)
54+
55+
与正常 Hertz 发起一个普通的 http 请求完全一致。
56+
注意:新 hertz 版本会自动识别 SSE 流,不用显式设置 WithResponseBodyStream(true)
5257

5358
**部分可选 Header**
59+
5460
```go
5561
import "github.com/cloudwego/hertz/pkg/protocol/sse"
5662

57-
sse.AddAcceptMIME(req) // 部分SSE Server可能会要求显式增加 Accept: text/event-stream
63+
sse.AddAcceptMIME(req) // 部分 SSE Server 可能会要求显式增加 Accept: text/event-stream
5864
sse.SetLastEventID(req, "id-123") // 对于有状态服务,需要通过 SetLastEventID 告诉 Server
5965
```
6066

6167
### 处理返回
68+
6269
```go
6370
import "github.com/cloudwego/hertz/pkg/protocol/sse"
6471

@@ -74,7 +81,7 @@ func HandleSSE(ctx context.Context, resp *protocol.Response) error {
7481
return nil
7582
})
7683
if err != nil { // 如果 Server 正常断开,这里 err == nil,不会报错
77-
// 其他io错误 或 ctx cancelled
84+
// 其他 io 错误 或 ctx cancelled
7885
return err
7986
}
8087
println("Client LastEventID", r.LastEventID()) // 可用于保存最后接收的 Event ID
@@ -85,20 +92,22 @@ func HandleSSE(ctx context.Context, resp *protocol.Response) error {
8592
### 常见问题
8693

8794
#### 如何实现多级的 SSE / SSE Proxy?
88-
- 可以同时参考 Client 端和 Server端的实现
95+
96+
- 可以同时参考 Client 端和 Server 端的实现
8997
- Server 启用 `WithSenseClientDisconnection` 并且把 context 传入 下一跳的 Reader ForEach
90-
- 这样当Client断开时,会自动识别,并且中断整个流
98+
- 这样当 Client 断开时,会自动识别,并且中断整个流
9199

92100
## Event 结构体
101+
93102
```go
94103
// Event represents a Server-Sent Event (SSE).
95104
type Event struct {
96105
ID string // 即 Event ID,sse.Reader 会自动记录最后的 Event ID,可使用 LastEventID() 获取
97106
Type string // 即 Event Type,常见的如 "message"
98-
Data []byte // 为了方便使用 Unmarshal/Marshal,这里使用 []byte,但是按spec这个字段必须要 utf8 string
107+
Data []byte // 为了方便使用 Unmarshal/Marshal,这里使用 []byte,但是按 spec 这个字段必须要 utf8 string
99108

100109
// 不建议使用,主要是针对浏览器的 SourceEvent 返回该字段控制其重试策略。
101-
// 如果使用Hertz作为Client可以参考:https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/retry/
110+
// 如果使用 Hertz 作为 Client 可以参考:https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/retry/
102111
Retry time.Duration
103112
}
104113
```

0 commit comments

Comments
 (0)