go 上下文:context.Context
go 上下文:context.Context
Go语言中的上下文(Context)是一种用于在 Goroutines 之间传递取消信号、截止时间和其他请求范围值的标准方式。context 包提供了 Context 类型和一些相关的函数,用于在并发程序中有效地传递上下文信息。
在Go语言中,上下文通常用于以下场景:
1.请求的传递:当一个请求从客户端发送到服务器时,可以使用上下文来携带与该请求相关的数据。这些数据可以是用户的身份信息、请求的元数据或其他与请求相关的信息。通过将上下文传递给处理该请求的goroutine,可以确保在整个处理过程中访问这些数据。2.取消操作:上下文可以用于取消正在进行的操作。当用户或其他代码发送取消信号时,可以将该信号传递给正在执行操作的goroutine。goroutine在接收到取消信号后,可以根据需要执行清理操作并退出。3.截止时间:有时候需要在一段时间后终止正在进行的操作。通过将截止时间与上下文一起传递给goroutine,可以确保在超过截止时间后执行适当的清理操作并退出。4.跨多个服务通信:当在分布式系统中使用Go语言时,上下文可以用于跨不同的服务之间传递请求数据、取消信号和截止时间。通过使用上下文,可以确保在整个系统中的各个服务之间保持一致的上下文和请求生命周期管理。
通过使用上下文,可以有效地在 Goroutines 之间传递取消信号、截止时间和请求范围的值,从而更好地控制并发程序的行为。
1. context.Context 接口
Context 接口定义了在 Goroutines 之间传递的上下文的基本方法:
type Context interface {Deadline() (deadline time.Time, ok bool)Done() <-chan struct{}Err() errorValue(key interface{}) interface{}}
•Deadline():返回上下文的截止时间。如果存在截止时间,ok 为 true,否则为 false。•Done():返回一个通道,该通道关闭时表示上下文被取消或者超过了截止时间。•Err():返回上下文取消的原因。如果上下文没有被取消,则返回 nil。•Value(key):返回与给定 key 关联的值。这允许在上下文中传递请求范围的数据。
2. 创建上下文
在 Go 中,上下文可以通过 context.Background() 创建,它是一个无值的上下文,通常用作根上下文。根上下文不能被取消,也不能传递截止时间。
ctx := context.Background()
可以使用 context.WithCancel、context.WithTimeout、context.WithDeadline 和 context.WithValue 等函数创建派生上下文,这些函数分别用于创建带有取消、超时、截止时间和值的上下文。
// 创建一个带有取消功能的上下文ctx, cancel := context.WithCancel(context.Background())// 创建一个带有超时的上下文ctx, cancel := context.WithTimeout(context.Background(), time.Second)// 创建一个带有截止时间的上下文deadline := time.Now().Add(2 * time.Second)ctx, cancel := context.WithDeadline(context.Background(), deadline)// 创建一个带有值的上下文key := "key"value := "value"ctx := context.WithValue(context.Background(), key, value)
3. 传递上下文
在 Go 中,通过函数参数将上下文传递给调用的函数,从而使调用的函数能够感知上下文的取消或超时。例如:
func myFunction(ctx context.Context) {// 在这里使用 ctx 处理逻辑select {case <-ctx.Done():// 上下文被取消,执行清理工作fmt.Println("Context canceled")returndefault:// 继续正常的逻辑fmt.Println("Doing some work")}}func main() {// 创建带有取消功能的上下文ctx, cancel := context.WithCancel(context.Background())defer cancel()// 启动 Goroutine,传递上下文go myFunction(ctx)// 主 Goroutine 执行一些工作time.Sleep(2 * time.Second)}
4. 上下文的取消
调用 cancel() 函数会取消与上下文相关的 Goroutines。一旦上下文被取消,与之关联的所有 Goroutines 都会收到取消信号。
ctx, cancel := context.WithCancel(context.Background())// 启动 Goroutine,传递上下文go func(ctx context.Context) {select {case <-ctx.Done():// 上下文被取消,执行清理工作fmt.Println("Context canceled")return}}(ctx)// 取消上下文cancel()
5. 上下文的超时和截止时间
使用 context.WithTimeout 或 context.WithDeadline 函数可以设置上下文的超时或截止时间。当超过指定的时间后,上下文会自动取消。
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)defer cancel()// 启动 Goroutine,传递上下文go func(ctx context.Context) {select {case <-ctx.Done():// 上下文超时,执行清理工作fmt.Println("Context timeout")return}}(ctx)// 主 Goroutine 执行一些工作time.Sleep(3 * time.Second)
6. 上下文值
context.WithValue 函数可以用于在上下文中传递请求范围的值。这些值可以通过 context.Value 方法在上下文中检索。
ctx := context.WithValue(context.Background(), "user", "john_doe")// 从上下文中获取值value := ctx.Value("user")fmt.Println(value) // 输出: john_doe
7. 上下文的链式调用
可以通过链式调用的方式,将多个上下文进行组合,形成一个父子关系的上下文链。
parentCtx := context.Background()ctx1, cancel1 := context.WithTimeout(parentCtx, 2*time.Second)defer cancel1()ctx2, cancel2 := context.WithCancel(ctx1)defer cancel2()
上述的 ctx2 是 ctx1 的子上下文,当 ctx1 超时或被取消时,ctx2 也会相应地被取消。
声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[1]进行许可,使用时请注明出处。
Author: mengbin[2]
blog: mengbin[3]
Github: mengbin92[4]
cnblogs: 恋水无意[5]
References
[1] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh[2] mengbin: mengbin1992@outlook.com[3] mengbin: https://mengbin.top[4] mengbin92: https://mengbin92.github.io/[5] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/

浙公网安备 33010602011771号