欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > go context 源码刨析(一)

go context 源码刨析(一)

2025/4/19 22:21:41 来源:https://blog.csdn.net/2401_82869454/article/details/139651248  浏览:    关键词:go context 源码刨析(一)

Context

上下文context.Context 是用来设置截止时间、同步信号,传递请求相关值的结构体。

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() errorValue(key any) any
}

设计原理

在 Goroutine 构成的树形结构中对信号进行同步以减少计算资源的浪费是 context.Context 的最大作用。Go 服务的每一个请求都是通过单独的 Goroutine 处理的,HTTP/RPC 请求的处理器会启动新的 Goroutine 访问数据库和其他服务。

如下图所示(图片来自《go设计与实现》),我们可能会创建多个 Goroutine 来处理一次请求,而 context.Context 的作用是在不同 Goroutine 之间同步请求特定数据、取消信号以及处理请求的截止日期。
图片来自面向信仰编程
下层 goroutine 通过监听上层 goroutine context 的 Done 方法,同步取消。

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)}
}

默认上下文

context 包中有两个默认的 context, context.Backgroundcontext.TODO,这两个方法都会返回预先初始化好的私有变量 background 和 todo,它们会在同一个 Go 程序中被复用。

这两个私有变量都是通过 new(emptyCtx) 语句初始化的,它们是指向私有结构体 context.emptyCtx 的指针,这是最简单、最常用的上下文类型。

type emptyCtx int
var (background = new(emptyCtx)todo       = new(emptyCtx)
)
func Background() Context {return background
}
func TODO() Context {return todo
}
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {return
}
func (*emptyCtx) Done() <-chan struct{} {return nil
}
func (*emptyCtx) Err() error {return nil
}
func (*emptyCtx) Value(key any) any {return nil
}

context.Backgroundcontext.TODO 没有差别,只是在使用和语义上不同:

  • context.Background 是上下文的默认值,所有其他的上下文都应该从它衍生出来。
  • context.TODO 应该仅在不确定应该使用哪种上下文时使用。

这两个是顶级Context,其他内置Context都是由它们派生出来的。

取消

context.WithCancel 函数能够从 context.Context 中衍生出一个新的子上下文并返回用于取消该上下文的函数。一旦我们执行返回的取消函数,当前上下文以及它的子上下文都会被取消,所有的 Goroutine 都会同步收到这一取消信号。
图片来自《go设计与实现》
在这里插入图片描述

func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {if parent == nil {panic("cannot create context from nil parent")}c := newCancelCtx(parent)propagateCancel(parent, &c)return &c, func() { c.cancel(true, Canceled) }
}
  • context.newCancelCtx 将传入的上下文包装成私有结构体 context.cancelCtx。
  • context.propagateCancel 会构建父子上下文之间的关联,当父上下文被取消时,子上下文也会被取消。

重点是 propagateCancel:

func propagateCancel(parent Context, child canceler) {done := parent.Done()if done == nil {return // parent is never canceled}select {case <-done:// parent is already canceledchild.cancel(false, parent.Err())returndefault:}if p, ok := parentCancelCtx(parent); ok {p.mu.Lock()if p.err != nil {// parent has already been canceledchild.cancel(false, p.err)} else {if p.children == nil {p.children = make(map[canceler]struct{})}p.children[child] = struct{}{}}p.mu.Unlock()} else {atomic.AddInt32(&goroutines, +1)go func() {select {case <-parent.Done():child.cancel(false, parent.Err())case <-child.Done():}}()}
}

propagateCancel 与父上下文相关的三种不同情况:

  • 当 parent.Done() == nil,也就是 parent 不会触发取消事件时,当前函数会直接返回。
  • 当 child 的继承链包含可以取消的上下文时,会判断 parent 是否已经触发了取消信号。
    • 如果已经被取消,child 会立刻被取消;
    • 如果没有被取消,child 会被加入 parent 的 children 列表中,等待 parent 释放取消信号。children列表是一个map。
  • 当父上下文是开发者自定义的类型、实现了 context.Context 接口并在 Done() 方法中返回了非空的管道时。
    • 运行一个新的 Goroutine 同时监听 parent.Done() 和 child.Done() 两个 Channel。
    • 在 parent.Done() 关闭时调用 child.cancel 取消子上下文。

context.cancelCtx 实现了了 context.cancelCtx.cancel,该方法会关闭上下文中的 Channel 并向所有的子上下文同步取消信号。

func (c *cancelCtx) cancel(removeFromParent bool, err error) {if err == nil {panic("context: internal error: missing cancel error")}c.mu.Lock()if c.err != nil {c.mu.Unlock()return // already canceled}c.err = errd, _ := c.done.Load().(chan struct{})if d == nil {c.done.Store(closedchan)} else {close(d)}for child := range c.children {// NOTE: acquiring the child's lock while holding parent's lock.child.cancel(false, err)}c.children = nilc.mu.Unlock()if removeFromParent {removeChild(c.Context, c)}
}

cancel 会关闭上下文中的 Channel,同时会遍历children,同步取消所有子上下文。如果 removeFromParent 为 true,会把上下文从父上下文去除。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词