Golang Context源碼學習

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

起因

最近學習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{}) Contextfunc WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

以上即Contxt的介面結構和最常用的暴露出的方法,

//內部interfacetype canceler interface {    cancel(removeFromParent bool, err error)    Done() <-chan struct{}}//內部重要structtype 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裡的。

完結感想

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.