注:写帖子时go的版本是1.12.7
Context的github地址
go
语言中实现一个interface
不用像其余语言同样须要显示的声明实现接口。go
语言只要实现了某interface
的方法就能够作类型转换。go
语言没有继承的概念,只有Embedding
的概念。想深刻学习这些用法,阅读源码是最好的方式.Context
的源码很是推荐阅读,从中能够领悟出go
语言接口设计的精髓。git
Context
源码中只对外显露出一个Context
接口github
type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }
对于Context
的实现源码里有一个最基本的实现,就是私有的emptyCtx
,他也就是咱们常常使用的context.Background()
底层的实现,他是一个int
类型,实现了Context
接口的全部方法,但都是没有作任何处理,都是返回的默认空值。只有String()
方法,里有几行代码,去判断emptyCtx
的类型来进行相应的字符串输出,String()
方法实际上是实现了接口Stringer
。emptyCtx
是整个Context
的灵魂
,为何这么说,由于你对context
的全部的操做都是基于他去作的再次封装。
注意一下Value(key interface{}) interface{}
,由于尚未泛型
,因此能用的作法就是传递或者返回interface{}
。不知道Go2
会不会加入泛型
,说是会加入,可是尚未出最终版,一切都是未知的,由于前一段时间还说会加入try
,后来又宣布放弃。golang
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 } func (e *emptyCtx) String() string { switch e { case background: return "context.Background" case todo: return "context.TODO" } return "unknown empty Context" } var ( background = new(emptyCtx) todo = new(emptyCtx) )
在使用Context
时咱们能直接获得就是background
和todo
安全
func Background() Context { return background } func TODO() Context { return todo }
其余全部对外公开的方法都必须传入一个Context
作为parent
,这里设计的很巧妙,为何要有parent
后面我会详细说。学习
能够cancel掉的context有三个公开的方法,也就是,是否带过时时间的Context
ui
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
Context
只用关心本身是否Done()
,具体这个是怎么完成的他并不关心,是否能够cancel
掉也不是他的业务,因此源码中把这部分功能分开来。
Context
最经常使用的功能就是去监控他的Done()
是否已完成,而后判断完成的缘由,根据本身的业务展开相应的操做。要提一下Context
是线程安全的,他在必要的地方都加了锁处理。Done()
的原理:实际上是close
掉了channel
因此全部监控Done()
方法都能知道这个Context
执行完了。线程
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) defer cancel() v, err := DoSomething(ctx) if err != nil { return err } select { case <-ctx.Done(): return ctx.Err() case out <- v: }
我这里不缀述Context
是如何使用的。这篇帖子主要分析的是源码。
Context
能够被cancel
掉须要考虑几个问题:设计
Context
的cancel
。cancel
后Context
是否也应该删除掉。咱们从源码中来找到答案。
看一下canceler
的接口,这是一个独立的私有接口,和Context
接口独立开来,Context
只作本身的事,并不用关心本身有啥附加的功能,好比如今说的cancel
功能,这也是一个很好的例子,若是有须要对Context
进行扩展,能够参考他们的代码。code
type canceler interface { cancel(removeFromParent bool, err error) Done() <-chan struct{} }
和两个错误继承
var Canceled = errors.New("context canceled") var DeadlineExceeded error = deadlineExceededError{}
是个是被主动Cancel
的错误和一个超时
的错误,这两个错误是对外显露的,咱们也是根据这两个Error
判断Done()
是如何完成的。
实现canceler
接口的是结构体cancelCtx
// that implement canceler. type cancelCtx struct { Context mu sync.Mutex // protects following fields done chan struct{} // created lazily, closed by first cancel call children map[canceler]struct{} // set to nil by the first cancel call err error // set to non-nil by the first cancel call }
注意:
cancelCtx
把Context
接口Embedding
进去了,也就是说cancelCtx
多重实现接口,不可是个canceler
类型也是一个Context
类型。
源码中cancelCtx
并无实现Context
接口中的全部的方法,这就是Embedding
的强大之处,Context
接口的具体实现都是外部传进来的具体Context
实现类型来实现的eg:cancelCtx{Context: xxxx}
。
还要注意一点就是这两个接口都有各自的Done()
方法,cancelCtx
有实现本身的Done()
方法,也就是说不管转换成canceler
接口类型仍是Context
类型调用Done()
方法时,都是他本身的实现
以cancelCtx
为基础还有一个是带过时时间的实现timerCtx
type timerCtx struct { cancelCtx timer *time.Timer // Under cancelCtx.mu. deadline time.Time }
timerCtx
是WithDeadline
和WithTimeout
方法的基础。
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
WithCancel
须要调用者主动去调用cancel
,其余的两个,就是有过时时间,若是不主动去调用cancel
到了过时时间系统会自动调用。
上面我有说过
context
包中Background()
和TODO()
方法,是其余全部公开方法的基础,由于其余全部的公开方法都须要传递进来一个Context
接口作为parent
。这样咱们全部建立的新的Context
都是以parent
为基础来进行封装和操做
看一下cancelCtx
的是如何初始化的
func newCancelCtx(parent Context) cancelCtx { return cancelCtx{Context: parent} } func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { c := newCancelCtx(parent) propagateCancel(parent, &c) return &c, func() { c.cancel(true, Canceled) } }
propagateCancel
回答了咱们第一个问题
如何处理父或子
Context
的cancel
。
func propagateCancel(parent Context, child canceler) { if parent.Done() == nil { return // parent is never canceled } 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 { go func() { select { case <-parent.Done(): child.cancel(false, parent.Err()) case <-child.Done(): } }() } }
propagateCancel
作了如下几件事
parent
是否能够cancel
parent
是不是cancelCtx
类型cancel
掉,是则cancel掉child
,不然加入child
parent
和child 的Done()
咱们看一下timerCtx
的具体实现
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 if c.done == nil { c.done = closedchan } else { close(c.done) } 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
的调用会发现
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { c := newCancelCtx(parent) propagateCancel(parent, &c) return &c, func() { c.cancel(true, Canceled) } } func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) { 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) } }
返回的cancel
方法都是func() { c.cancel(true, Canceled) }
回答了咱们的第二个问题
cancel
后Context
是否也应该删除掉。
全部建立的能够cancel
掉的方法都会被从parent
上删除掉
Context
还有一个功能就是保存key/value
的信息,从源码中咱们能够看出一个Context
只能保存一对,可是咱们能够调用屡次WithValue
建立多个Context
func WithValue(parent Context, key, val interface{}) Context { if key == nil { panic("nil key") } if !reflect.TypeOf(key).Comparable() { panic("key is not comparable") } return &valueCtx{parent, key, val} }
在查询key
的时候,是一个向上递归的过程:
func (c *valueCtx) Value(key interface{}) interface{} { if c.key == key { return c.val } return c.Context.Value(key) }
总结一下