上下文Context
上下文 context.Context
是Go语言中用来 设置截止日期、同步信号、传递请求相关值的结构体
。
1.1 context.Context接口定义的四个需要实现方法
- Deadline — 返回 context.Context 被取消的时间,也就是完成工作的截止日期;
- Done — 返回一个 Channel,这个 Channel 会在当前工作完成或者上下文被取消后关闭,多次调用 Done 方法会返回同一个 Channel;
- Err — 返回 context.Context 结束的原因,它只会在 Done 方法对应的 Channel 关闭时返回非空的值;
- 如果 context.Context 被取消,会返回 Canceled 错误;
- 如果 context.Context 超时,会返回 DeadlineExceeded 错误;
- Value — 从 context.Context 中获取键对应的值,对于同一个上下文来说,多次调用 Value 并传入相同的 Key 会返回相同的结果,该方法可以用来传递请求特定的数据;
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
1.2 使用 Context 同步信号
/*
使用 Context 同步信号
下面是一个代码片段:
创建了一个过期时间为 1s 的上下文,并向上下文传入 handle 函数,
然后该方法会使用 500ms 的时间处理传入的请求
*/
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// 异步
go handle(ctx, 500*time.Millisecond)
select {
case <-ctx.Done():
fmt.Println("main",ctx.Err())
}
}
func handle(ctx context.Context, duration time.Duration) {
select {
case <-ctx.Done():
fmt.Println("handle", ctx.Err())
case <-time.After(duration):
fmt.Println("process request with", duration)
}
}
运行结果:
process request with 500ms
main context deadline exceeded
- handle 函数没有进入超时的 select 分支,但是 main 函数的 select 却会等待 context.Context 超时并打印出 main context deadline exceeded
- 如果我们将处理请求时间增加至 1500ms,整个程序都会因为上下文的过期而被中止;
1.3 取消信号
context.WithCancel()
- context.WithCancel() 函数能够从 context.Context 中衍生出一个新的子上下文并返回用于取消该上下文的函数。
- 一旦执行返回的取消函数:当前上下文以及它的子上下文都会被取消;所有的 Goroutine 都会同步收到这一取消信号
- 调用的是 context.cancelCtx.cancel()
context.WithDeadline 和 context.WithTimeout
- 同时context.WithDeadline 和 context.WithTimeout 也都能创建可以被取消的计时器上下文 context.timerCtx。
- context.timerCtx.cancel 方法不仅调用了 context.cancelCtx.cancel,还会停止持有的定时器减少不必要的资源浪费)。