Skip to content

Commit 38f4965

Browse files
alice-yydsAsterDY
andauthored
doc: sync Goroutine-Local-Storage 功能使用 (cloudwego#862)
Co-authored-by: Yi Duan <[email protected]>
1 parent 281f385 commit 38f4965

File tree

2 files changed

+117
-0
lines changed
  • content
    • en/docs/kitex/Tutorials/advanced-feature
    • zh/docs/kitex/Tutorials/advanced-feature

2 files changed

+117
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "GLS Feature Usage"
3+
date: 2023-11-29
4+
weight: 11
5+
keywords: ["GLS","context"]
6+
description: "Goroutine local storage for implicitly pass context"
7+
---
8+
9+
## Server side enable request context backup
10+
11+
- Option on
12+
- Use the server option `WithContextBackup`;
13+
- The first parameter `enable` indicates that the GLS is enabled;
14+
- The second option, `async`, means to enable asynchronous implicitly pass-through (indicating that the context in the asynchronous call to go func () is also transparent fallback)
15+
16+
```go
17+
svr := xxx.NewServer(new(XXXImpl), server.WithContextBackup(true, true))
18+
19+
```
20+
21+
- Adjust localsession [management options](https://github.com/cloudwego/localsession/blob/main/manager.go#L24) by environment variables
22+
- First, enable `WithContextBackup` on the Server side.
23+
- Configure `CLOUDWEGO_SESSION_CONFIG_KEY ``= [{Whether to enable asynchronous pass-through}] [, {Global sharding number}] [, {GC interval}] in environment variables `, all three options are optional, **null means use default value**
24+
- Ex: `true,10,1h ` means, turn on asynchronous + sharding 10 buckets + 1 hour GC interval
25+
26+
## Client start request context fallback
27+
28+
- Option on
29+
- Use the client option `WithContextBackup`;
30+
- The parameter handler represents the backup logic BackupHandler customized by the business.
31+
32+
```go
33+
func(prev, cur context.Context) (ctx context.Context, backup bool)
34+
35+
```
36+
37+
- `Prev` parameter represents the context of the backup
38+
- `Cur` parameter represents the context obtained by the current client
39+
- `Ctx` return value represents the final context where the user completes processing
40+
- `Backup` return value indicates whether to continue localsession [built-in fallback backup ](https://github.com/cloudwego/localsession/blob/main/backup/metainfo.go#L54), mainly metainfo Persistent KVS pass-through at present
41+
42+
```go
43+
var expectedKey interface{}
44+
cli := xxx.NewClient(serverName, client.WithContextBackup(func(prev, cur context.Context) (ctx context.Context, backup bool) {
45+
if v := cur.Value(expectedKey); v != nil {
46+
// expectedKey exists, no need for recover context
47+
return cur, false
48+
}
49+
// expectedKey doesn't exists, need recover context from prev
50+
ctx = context.WithValue(cur, expectedKey, prev.Value(expectedKey))
51+
return ctx, true
52+
})
53+
54+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: "Goroutine-Local-Storage 功能使用"
3+
date: 2023-11-29
4+
weight: 11
5+
keywords: ["GLS","上下文"]
6+
description: "协程上下文隐式传递"
7+
---
8+
9+
## Introduction
10+
11+
GLS 用于存储 goroutine 内的上下文信息,作用类似于 context。相比 context 有如下优势:
12+
13+
1. **不需要显式传递**,在任意函数位置都可调用 CurSession() 获取上下文(如果有)
14+
2. **可以在父子****协程****间传递**(需要开启选项)
15+
16+
框架目前主要使用 GLS 进行 context 备份,以避免用户误传 context 导致链路透传信息(如 logid、metainfo 等)丢失
17+
18+
## Server 端开启请求 context 备份
19+
20+
- 选项开启
21+
- 使用 server option WithContextBackup;
22+
- 第一个参数 enable 表示开启备份功能;
23+
- 第二个选项 async 表示开启异步隐式透传(表示对 go func() 异步调用中的 context 也进行透明兜底)
24+
25+
```go
26+
svr := xxx.NewServer(new(XXXImpl), server.WithContextBackup(true, true))
27+
28+
```
29+
30+
- 环境变量调整 localsession [管理选项](https://github.com/cloudwego/localsession/blob/main/manager.go#L24)
31+
1. 首先在【Server 端开启 WithContextBackup】
32+
2. 环境变量中配置 `CLOUDWEGO_SESSION_CONFIG_KEY``=[{是否开启异步透传}][,{全局分片数}][,{GC间隔}]`,三个选项都为可选,**空表示使用默认值**
33+
1. ex: `true,10,1h ` 表示 开启异步 + 分片 10+1 小时 GC 间隔
34+
35+
## Client 端开启请求 context 兜底
36+
37+
- 选项开启
38+
- 使用 client option `client.``WithContextBackup``(handler)`
39+
- 参数 handler 表示业务自定义的备份逻辑 BackupHandler,其签名为
40+
41+
```go
42+
func(prev, cur context.Context) (ctx context.Context, backup bool)
43+
44+
```
45+
46+
- prev 参数表示备份的 context
47+
- cur 参数表示当前 client 拿到的 context
48+
- ctx 返回值表示用户处理完成的最终 context
49+
- backup 返回值表示是否继续进行 localsession[ 内置的兜底备份](https://github.com/cloudwego/localsession/blob/main/backup/metainfo.go#L54),当前主要是 metainfo Persistent KVS 的透传
50+
51+
```go
52+
var expectedKey interface{}
53+
cli := client.New(serverName, client.WithContextBackup(func(prev, cur context.Context) (ctx context.Context, backup bool) {
54+
if v := cur.Value(expectedKey); v != nil {
55+
// expectedKey exists, no need for recover context
56+
return cur, false
57+
}
58+
// expectedKey doesn't exists, need recover context from prev
59+
ctx = context.WithValue(cur, expectedKey, prev.Value(expectedKey))
60+
return ctx, true
61+
})
62+
63+
```

0 commit comments

Comments
 (0)