TrumanWong

上下文Context

TrumanWong
3/6/2022

context.Context其实是一个接口,提供了以下4种方法:

type Context interface {
    Deadline() (deadline time.Time, ok bool)
    Done() <-chan struct{}
    Err() error
    Value(key interface{}) interface{}
}
  • Deadline方法的第一个返回值表示还有多久到期,第二个返回值表示是否到期。
  • Done是使用最频繁的方法,其返回一个通道,一般的做法是监听该通道的信号,如果收到信号则表示通道已经关闭,需要执行退出。
  • 如果通道已经关闭,则Err返回退出的原因。
  • value方法返回指定key对应的value,这是context携带的值。

context中携带值是非常少见的,其一般在跨程序的API中使用,并且该值的作用域在结束时终结。key必须是访问安全的,因为可能有多个协程同时访问它。一种常见的策略是在context中存储授权相关的值,这些鉴权不会影响程序的核心逻辑。

设计原理

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

如下图所示,我们可能会创建多个Goroutine来处理一次请求,而context.Context的作用是在不同Goroutine之间同步请求特定数据、取消信号以及处理请求的截止日期。

TrumanWong

每一个context.Context都会从最顶层的Goroutine一层一层传递到最下层。context.Context可以在上层Goroutine执行出现错误时,将信号及时同步给下层。

TrumanWong

如上图所示,当最上层的Goroutine因为某些原因执行失败时,下层的Goroutine由于没有接收到这个信号所以会继续工作;但是当我们正确地使用context.Context时,就可以在下层及时停掉无用的工作以减少额外的资源消耗:

TrumanWong

我们可以通过一个代码片段了解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, d time.Duration) {
    select {
    case <-ctx.Done():
        fmt.Println("handle", ctx.Err())
    case <-time.After(d):
        fmt.Println("process request with", d)
    }
}

因为过期时间大于处理时间,所以我们有足够的时间处理该请求,运行上述代码会打印出下面的内容:

$ go run main.go
process request with 500ms
main context deadline exceeded

handle函数没有进入超时的select分支,但是main函数的select却会等待context.Context超时并打印出main context deadline exceeded

如果我们将处理请求时间增加值1500ms,整个程序都会因为上下文的过期而被终止:

$ go run main.go
handle context deadline exceeded
main context deadline exceeded

相信这两个例子能够帮助各位理解context.Context的使用方法和设计原理:多个Goroutine同时订阅ctx.Done()管道中的消息,一旦接收到信号就会立刻停止当前正在执行的工作。

默认上下文

context包中最常用的方法还是context.Backgroundcontext.TODO,这两个方法都会返回预先初始化好的私有变量backgroundtodo,它们会在同一个Go程序中被复用:

// Background returns a non-nil, empty Context. It is never canceled, has no
// values, and has no deadline. It is typically used by the main function,
// initialization, and tests, and as the top-level Context for incoming
// requests.
func Background() Context {
    return background
}

// TODO returns a non-nil, empty Context. Code should use context.TODO when
// it's unclear which Context to use or it is not yet available (because the
// surrounding function has not yet been extended to accept a Context
// parameter).
func TODO() Context {
    return todo
}

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

var (
    background = new(emptyCtx)
    todo       = new(emptyCtx)
)

// An emptyCtx is never canceled, has no values, and has no deadline. It is not
// struct{}, since vars of this type must have distinct addresses.
type emptyCtx int

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 interface{}) interface{} {
    return nil
}

从上述代码中,我们不难看出context.emptyCtx通过空方法实现了context.Context接口中所有方法,它没有任何功能。

TrumanWong

从源码来看,context.Backgroundcontext.TODO也只是互为别名,没有太大差别,只是在使用和语义上稍有不同:

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

在多数情况下,如果当前函数没有上下文作为入参,我们都会使用context.Background作为起始的上下文向下传递。

取消信号

context.WithCancel函数能够从context.Context中衍生出来的一个新的子上下文并返回用于取消该上下文的函数。一旦我们执行返回的取消函数,当前上下文以及它的子上下文都会被取消,所有的Goroutine都会同步收到这一取消信号。

我们直接从context.WithCancel函数的实现来看它到底做了什么:

// WithCancel returns a copy of parent with a new Done channel. The returned
// context's Done channel is closed when the returned cancel function is called
// or when the parent context's Done channel is closed, whichever happens first.
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
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 arranges for child to be canceled when parent is.
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():
            }
        }()
    }
}

上述函数总共与父上下文相关的三种不同的情况:

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

context.propagateCancel的作用是在parentchild之间同步取消和结束的信号,保证在parent被取消时,child也会收到对应的信号,不会出现状态不一致的情况。

context.cancelCtx实现的几个接口方法没有太多值得分析的地方,该结构体最重要的方法是context.cancelCtx.cancel,该方法会关闭上下文中的Channel并向所有的子上下文同步取消信号:

// cancel closes c.done, cancels each of c's children, and, if
// removeFromParent is true, removes c from its parent's children.
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)
    }
}

除了context.WithCancel之外,context包中的另外两个函数context.WithDeadlinecontext.WithTimeout也都能创建可以被取消的计时器上下文context.timerCtx

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
    return WithDeadline(parent, time.Now().Add(timeout))
}

func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
    if parent == nil {
        panic("cannot create context from nil parent")
    }
    if cur, ok := parent.Deadline(); ok && cur.Before(d) {
        // The current deadline is already sooner than the new one.
        return WithCancel(parent)
    }
    c := &timerCtx{
        cancelCtx: newCancelCtx(parent),
        deadline:  d,
    }
    propagateCancel(parent, c)
    dur := time.Until(d)
    if dur <= 0 {
        c.cancel(true, DeadlineExceeded) // deadline has already passed
        return c, func() { c.cancel(false, Canceled) }
    }
    c.mu.Lock()
    defer c.mu.Unlock()
    if c.err == nil {
        c.timer = time.AfterFunc(dur, func() {
            c.cancel(true, DeadlineExceeded)
        })
    }
    return c, func() { c.cancel(true, Canceled) }
}

context.WithDeadline在创建context.timerCtx的过程中判断了父上下文的截止日期与当前日期,并通过timer.AfterFunc创建定时器,当时间超过了截至日期后会调用context.timerCtx.cancel同步取消信号,

context.timerCtx内部不仅通过嵌入context.cancelCtx结构体继承了相关的变量和方法,还通过持有的定时器timer和截止时间deadline实现了定时取消的功能:

// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
// implement Done and Err. It implements cancel by stopping its timer then
// delegating to cancelCtx.cancel.
type timerCtx struct {
    cancelCtx
    timer *time.Timer // Under cancelCtx.mu.

    deadline time.Time
}

func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
    return c.deadline, true
}

func (c *timerCtx) String() string {
    return contextName(c.cancelCtx.Context) + ".WithDeadline(" +
        c.deadline.String() + " [" +
        time.Until(c.deadline).String() + "])"
}

func (c *timerCtx) cancel(removeFromParent bool, err error) {
    c.cancelCtx.cancel(false, err)
    if removeFromParent {
        // Remove this timerCtx from its parent cancelCtx's children.
        removeChild(c.cancelCtx.Context, c)
    }
    c.mu.Lock()
    if c.timer != nil {
        c.timer.Stop()
        c.timer = nil
    }
    c.mu.Unlock()
}

context.timerCtx.cancel方法不仅调用了context.cancelCtx.cancel,还会停止持有的定时器减少不必要的资源浪费。

传值方法

在最后我们需要了解如何使用上下文传值,context包中的context.WithValue能从父上下文中创建一个子上下文,传值的子上下文使用context.valueCtx类型:

func WithValue(parent Context, key, val interface{}) Context {
    if parent == nil {
        panic("cannot create context from nil parent")
    }
    if key == nil {
        panic("nil key")
    }
    if !reflectlite.TypeOf(key).Comparable() {
        panic("key is not comparable")
    }
    return &valueCtx{parent, key, val}
}

context.valueCtx结构体会将除了Value之外的ErrDeadline等方法代理到父上下文中,它只会响应context.valueCtx.Value方法,该方法的实现也很简单:

// A valueCtx carries a key-value pair. It implements Value for that key and
// delegates all other calls to the embedded Context.
type valueCtx struct {
    Context
    key, val interface{}
}

// stringify tries a bit to stringify v, without using fmt, since we don't
// want context depending on the unicode tables. This is only used by
// *valueCtx.String().
func stringify(v interface{}) string {
    switch s := v.(type) {
    case stringer:
        return s.String()
    case string:
        return s
    }
    return "<not Stringer>"
}

func (c *valueCtx) String() string {
    return contextName(c.Context) + ".WithValue(type " +
        reflectlite.TypeOf(c.key).String() +
        ", val " + stringify(c.val) + ")"
}

func (c *valueCtx) Value(key interface{}) interface{} {
    if c.key == key {
        return c.val
    }
    return c.Context.Value(key)
}

如果context.valueCtx中存储的键值对与context.valueCtx.Value方法中传入的参数不匹配,就会从父上下文中查找该键对应的值直到某个父上下文中返回nil或者查找到对应的值。

小结

Go语言中的context.Context的主要作用还是在多个Goroutine组成的树中同步取消信号以减少对资源的消耗和占用,虽然它也有传值的功能,但这个功能我们很少用到。

在真正使用传值的功能时我们也应该非常谨慎,使用context.Context传递请求的所有参数是一种非常差的设计,比较常见的使用场景是传递请求对应用户的认证令牌以及用于进行分布式追踪的请求ID