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() error
        Value(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 canceled
                child.cancel(false, parent.Err())
                return
        default:
        }
        if p, ok := parentCancelCtx(parent); ok {
                p.mu.Lock()
                if p.err != nil {
                        // parent has already been canceled
                        child.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 = err
        d, _ := 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 = nil
        c.mu.Unlock()
        if removeFromParent {
                removeChild(c.Context, c)
        }
}

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

相关推荐

  1. Nginx内存池

    2024-06-14 09:16:02       13 阅读
  2. Nginx线程池

    2024-06-14 09:16:02       8 阅读
  3. Kafka 生产者投递内存池

    2024-06-14 09:16:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 09:16:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 09:16:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 09:16:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 09:16:02       18 阅读

热门阅读

  1. Webrtc支持FFMPEG硬解码之解码实现(三)

    2024-06-14 09:16:02       10 阅读
  2. ### RabbitMQ五种工作模式:

    2024-06-14 09:16:02       9 阅读
  3. 【设计模式】结构型设计模式之 桥接模式

    2024-06-14 09:16:02       7 阅读
  4. 威胁情报多场景下的实战技术落地

    2024-06-14 09:16:02       8 阅读
  5. Hudi extraMetadata 研究总结

    2024-06-14 09:16:02       7 阅读
  6. 大语言模型学习笔记-1

    2024-06-14 09:16:02       6 阅读
  7. MySQL CDC

    2024-06-14 09:16:02       8 阅读
  8. 璨与序列 题解(stl,dfs)

    2024-06-14 09:16:02       7 阅读
  9. 后端主流框架--Spring

    2024-06-14 09:16:02       5 阅读
  10. 响应式网页开发方法与实践

    2024-06-14 09:16:02       6 阅读
  11. 602. 好友申请 II :谁有最多的好友

    2024-06-14 09:16:02       6 阅读
  12. AI学习指南机器学习篇-支持向量机模型评估

    2024-06-14 09:16:02       8 阅读