Skip to content

Commit 2e04c7b

Browse files
authored
doc: sync Stream Middleware (#1192)
1 parent 1722cf9 commit 2e04c7b

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: "Stream Middleware"
3+
date: 2025-01-13
4+
weight: 1
5+
keywords: ["Stream Middleware"]
6+
description: ""
7+
---
8+
9+
## Middleware Type
10+
11+
### Stream Recv/Send Middleware
12+
13+
**Trigger timing** : Called when streaming messages
14+
15+
#### Type definition
16+
17+
```go
18+
type StreamRecvEndpoint func(ctx context.Context, stream Stream, res any) (err error)
19+
type StreamSendEndpoint func(ctx context.Context, stream Stream, req any) (err error)
20+
21+
type StreamRecvMiddleware func(next StreamRecvEndpoint) StreamRecvEndpoint
22+
type StreamSendMiddleware func(next StreamSendEndpoint) StreamSendEndpoint
23+
```
24+
25+
**Parameter description** :
26+
27+
- Directly obtain the current stream object
28+
- Res/req both represent real requests and responses.
29+
- Behavior before and after calling the Next function:
30+
31+
| Middleware type | Before calling Next | After calling Next |
32+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
33+
| StreamRecvMiddleware | - The data is not really collected, just called the stream. Recv () function.<br><br>- Res parameter is empty | - Data received or encountered an error<br><br>- The res parameter has a real value |
34+
| StreamSendMiddleware | - The data was not actually sent, just called the stream.Send () function<br><br>- The req parameter is a real request | - Data transmission completed or encountered an error<br><br>- The req parameter is a real request |
35+
36+
#### Examples
37+
38+
**Usage scenario** : Inject relevant business logic when the stream receives/sends messages.
39+
40+
```go
41+
svr, err := xxx.NewServer(
42+
//...
43+
streamxserver.WithStreamRecvMiddleware(func(next streamx.StreamRecvEndpoint) streamx.StreamRecvEndpoint {
44+
return func(ctx context.Context, stream streamx.Stream, res any) (err error) {
45+
// ctx has user's token
46+
token, ok := metainfo.GetPersistentValue(ctx, "user_token")
47+
// check token balance
48+
if !hasBalance(token) {
49+
return fmt.Errorf("user dont have enough balance: token=%s", token)
50+
}
51+
return next(ctx, stream, res)
52+
}
53+
}),
54+
)
55+
```
56+
57+
## Inject Middleware
58+
59+
#### Inject Client Middleware
60+
61+
```go
62+
cli, err := xxx.NewClient(
63+
"a.b.c",
64+
streamxclient.WithStreamRecvMiddleware(func(next streamx.StreamRecvEndpoint) streamx.StreamRecvEndpoint {
65+
return func(ctx context.Context, stream streamx.Stream, res any) (err error) {
66+
return next(ctx, stream, res)
67+
}
68+
}),
69+
streamxclient.WithStreamSendMiddleware(func(next streamx.StreamSendEndpoint) streamx.StreamSendEndpoint {
70+
return func(ctx context.Context, stream streamx.Stream, req any) (err error) {
71+
return next(ctx, stream, req)
72+
}
73+
}),
74+
)
75+
```
76+
77+
#### Inject Server Middleware
78+
79+
```go
80+
server, err := xxx.NewServer(
81+
// ....
82+
streamxserver.WithStreamRecvMiddleware(func(next streamx.StreamRecvEndpoint) streamx.StreamRecvEndpoint {
83+
return func(ctx context.Context, stream streamx.Stream, res any) (err error) {
84+
return next(ctx, stream, res)
85+
}
86+
}),
87+
streamxserver.WithStreamSendMiddleware(func(next streamx.StreamSendEndpoint) streamx.StreamSendEndpoint {
88+
return func(ctx context.Context, stream streamx.Stream, req any) (err error) {
89+
return next(ctx, stream, req)
90+
}
91+
}),
92+
)
93+
```

0 commit comments

Comments
 (0)