golang从context源码领悟接口的设计 (2)

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类型
2.1. 如果是,再检查是否已经cancel掉,是则cancel掉child,否则加入child
2.2. 如果不是,则监控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上删除掉

保存key/value信息的Context

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdpdy.html