Skip to content

Commit

Permalink
doc: upload file Goroutine-Local-Storage 功能使用
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-yyds committed Nov 29, 2023
1 parent 5b28e2a commit 792c449
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions content/zh/docs/kitex/Tutorials/advanced-feature/GLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,37 @@ GLS 用于存储 goroutine 内的上下文信息,作用类似于 context。相
- 第一个参数 enable 表示开启备份功能;
- 第二个选项 async 表示开启异步隐式透传(表示对 go func() 异步调用中的 context 也进行透明兜底)

```go
```

```go
svr := xxx.NewServer(new(XXXImpl), server.WithContextBackup(true, true))

```

- 环境变量调整 localsession [管理选项](https://github.com/cloudwego/localsession/blob/main/manager.go#L24)
1. 首先在【Server 端开启WithContextBackup
2. 环境变量中配置 `CLOUDWEGO_SESSION_CONFIG_KEY``=[{是否开启异步透传}][,{全局分片数}][,{GC间隔}]`,三个选项都为可选,**空表示使用默认值**
1. ex: `true,10,1h `表示 开启异步+分片10+1小时GC间隔
1. 首先在【Server 端开启 WithContextBackup
2. 环境变量中配置 `CLOUDWEGO_SESSION_CONFIG_KEY``=[{是否开启异步透传}][,{全局分片数}][,{GC间隔}]`,三个选项都为可选,**空表示使用默认值**
1. ex: `true,10,1h ` 表示 开启异步 + 分片 10+1 小时 GC 间隔

## Client 端开启请求 context 兜底

- 选项开启
- 使用 client option `client.``WithContextBackup``(handler)`;
- 参数 handler 表示业务自定义的备份逻辑 BackupHandler,其签名为
```go
func(prev, cur context.Context) (ctx context.Context, backup bool)
- 使用 client option `client.``WithContextBackup``(handler)`
- 参数 handler 表示业务自定义的备份逻辑 BackupHandler,其签名为

```
```go
func(prev, cur context.Context) (ctx context.Context, backup bool)

```
- `p``rev` 参数表示备份的 context
- `c``ur` 参数表示当前 client 拿到的 context
- `c``tx` 返回值表示用户处理完成的最终 context
- `b``ackup` 返回值表示是否继续进行 localsession[ 内置的兜底备份](https://github.com/cloudwego/localsession/blob/main/backup/metainfo.go#L54),当前主要是 metainfo Persistent KVS 的透传
```go
```

```go
var expectedKey interface{}
cli := client.New(serverName, client.WithContextBackup(func(prev, cur context.Context) (ctx context.Context, backup bool) {
if v := cur.Value(expectedKey); v != nil {
// expectedKey exists, no need for recover context
return cur, false
}
// expectedKey doesn't exists, need recover context from prev
ctx = context.WithValue(cur, expectedKey, prev.Value(expectedKey))
return ctx, true
if v := cur.Value(expectedKey); v != nil {
// expectedKey exists, no need for recover context
return cur, false
}
// expectedKey doesn't exists, need recover context from prev
ctx = context.WithValue(cur, expectedKey, prev.Value(expectedKey))
return ctx, true
})

```
```

0 comments on commit 792c449

Please sign in to comment.