Skip to content

Commit 53a4879

Browse files
docs: create generic streaming documentation in Chinese version (#1326)
1 parent c50d9fe commit 53a4879

File tree

2 files changed

+397
-18
lines changed

2 files changed

+397
-18
lines changed

content/en/docs/kitex/Tutorials/advanced-feature/generic-call/generic_streaming.md

+68-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: ""
1414

1515
### Generic Streaming Client Initialization
1616

17-
#### protobuf
17+
#### Protobuf
1818

1919
Take the following Protobuf IDL as an example:
2020

@@ -42,19 +42,26 @@ service Echo {
4242

4343
The four methods included in the example IDL correspond to four scenarios:
4444

45-
1. Client streaming: the client sends multiple messages, the server returns one message, and then closes the stream.
46-
2. Server streaming: The client sends one message, the server returns multiple messages, and then closes the stream. It's suitable for LLM scenarios.
47-
3. Bidirectional streaming: The sending and receiving of client/server are independent, which can be organized in arbitrary order.
48-
4. Unary: non streaming.
45+
1. Client Streaming: the client sends multiple messages, the server returns one message, and then closes the stream.
46+
2. Server Streaming: The client sends one message, the server returns multiple messages, and then closes the stream. It's suitable for LLM scenarios.
47+
3. Bidirectional Streaming: The sending and receiving of client/server are independent, which can be organized in arbitrary order.
48+
4. Unary: In gRPC, this is a single call mode without using streams, similar to the Ping Pong mode in Thrift.
4949

5050
First of all, please initialize the streaming client. Here is an example of streaming client initialization.
5151

5252
```go
5353
import (
54-
dproto "github.com/cloudwego/dynamicgo/proto"
54+
"context"
55+
56+
"github.com/cloudwego/kitex/client"
57+
"github.com/cloudwego/kitex/client/genericclient"
58+
"github.com/cloudwego/kitex/pkg/generic"
59+
"github.com/cloudwego/kitex/pkg/generic/proto"
60+
"github.com/cloudwego/kitex/pkg/transmeta"
61+
"github.com/cloudwego/kitex/transport"
5562
)
5663

57-
dOpts := dproto.Options{} // you can specify parsing options as you want
64+
dOpts := proto.Options{} // you can specify parsing options as you want
5865
p, err := generic.NewPbFileProviderWithDynamicGo(your_idl, ctx, dOpts)
5966
// create json pb generic
6067
g, err := generic.JSONPbGeneric(p)
@@ -93,15 +100,25 @@ service TestService {
93100

94101
The four methods included in the example IDL correspond to four scenarios:
95102

96-
1. Client streaming: the client sends multiple messages, the server returns one message, and then closes the stream.
97-
2. Server streaming: The client sends one message, the server returns multiple messages, and then closes the stream. It's suitable for LLM scenarios.
98-
3. Bidirectional streaming: The sending and receiving of client/server are independent, which can be organized in arbitrary order.
99-
4. Unary (gRPC): Non-streaming. With `streaming.mode` annotation. Not recommended due to performance loss.
100-
5. Unary (KitexThrift): Non-streaming. Recommended.
103+
1. Client Streaming: the client sends multiple messages, the server returns one message, and then closes the stream.
104+
2. Server Streaming: The client sends one message, the server returns multiple messages, and then closes the stream. It's suitable for LLM scenarios.
105+
3. Bidirectional Streaming: The sending and receiving of client/server are independent, which can be organized in arbitrary order.
106+
4. Unary (gRPC): Non-streaming with `streaming.mode` annotation. Not recommended due to performance loss.
107+
5. Ping Pong mode (KitexThrift): Traditional Thrift request-response pattern without using streams. Better performance, recommended.
101108

102109
Here is an example of streaming client initialization.
103110

104111
```go
112+
import (
113+
"context"
114+
115+
"github.com/cloudwego/kitex/client"
116+
"github.com/cloudwego/kitex/client/genericclient"
117+
"github.com/cloudwego/kitex/pkg/generic"
118+
"github.com/cloudwego/kitex/pkg/transmeta"
119+
"github.com/cloudwego/kitex/transport"
120+
)
121+
105122
p, err := generic.NewThriftFileProvider(your_idl_path)
106123
/*
107124
// if you use dynamicgo
@@ -122,6 +139,14 @@ cli, err := genericclient.NewStreamingClient("destService", g,
122139
Example:
123140

124141
```go
142+
import (
143+
"context"
144+
"fmt"
145+
"time"
146+
147+
"github.com/cloudwego/kitex/client/genericclient"
148+
)
149+
125150
// initialize client streaming client using the streaming client you created
126151
streamCli, err := genericclient.NewClientStreaming(ctx, cli, "StreamRequestEcho")
127152
for i := 0; i < 3; i++ {
@@ -137,11 +162,20 @@ strResp, ok := resp.(string) // response is json string
137162

138163
### Server Streaming
139164

140-
Note: A non-nil error (including `io.EOF`) returned by `Recv` indicates that the server has finished sending (or encountered an error)
165+
Note: An `io.EOF` error returned by `Recv` indicates that the server has finished sending and normally closed the stream, while other non-nil errors indicate actual errors.
141166

142167
Example:
143168

144169
```go
170+
import (
171+
"context"
172+
"fmt"
173+
"io"
174+
175+
"github.com/cloudwego/kitex/client/genericclient"
176+
"github.com/cloudwego/kitex/pkg/klog"
177+
)
178+
145179
// initialize server streaming client using the streaming client you created, and send a message
146180
streamCli, err := genericclient.NewServerStreaming(ctx, cli, "StreamResponseEcho", `{"message": "grpc server streaming generic request"}`)
147181
for {
@@ -164,6 +198,16 @@ for {
164198
Example:
165199

166200
```go
201+
import (
202+
"context"
203+
"fmt"
204+
"io"
205+
"sync"
206+
207+
"github.com/cloudwego/kitex/client/genericclient"
208+
"github.com/cloudwego/kitex/pkg/klog"
209+
)
210+
167211
// initialize bidirectional streaming client using the streaming client you created
168212
streamCli, err := genericclient.NewBidirectionalStreaming(ctx, cli, "BidirectionalEcho")
169213

@@ -222,6 +266,12 @@ The usage of unary call is similar to normal (non-streaming) generic call.
222266
Example:
223267

224268
```go
269+
import (
270+
"context"
271+
272+
"github.com/cloudwego/kitex/client/genericclient"
273+
)
274+
225275
resp, err := cli.GenericCall(ctx, "UnaryEcho", `{"message": "unary request"}`)
226276
strResp, ok := resp.(string) // response is json string
227277
```
@@ -230,24 +280,24 @@ strResp, ok := resp.(string) // response is json string
230280

231281
### Recv() got err: rpc error: code = 12 desc = Method not found!
232282

233-
This error occurs when calling with Kitex **protobuf** generic streaming when the downstream is **gRPC-python** (gRPC libraries for other languages may also have this problem).
283+
This error occurs when calling with Kitex **Protobuf** generic streaming when the downstream is **gRPC-python** (gRPC libraries for other languages may also have this problem).
234284

235-
The root cause is that Kitex does not parse the package in the protobuf idl, so the package part of `:path` in the gPRC request is missing, and gRPC-python can't find the corresponding method.
285+
The root cause is that Kitex does not parse the package in the Protobuf IDL, so the package part of `:path` in the gPRC request is missing, and gRPC-python can't find the corresponding method.
236286

237287
e.g.
238288

239289
- normal client
240290

241291
`:path` - /search.gpt_engine.GPTStreamService/GPTGeneration
242292

243-
- protobuf generic client
293+
- Protobuf generic client
244294

245295
`:path` - /GPTStreamService/GPTGeneration
246296

247297
#### Solution
248298

249-
Use the following branch to solve it and wait for the official release of Kitex v1.18.1 to fix this issue.
299+
Use Kitex v0.13.1 or higher version to fix this issue. Kitex v0.13.1 was released in April 2025 ([See release notes](https://github.com/cloudwego/kitex/releases/tag/v0.13.1)):
250300

251301
```shell
252-
go get -u github.com/cloudwego/kitex@v0.12.1-0.20241220085925-b5894d2f9e0c
302+
go get -u github.com/cloudwego/kitex@v0.13.1
253303
```

0 commit comments

Comments
 (0)