Golang Context源碼學習

FosWpq 7年前發布 | 36K 次閱讀 Google Go/Golang開發 Golang

起因

最近學習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() </code></pre>

這里通過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
}

} </code></pre>

大致結構

//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) </code></pre>

以上即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{} } </code></pre>

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

} </code></pre>

主要干了以上三件事

  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() } </code></pre>

首先判斷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里的。

完結感想

原來一直沒有寫過這種文章,寫了一篇才發現好耗時間。而且寫的這兩也不咋地,不過的確寫寫能夠加深理解,找出浮光掠影式看代碼沒發現的地方,還是很有好處的。以后要堅持下去。

來自:studygolang.com/wr?u=http%3a%2f%2fwww.jianshu.com%2fp%2f7e3c151a5422

 

 本文由用戶 FosWpq 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!