Golang 之context庫用法

1. context

Golang 中的context 是Go語言在 golang1.7 發(fā)布時(shí)新增的標(biāo)準(zhǔn)包

目的是增強(qiáng)Golang開發(fā)中并發(fā)控制技術(shù)

簡單來講當(dāng)一個(gè)服務(wù)啟動(dòng)時(shí),可能由此服務(wù)派生出多個(gè)多層級(jí)的 goroutine , 但是本質(zhì)上來講每個(gè)層級(jí)的 goroutine 都是平行調(diào)度使用,不存在goroutine '父子' 關(guān)系 , 當(dāng)其中一個(gè) goroutine 執(zhí)行的任務(wù)被取消了或者處理超時(shí)了,那么其他被啟動(dòng)起來的Goroutine 都應(yīng)該迅速退出,另外多個(gè)多層的Goroutine 想傳遞請求域的數(shù)據(jù)該如何處理?

如果單個(gè)請求的Goroutine 結(jié)構(gòu)比較簡單,或者處理起來也不麻煩,但是如果啟動(dòng)的Goroutine 是多個(gè)并且結(jié)構(gòu)層次很深那么光是保障每個(gè)Goroutine 正常退出也不很容易了

為此Go1.7以來提供了 context 來解決類似的問題 , context 可以跟蹤 Goroutine 的調(diào)用, 在調(diào)用內(nèi)部維護(hù)一個(gè)調(diào)用樹,通過這個(gè)調(diào)用樹可以在傳遞超時(shí)或者退出通知,還能在調(diào)用樹中傳遞元數(shù)據(jù)

context的中文翻譯是上下文 ,我們可以理解為 context 管理了一組呈現(xiàn)樹狀結(jié)構(gòu)的 Goroutine ,讓每個(gè)Goroutine 都擁有相同的上下文,并且可以在這個(gè)上下文中傳遞數(shù)據(jù)

2. context.go

2.0 結(jié)構(gòu)圖
context.png

我們看一 context.go 的源文件了解一下context 的構(gòu)成 該文件通常位于

$GOROOT/src/context/context.go
2.1 Context interface

context 實(shí)際上只是定義的4個(gè)方法的接口,凡是實(shí)現(xiàn)了該接口的都稱為一種 context

// A Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
//
// Context's methods may be called by multiple goroutines simultaneously.
type Context interface {
    // 標(biāo)識(shí)deadline是否已經(jīng)設(shè)置了,沒有設(shè)置時(shí),ok的值是false,并返回初始的time.Time
    Deadline() (deadline time.Time, ok bool)
    // 返回一個(gè)channel, 當(dāng)返回關(guān)閉的channel時(shí)可以執(zhí)行一些操作
    Done() <-chan struct{}
    // 描述context關(guān)閉的原因,通常在Done()收到關(guān)閉通知之后才能知道原因
    Err() error
    // 獲取上游Goroutine 傳遞給下游Goroutine的某些數(shù)據(jù)
    Value(key interface{}) interface{}
}
2.2 emptyCtx
// An emptyCtx is never canceled, has no values, and has no deadline. It is not
// struct{}, since vars of this type must have distinct addresses.
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)
)

// Background returns a non-nil, empty Context. It is never canceled, has no
// values, and has no deadline. It is typically used by the main function,
// initialization, and tests, and as the top-level Context for incoming
// requests.
func Background() Context {
    return background
}

// TODO returns a non-nil, empty Context. Code should use context.TODO when
// it's unclear which Context to use or it is not yet available (because the
// surrounding function has not yet been extended to accept a Context
// parameter).
func TODO() Context {
    return todo
}

我們看到 emptyCtx 實(shí)現(xiàn)了Context 接口,但是其實(shí)現(xiàn)的方法都是空nil 那么我們就可以知道其實(shí)emptyCtx 是不具備任何實(shí)際功能的,那么它存在的目的是什么呢?

emptyCtx 存在的意義是作為 Context 對象樹根節(jié)點(diǎn) root節(jié)點(diǎn) , 在context.go 包中提供 Background()TODO() 兩個(gè)函數(shù) ,這兩個(gè)函數(shù)都是返回的都是 emptyCtx 實(shí)例 ,通常我們使用他們來構(gòu)建Context的根節(jié)點(diǎn) , 有了root根節(jié)點(diǎn)之后就可同事 context.go 包中提供的其他的包裝函數(shù)創(chuàng)建具有意義的context 實(shí)例 ,并且沒有context 實(shí)例的創(chuàng)建都是以上一個(gè) context 實(shí)例對象作為參數(shù)的(所以必須有一個(gè)根節(jié)點(diǎn)) ,最終形成一個(gè)樹狀的管理結(jié)構(gòu)

2.3 cancelCtx

定義了cancelCtx 類型的結(jié)構(gòu)體

其中字段children 記錄派生的child,當(dāng)該類型的context(上下文) 被執(zhí)行cancel是會(huì)將所有派生的child都執(zhí)行cancel

對外暴露了 Err() Done() String() 方法

// A cancelCtx can be canceled. When canceled, it also cancels any children
// 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
}

func (c *cancelCtx) Done() <-chan struct{} {
    c.mu.Lock()
    if c.done == nil {
        c.done = make(chan struct{})
    }
    d := c.done
    c.mu.Unlock()
    return d
}

func (c *cancelCtx) Err() error {
    c.mu.Lock()
    err := c.err
    c.mu.Unlock()
    return err
}

func (c *cancelCtx) String() string {
    return fmt.Sprintf("%v.WithCancel", c.Context)
}

// cancel closes c.done, cancels each of c's children, and, if
// removeFromParent is true, removes c from its parent's children.
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)
    }
}
2.4 valueCtx

通過 valueCtx 結(jié)構(gòu)知道僅是在Context 的基礎(chǔ)上增加了元素 keyvalue

通常用于在層級(jí)協(xié)程之間傳遞數(shù)據(jù)

// A valueCtx carries a key-value pair. It implements Value for that key and
// delegates all other calls to the embedded Context.
type valueCtx struct {
    Context
    key, val interface{}
}

func (c *valueCtx) String() string {
    return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
}

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

cancelCtx 基礎(chǔ)上增加了字段 timerdeadline

timer 觸發(fā)自動(dòng)cancel的定時(shí)器

deadline 標(biāo)識(shí)最后執(zhí)行cancel的時(shí)間

type timerCtx struct {
    cancelCtx
    timer *time.Timer // Under cancelCtx.mu.

    deadline time.Time
}

func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
    return c.deadline, true
}

func (c *timerCtx) String() string {
    return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline))
}

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

3. 使用示例

context.go 包中提供了4個(gè)以 With 開頭的函數(shù), 這幾個(gè)函數(shù)的主要功能是實(shí)例化不同類型的context

通過 Background()TODO() 創(chuàng)建最 emptyCtx 實(shí)例 ,通常是作為根節(jié)點(diǎn)

通過 WithCancel() 創(chuàng)建 cancelCtx 實(shí)例

通過 WithValue() 創(chuàng)建 valueCtx 實(shí)例

通過 WithDeadlineWithTimeout 創(chuàng)建 timerCtx 實(shí)例

3.1 WithCancel

源碼如下

// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {
    return cancelCtx{Context: parent}
}
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    // 創(chuàng)建cancelCtx實(shí)例
    c := newCancelCtx(parent)
    // 添加到父節(jié)點(diǎn)的children中
    propagateCancel(parent, &c)
    // 返回實(shí)例和方法
    return &c, func() { c.cancel(true, Canceled) }
}

**使用示例 : **

package main

import (
    "context"
    "fmt"
    "time"
)

func MyOperate1(ctx context.Context) {
    for {
        select {
        default:
            fmt.Println("MyOperate1", time.Now().Format("2006-01-02 15:04:05"))
            time.Sleep(2 * time.Second)
        case <-ctx.Done():
            fmt.Println("MyOperate1 Done")
            return
        }
    }
}
func MyOperate2(ctx context.Context) {
    fmt.Println("Myoperate2")
}
func MyDo2(ctx context.Context) {
    go MyOperate1(ctx)
    go MyOperate2(ctx)
    for {
        select {
        default:
            fmt.Println("MyDo2 : ", time.Now().Format("2006-01-02 15:04:05"))
            time.Sleep(2 * time.Second)
        case <-ctx.Done():
            fmt.Println("MyDo2 Done")
            return
        }
    }

}
func MyDo1(ctx context.Context) {
    go MyDo2(ctx)
    for {
        select {
        case <-ctx.Done():
            fmt.Println("MyDo1 Done")
            // 打印 ctx 關(guān)閉原因
            fmt.Println(ctx.Err())
            return
        default:
            fmt.Println("MyDo1 : ", time.Now().Format("2006-01-02 15:04:05"))
            time.Sleep(2 * time.Second)
        }
    }
}
func main() {
    // 創(chuàng)建 cancelCtx 實(shí)例
    // 傳入context.Background() 作為根節(jié)點(diǎn)
    ctx, cancel := context.WithCancel(context.Background())
    // 向協(xié)程中傳遞ctx
    go MyDo1(ctx)
    time.Sleep(5 * time.Second)
    fmt.Println("stop all goroutines")
    // 執(zhí)行cancel操作
    cancel()
    time.Sleep(2 * time.Second)
}

3.2 WithDeadline

設(shè)置了deadlinecontext

這個(gè)deadline(最終期限) 表示context在指定的時(shí)刻結(jié)束

源碼如下

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

使用示例

package main

import (
    "context"
    "fmt"
    "time"
)

func dl2(ctx context.Context) {
    n := 1
    for {
        select {
        case <-ctx.Done():
            fmt.Println(ctx.Err())
            return
        default:
            fmt.Println("dl2 : ", n)
            n++
            time.Sleep(time.Second)
        }
    }
}

func dl1(ctx context.Context) {
    n := 1
    for {
        select {
        case <-ctx.Done():
            fmt.Println(ctx.Err())
            return
        default:
            fmt.Println("dl1 : ", n)
            n++
            time.Sleep(2 * time.Second)
        }
    }
}
func main() {
    // 設(shè)置deadline為當(dāng)前時(shí)間之后的5秒那個(gè)時(shí)刻
    d := time.Now().Add(5 * time.Second)
    ctx, cancel := context.WithDeadline(context.Background(), d)
    defer cancel()
    go dl1(ctx)
    go dl2(ctx)
    for{
        select {
            case <-ctx.Done():
                fmt.Println("over",ctx.Err())
                return
        }
    }
}

3.3 WithTimeout

實(shí)際就是調(diào)用了WithDeadline()

源碼如下

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
    return WithDeadline(parent, time.Now().Add(timeout))
}

使用示例 :

package main

import (
   "context"
   "fmt"
   "time"
)

func to1(ctx context.Context) {
   n := 1
   for {
       select {
       case <-ctx.Done():
           fmt.Println("to1 is over")
           return
       default:
           fmt.Println("to1 : ", n)
           n++
           time.Sleep(time.Second)
       }
   }
}
func main() {
   // 設(shè)置為6秒后context結(jié)束
   ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
   defer cancel()
   go to1(ctx)
   n := 1
   for {
       select {
       case <-time.Tick(2 * time.Second):
           if n == 9 {
               return
           }
           fmt.Println("number :", n)
           n++
       }
   }
}

3.4 WithValue

僅是在Context 基礎(chǔ)上添加了 key : value 的鍵值對

context 形成的樹狀結(jié)構(gòu),后面的節(jié)點(diǎn)可以訪問前面節(jié)點(diǎn)傳導(dǎo)的數(shù)據(jù)

源碼如下 :

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

// A valueCtx carries a key-value pair. It implements Value for that key and
// delegates all other calls to the embedded Context.
type valueCtx struct {
   Context
   key, val interface{}
}

使用示例 :

package main

import (
   "context"
   "fmt"
   "time"
)

func v3(ctx context.Context) {
   for {
       select {
       case <-ctx.Done():
           fmt.Println("v3 Done : ", ctx.Err())
           return
       default:
           fmt.Println(ctx.Value("key"))
           time.Sleep(3 * time.Second)
       }
   }
}
func v2(ctx context.Context) {
   fmt.Println(ctx.Value("key"))
   fmt.Println(ctx.Value("v1"))
   // 相同鍵,值覆蓋
   ctx = context.WithValue(ctx, "key", "modify from v2")
   go v3(ctx)
}
func v1(ctx context.Context) {
   if v := ctx.Value("key"); v != nil {
       fmt.Println("key = ", v)
   }
   ctx = context.WithValue(ctx, "v1", "value of v1 func")
   go v2(ctx)
   for {
       select {
       default:
           fmt.Println("print v1")
           time.Sleep(time.Second * 2)
       case <-ctx.Done():
           fmt.Println("v1 Done : ", ctx.Err())
           return
       }
   }
}
func main() {
   ctx, cancel := context.WithCancel(context.Background())
   // 向context中傳遞值
   ctx = context.WithValue(ctx, "key", "main")
   go v1(ctx)
   time.Sleep(10 * time.Second)
   cancel()
   time.Sleep(3 * time.Second)
}

參考資料

- [1] context

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谓形,一起剝皮案震驚了整個(gè)濱河市灶伊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌寒跳,老刑警劉巖聘萨,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異冯袍,居然都是意外死亡匈挖,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門康愤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來儡循,“玉大人,你說我怎么就攤上這事征冷≡裣ィ” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵检激,是天一觀的道長肴捉。 經(jīng)常有香客問我,道長叔收,這世上最難降的妖魔是什么齿穗? 我笑而不...
    開封第一講書人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮饺律,結(jié)果婚禮上窃页,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好脖卖,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開白布乒省。 她就那樣靜靜地躺著,像睡著了一般畦木。 火紅的嫁衣襯著肌膚如雪袖扛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評(píng)論 1 284
  • 那天十籍,我揣著相機(jī)與錄音蛆封,去河邊找鬼。 笑死妓雾,一個(gè)胖子當(dāng)著我的面吹牛娶吞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播械姻,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼机断!你這毒婦竟也來了楷拳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬榮一對情侶失蹤吏奸,失蹤者是張志新(化名)和其女友劉穎欢揖,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奋蔚,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡她混,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了泊碑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坤按。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖馒过,靈堂內(nèi)的尸體忽然破棺而出臭脓,到底是詐尸還是另有隱情,我是刑警寧澤腹忽,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布来累,位于F島的核電站,受9級(jí)特大地震影響窘奏,放射性物質(zhì)發(fā)生泄漏嘹锁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一着裹、第九天 我趴在偏房一處隱蔽的房頂上張望领猾。 院中可真熱鬧,春花似錦、人聲如沸瘤运。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拯坟。三九已至但金,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間郁季,已是汗流浹背冷溃。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留梦裂,地道東北人似枕。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像年柠,于是被迫代替她去往敵國和親凿歼。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345