context.Context一般用作函数或方法的第一个参数,其作用为管控协程在用户侧"生命周期"。它是线程安全的,在多个goroutine之间可以任意调用其方法,不需考虑锁的问题。
原理简析,context的结构是一棵以Background() 或 TODO() 为根节点的多叉树,在树中,子树被取消时会将取消信号向下传导,让子树的子树…一并取消。此时可以将子树从父ctx节点中清除。并不是使用了context.Context以后协程立即就能退出,还需要用户侧使用select监听ctx的Done() 返回值,并手动指定退出规则。
src/context/context.go
// A Context carries a deadline, a cancellation signal, and other values across
// API boundaries.
//
// Context's methods may be called by multiple goroutines simultaneously.
type Context interface {
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
Deadline() (deadline time.Time, ok bool)
// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
// The close of the Done channel may happen asynchronously,
// after the cancel function returns.
//
// WithCancel arranges for Done to be closed when cancel is called;
// WithDeadline arranges for Done to be closed when the deadline
// expires; WithTimeout arranges for Done to be closed when the timeout
// elapses.
//
// Done is provided for use in select statements:
//
// // Stream generates values with DoSomething and sends them to out
// // until DoSomething returns an error or ctx.Done is closed.
// func Stream(ctx context.Context, out chan<- Value) error {
// for {
// v, err := DoSomething(ctx)
// if err != nil {
// return err
// }
// select {
// case <-ctx.Done():
// return ctx.Err()
// case out <- v:
// }
// }
// }
//
// See https://blog.golang.org/pipelines for more examples of how to use
// a Done channel for cancellation.
Done() <-chan struct{}
// If Done is not yet closed, Err returns nil.
// If Done is closed, Err returns a non-nil error explaining why:
// Canceled if the context was canceled
// or DeadlineExceeded if the context's deadline passed.
// After Err returns a non-nil error, successive calls to Err return the same error.
Err() error
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
//
// Use context values only for request-scoped data that transits
// processes and API boundaries, not for passing optional parameters to
// functions.
//
// A key identifies a specific value in a Context. Functions that wish
// to store values in Context typically allocate a key in a global
// variable then use that key as the argument to context.WithValue and
// Context.Value. A key can be any type that supports equality;
// packages should define keys as an unexported type to avoid
// collisions.
//
// Packages that define a Context key should provide type-safe accessors
// for the values stored using that key:
//
// // Package user defines a User type that's stored in Contexts.
// package user
//
// import "context"
//
// // User is the type of value stored in the Contexts.
// type User struct {...}
//
// // key is an unexported type for keys defined in this package.
// // This prevents collisions with keys defined in other packages.
// type key int
//
// // userKey is the key for user.User values in Contexts. It is
// // unexported; clients use user.NewContext and user.FromContext
// // instead of using this key directly.
// var userKey key
//
// // NewContext returns a new Context that carries value u.
// func NewContext(ctx context.Context, u *User) context.Context {
// return context.WithValue(ctx, userKey, u)
// }
//
// // FromContext returns the User value stored in ctx, if any.
// func FromContext(ctx context.Context) (*User, bool) {
// u, ok := ctx.Value(userKey).(*User)
// return u, ok
// }
Value(key any) any
}
把注释清理干净,Context接口长这样
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
}
它的4个方法:
- Deadline() (deadline time.Time, ok bool) 返回context被自动取消的最终时间及其是否会被取消,用户可根据返回的时间长短决定是否执行相应逻辑
- Done() <-chan struct{} context被取消会自动取消时,返回一个关闭的通道,通知所有监听其通道的协程下线
- Err() error 返回context被取消时的error信息
- Value(jey any) any 根据key取value
Canceled 哨兵error,作为context已被取消的判断依据
// Canceled is the error returned by Context.Err when the context is canceled.
var Canceled = errors.New("context canceled")
DeadlineExceeded 超过截止时间的error
// DeadlineExceeded is the error returned by Context.Err when the context's
// deadline passes.
var DeadlineExceeded error = deadlineExceededError{}
type deadlineExceededError struct{}
func (deadlineExceededError) Error() string { return "context deadline exceeded" } // 实现了error接口
func (deadlineExceededError) Timeout() bool { return true } // 超时的
func (deadlineExceededError) Temporary() bool { return true } // 临时的
emptyCtx 实现了Context接口,作为根Context使用,其没有超时,截止时间等操作逻辑
// 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 any) any {
return nil
}
func (e *emptyCtx) String() string { //实现Stringer接口
switch e {
case background:
return "context.Background"
case todo:
return "context.TODO"
}
return "unknown empty Context"
}
Background() 和 TODO() 返回new(emptyCtx) 只作为根Context使用,其虽然实现了Context接口,但没有具体的操作实现,只是做了简单的返回,TODO()函数是不知道传什么类型的context时的一种占位操作,当确定业务需求时,应将其替换成具体的context
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
// 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
}
CancelFunc 通知一个操作放弃它的工作。它不等待工作停止。它可以被多个goroutine同时调用。其在第一次调用之后,对CancelFunc的后续调用不执行任何操作
// A CancelFunc tells an operation to abandon its work.
// A CancelFunc does not wait for the work to stop.
// A CancelFunc may be called by multiple goroutines simultaneously.
// After the first call, subsequent calls to a CancelFunc do nothing.
type CancelFunc func()
// 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 { // 如果传入的父ctx是空的,则panic
panic("cannot create context from nil parent")
}
c := newCancelCtx(parent) //创建一个cancelCtx结构体作为子ctx
propagateCancel(parent, &c) // 与父ctx产生关联
return &c, func() { c.cancel(true, Canceled) } // 返回包装了c.cancel的匿名函数句柄
}
newCancelCtx
// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {
return cancelCtx{Context: parent}
}
// goroutines counts the number of goroutines ever created; for testing.
var goroutines int32 //协程数量,用于测试
propagateCancel 子ctx与父ctx产生关联
// propagateCancel arranges for child to be canceled when parent is.
func propagateCancel(parent Context, child canceler) {
done := parent.Done()
if done == nil { // 父ctx的Done()的通道还没有被创建,那就各管理各的,直接返回
return // parent is never canceled 同时也证明父ctx没有被取消
}
// 父ctx的Done()通道已经被创建,则监听其零值,并与child子ctx产生关联,父ctx取消时,同时取消子ctx
select {
case <-done: //父ctx 已经被取消了
// parent is already canceled
child.cancel(false, parent.Err()) //取消子ctx
return
default: //防止阻塞
}
// 找到了可以被取消的父ctx
if p, ok := parentCancelCtx(parent); ok {
p.mu.Lock() // 加锁
if p.err != nil { //父ctx 已经被取消
// parent has already been canceled
child.cancel(false, p.err) // 取消子ctx
} else {
if p.children == nil { // 父ctx 还没有children列表
p.children = make(map[canceler]struct{}) //创建children列表
}
p.children[child] = struct{}{} // 将子ctx 挂载到父ctx的children列表
}
p.mu.Unlock() //解锁
} else { // 不能找到可被取消的父ctx
atomic.AddInt32(&goroutines, +1) // goroutines个数 原子自增1
go func() { // 启动单独的协程进行管控,该协程无论是结束父ctx还是子ctx都会退出
select {
case <-parent.Done(): // 监控父ctx是否结束
child.cancel(false, parent.Err()) //父ctx结束则子ctx 也结束
case <-child.Done(): // 子ctx被自己干掉,则不用管父ctx状态
}
}()
}
}
parentCancelCtx 找到可以被取消的父ctx
// &cancelCtxKey is the key that a cancelCtx returns itself for.
var cancelCtxKey int
//parentCancelCtx返回parent的*cancelCtx。
//通过查找parent.Value(&cancelCtxKey)来查找
//最里面的封闭*cancelCtx,然后检查是否
//parent.Done()匹配*cancelCtx。(如果不是,*cancelCtx
//已被包装在自定义实现中,提供了
//不同的done通道,在这种情况下,我们不应该绕过它。)
// parentCancelCtx returns the underlying *cancelCtx for parent.
// It does this by looking up parent.Value(&cancelCtxKey) to find
// the innermost enclosing *cancelCtx and then checking whether
// parent.Done() matches that *cancelCtx. (If not, the *cancelCtx
// has been wrapped in a custom implementation providing a
// different done channel, in which case we should not bypass it.)
func parentCancelCtx(parent Context) (*cancelCtx, bool) {
done := parent.Done()
if done == closedchan || done == nil {
return nil, false
}
p, ok := parent.Value(&cancelCtxKey).(*cancelCtx)
if !ok {
return nil, false
}
pdone, _ := p.done.Load().(chan struct{})
if pdone != done {
return nil, false
}
return p, true
}
removeChild 从父ctx中移除子ctx
// removeChild removes a context from its parent.
func removeChild(parent Context, child canceler) {
p, ok := parentCancelCtx(parent) //找到可以被取消的父ctx
if !ok { //找不到直接返回
return
}
p.mu.Lock() // 加锁
if p.children != nil { //检查列表
delete(p.children, child) //删除子ctx
}
p.mu.Unlock() //解锁
}
canceler是一种可以直接取消的上下文接口类型,cancelCtx和timerCtx都实现了它
其有2个方法:
- cancel(removeFromParent bool, err error) 取消方法,第一个参数代表是否从父ctx中移除该ctx,第二个参数是取消ctx时的error信息
- Done() <-chan struct{} 同Context接口中的Done()
// A canceler is a context type that can be canceled directly. The
// implementations are *cancelCtx and *timerCtx.
type canceler interface {
cancel(removeFromParent bool, err error)
Done() <-chan struct{}
}
closedchan 作为被关闭的channel的判定依据,其可重用
// closedchan is a reusable closed channel.
var closedchan = make(chan struct{}) //创建保内可见的channel
func init() { //代码初始化阶段直接将其关闭
close(closedchan)
}
cancelCtx 结构体与接口的实现
// A cancelCtx can be canceled. When canceled, it also cancels any children
// that implement canceler.
type cancelCtx struct {
Context // 匿名组合Context接口
mu sync.Mutex // protects following fields 保护后续字段
done atomic.Value // of chan struct{}, created lazily, closed by first cancel call 懒加载,在第一次cancel的时候创建
children map[canceler]struct{} // set to nil by the first cancel call 子ctx列表
err error // set to non-nil by the first cancel call ctx取消的原因
}
func (c *cancelCtx) Value(key any) any {
if key == &cancelCtxKey { // 如果key是cancelCtxKey,则返回自己
return c
}
return value(c.Context, key) //在ctx中查找,返回相应的key对应的value
}
func (c *cancelCtx) Done() <-chan struct{} {
d := c.done.Load()
if d != nil { // 加载到,直接断言返回
return d.(chan struct{})
}
c.mu.Lock() // 没加载到 上锁,创建,存储,解锁,断言返回
defer c.mu.Unlock()
d = c.done.Load()
if d == nil {
d = make(chan struct{})
c.done.Store(d)
}
return d.(chan struct{})
}
func (c *cancelCtx) Err() error { //Err方法,返回ctx取消原因
c.mu.Lock()
err := c.err
c.mu.Unlock()
return err
}
type stringer interface { //这里抄了一个Stringer接口,主要是使用其String() string 方法
String() string
}
func contextName(c Context) string { //context 的名称
if s, ok := c.(stringer); ok { //断言成stringer接口,直接返回
return s.String()
}
return reflectlite.TypeOf(c).String() //反射取string
}
func (c *cancelCtx) String() string { // 打印时的信息
return contextName(c.Context) + ".WithCancel"
}
// Cancel关闭c.done,取消c的每个子结点,并且,if
// removeFromParent == true,将c从父类的children列表中移除
// 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
panic("context: internal error: missing cancel error")
}
c.mu.Lock() //上锁
if c.err != nil { // 当前ctx的err非空,证明已经cancel过了
c.mu.Unlock() // 解锁
return // already canceled 已经cancel过了就返回
}
c.err = err // 赋值传入的err
d, _ := c.done.Load().(chan struct{}) // 加载一个chan
if d == nil { //加载不到就用全局的
c.done.Store(closedchan)
} else {
close(d) //加载到了 就关闭 关闭时会通知所有监听该chan的goroutine
}
for child := range c.children { //站在父亲的角度,通知其子ctx列表,让所有子ctx逐个取消
// NOTE: acquiring the child's lock while holding parent's lock.
child.cancel(false, err) // 第一个参数为false,是因为后续的c.children = nil 连窝端了,所以不用逐个删除,浪费时间
}
c.children = nil //列表置空
c.mu.Unlock() //解锁
if removeFromParent { //若函数第一个参数为true
removeChild(c.Context, c) //则从父ctx的children列表中移除子ctx
}
}
// WithDeadline returns a copy of the parent context with the deadline adjusted
// to be no later than d. If the parent's deadline is already earlier than d,
// WithDeadline(parent, d) is semantically equivalent to parent. The returned
// context's Done channel is closed when the deadline expires, 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 WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
if parent == nil { // 传入parent是空的,直接panic
panic("cannot create context from nil parent")
}
if cur, ok := parent.Deadline(); ok && cur.Before(d) { // 父ctx下线时间在传入时间之前,合理
// The current deadline is already sooner than the new one.
return WithCancel(parent) // 让WithCancel()接管
}
c := &timerCtx{ // 创建其结构体引用
cancelCtx: newCancelCtx(parent), //创建cancelCtx对象
deadline: d, //赋值下线时间
}
propagateCancel(parent, c) //与父ctx取得关联
dur := time.Until(d) //计算剩余时长
if dur <= 0 { // 负剩余 直接cancel
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 { // ctx还没被取消
c.timer = time.AfterFunc(dur, func() { // dur 时间后执行匿名函数,cancel
c.cancel(true, DeadlineExceeded)
})
}
return c, func() { c.cancel(true, Canceled) } //返回子ctx和cancel句柄
}
// 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 // 组合了一个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()
}
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete:
//
// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
// defer cancel() // releases resources if slowOperation completes before timeout elapses
// return slowOperation(ctx)
// }
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
// WithValue returns a copy of parent in which the value associated with key is
// val.
//
// Use context Values only for request-scoped data that transits processes and
// APIs, not for passing optional parameters to functions.
//
// The provided key must be comparable and should not be of type
// string or any other built-in type to avoid collisions between
// packages using context. Users of WithValue should define their own
// types for keys. To avoid allocating when assigning to an
// interface{}, context keys often have concrete type
// struct{}. Alternatively, exported context key variables' static
// type should be a pointer or interface.
func WithValue(parent Context, key, val any) 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}
}
// 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 // 组合Context接口
key, val any //包含一对k-v
}
// 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 any) string { //string 格式化
switch s := v.(type) {
case stringer:
return s.String()
case string:
return s
}
return ""
}
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 any) any { //获取key 对应的value
if c.key == key {
return c.val
}
return value(c.Context, key)
}
func value(c Context, key any) any {
for { //迭代
switch ctx := c.(type) { // 断言,而后判定返回
case *valueCtx:
if key == ctx.key {
return ctx.val
}
c = ctx.Context
case *cancelCtx:
if key == &cancelCtxKey {
return c
}
c = ctx.Context
case *timerCtx:
if key == &cancelCtxKey {
return &ctx.cancelCtx
}
c = ctx.Context
case *emptyCtx:
return nil
default:
return c.Value(key) // 有可能递归迭代
}
}
}