Golang Context源码学习

起因

最近学习golang框架的时候发现许多地方都用到了context的概念,比如grpc请求 etcd访问等许多地方。 本着追根溯源搞清楚实现方式的劲头,决定研究下实现原理。

用处

  1. 一般上用在GRpc等框架内,设置超时时间,比如
ctx, cancel := context.WithTimeout(context.Background(), 2 * time.Second)

dial, err := grpc.DialContext(ctx, etcdAddr, grpc.WithInsecure(),grpc.WithBalancer(balancer))    

cancel()

这里通过WithTimeout获得一个超时的Context 给grpc.DialContext 作为参数,这个Context本身内部有个timer定时器,在timer定时器时间到的时候会自动cancel掉Context 并且关闭Context内部的done chan, 一般使用ctx作参数的方法内部会检查done chan一旦发现chan 关闭,那么就应该认为这个操作需要结束了,从而返回错误(这个错误也是context内部的err,是在Context内部cancel时置的)

  1. 自己程序里用到Context时,内部实现方法类似如下
func doSomething(ctx Context) error{
    //go .....doSomethingLong......

    select{
        case <-ctx.Done():
            return ctx.Err()
        case err <- somethingChan:
            return err
    }
}

大致结构

//Context接口
type Context interface {
    Deadline() (deadline time.Time, ok bool)
    Done() <-chan struct{}
    Err() error
    Value(key interface{}) interface{}
}


//主要暴露的方法
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)

func WithValue(parent Context, key, val interface{}) Context

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

以上即Contxt的接口结构和最常用的暴露出的方法,

//内部interface
type canceler interface {
    cancel(removeFromParent bool, err error)
    Done() <-chan struct{}
}

//内部重要struct
type timerCtx struct {
    cancelCtx
    timer *time.Timer // Under cancelCtx.mu.

    deadline time.Time
}

type cancelCtx struct {
    Context

    done chan struct{} // closed by the first cancel call.

    mu       sync.Mutex
    children map[canceler]struct{} // set to nil by the first cancel call
    err      error                 // set to non-nil by the first cancel call
}

type valueCtx struct {
    Context
    key, val interface{}
}

canceler 接口

cancel方法直接取消context,大致实现方法是置cancelCtx的err字段,当err字段不为空时,即意味着这个context已经失效;
Done方法返回是否完成channel, 判断context是否成功完成

timerCtx

withDeadline和WithTimeout返回的实际结构体(parent未失效时),而其中又包含了一个cancelCtx. cancelCtx的context为timerCtx真正的parent.
实现了接口canceler.

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

cancelCtx

对应withCancel 内含Context为其parent Context.
实现了接口canceler

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

主要干了以上三件事

  1. 置了err
  2. 关闭chan done
  3. 递归调用子context的cancel方法

valueCtx

对应withValue。 内含Context为其parent Context. valueCtx逻辑最简单 只是额外加了一对键值对, 主要提供上下文变量保存的作用. Value方法可以递归向上查找key对应的value。见Value方法 如下

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

主要具体实现

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


func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    c := newCancelCtx(parent)
    propagateCancel(parent, &c)
    return &c, func() { c.cancel(true, Canceled) }
}


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

func removeChild(parent Context, child canceler) {
    p, ok := parentCancelCtx(parent)
    if !ok {
        return
    }
    p.mu.Lock()
    if p.children != nil {
        delete(p.children, child)
    }
    p.mu.Unlock()
}

首先判断parent本身是否已经过期了,如果过期, 只返回cancelCtx,因为已经过期,没必要使用timerCtx设置过期时间等等。 否则创建timerCtx。
然后找出最近的一个父cancelCtx,如果存在将此timerCtx置为他的child,不存在就起一个goroutine轮询等待parent完成,一旦parent完成,cancel掉此timerCtx。
根据child的过期时间作判断,如果已经过期,直接cancel掉timerCtx,并从parent中移除,防止资源堆积,如未过期设置timer过期时cancel掉timerCtx.

cancel方法有参数removeFromParent,表示是否从parent context移除本canceler.
因为只有cancelCtx有child字段,所以需要找到最近cancel parent来移除child.

总结

一圈代码看下来,原理和结构其实了解了,但是其实还是有一些实现的小细节让人绕了半天,比如

  1. cancelCtx的child放的全是canceler接口的map, 因为实现canceler接口的结构体才实现cancel方法。

  2. timerCtx的cancel方法里会先调用c.cancelCtx.cancel(false, err), 然后在判断removeFromParent, 再调用removeChild(c.cancelCtx.Context, c). 因为直接调用c.cancelCtx.canel(true, err)显示达不到移除c的目的,因为这里是从c.cancelCtx的child中移除c,然而c是在c.cancelCtx.Context的child里的。

完结感想

原来一直没有写过这种文章,写了一篇才发现好耗时间。而且写的这两也不咋地,不过的确写写能够加深理解,找出浮光掠影式看代码没发现的地方,还是很有好处的。以后要坚持下去。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容