Golang 之context库用法

1. context

Golang 中的context 是Go语言在 golang1.7 发布时新增的标准包

目的是增强Golang开发中并发控制技术

简单来讲当一个服务启动时,可能由此服务派生出多个多层级的 goroutine , 但是本质上来讲每个层级的 goroutine 都是平行调度使用,不存在goroutine '父子' 关系 , 当其中一个 goroutine 执行的任务被取消了或者处理超时了,那么其他被启动起来的Goroutine 都应该迅速退出,另外多个多层的Goroutine 想传递请求域的数据该如何处理?

如果单个请求的Goroutine 结构比较简单,或者处理起来也不麻烦,但是如果启动的Goroutine 是多个并且结构层次很深那么光是保障每个Goroutine 正常退出也不很容易了

为此Go1.7以来提供了 context 来解决类似的问题 , context 可以跟踪 Goroutine 的调用, 在调用内部维护一个调用树,通过这个调用树可以在传递超时或者退出通知,还能在调用树中传递元数据

context的中文翻译是上下文 ,我们可以理解为 context 管理了一组呈现树状结构的 Goroutine ,让每个Goroutine 都拥有相同的上下文,并且可以在这个上下文中传递数据

2. context.go

2.0 结构图
context.png

我们看一 context.go 的源文件了解一下context 的构成 该文件通常位于

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

context 实际上只是定义的4个方法的接口,凡是实现了该接口的都称为一种 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 {
    // 标识deadline是否已经设置了,没有设置时,ok的值是false,并返回初始的time.Time
    Deadline() (deadline time.Time, ok bool)
    // 返回一个channel, 当返回关闭的channel时可以执行一些操作
    Done() <-chan struct{}
    // 描述context关闭的原因,通常在Done()收到关闭通知之后才能知道原因
    Err() error
    // 获取上游Goroutine 传递给下游Goroutine的某些数据
    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 实现了Context 接口,但是其实现的方法都是空nil 那么我们就可以知道其实emptyCtx 是不具备任何实际功能的,那么它存在的目的是什么呢?

emptyCtx 存在的意义是作为 Context 对象树根节点 root节点 , 在context.go 包中提供 Background()TODO() 两个函数 ,这两个函数都是返回的都是 emptyCtx 实例 ,通常我们使用他们来构建Context的根节点 , 有了root根节点之后就可同事 context.go 包中提供的其他的包装函数创建具有意义的context 实例 ,并且没有context 实例的创建都是以上一个 context 实例对象作为参数的(所以必须有一个根节点) ,最终形成一个树状的管理结构

2.3 cancelCtx

定义了cancelCtx 类型的结构体

其中字段children 记录派生的child,当该类型的context(上下文) 被执行cancel是会将所有派生的child都执行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 结构知道仅是在Context 的基础上增加了元素 keyvalue

通常用于在层级协程之间传递数据

// 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 基础上增加了字段 timerdeadline

timer 触发自动cancel的定时器

deadline 标识最后执行cancel的时间

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个以 With 开头的函数, 这几个函数的主要功能是实例化不同类型的context

通过 Background()TODO() 创建最 emptyCtx 实例 ,通常是作为根节点

通过 WithCancel() 创建 cancelCtx 实例

通过 WithValue() 创建 valueCtx 实例

通过 WithDeadlineWithTimeout 创建 timerCtx 实例

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) {
    // 创建cancelCtx实例
    c := newCancelCtx(parent)
    // 添加到父节点的children中
    propagateCancel(parent, &c)
    // 返回实例和方法
    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 关闭原因
            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() {
    // 创建 cancelCtx 实例
    // 传入context.Background() 作为根节点
    ctx, cancel := context.WithCancel(context.Background())
    // 向协程中传递ctx
    go MyDo1(ctx)
    time.Sleep(5 * time.Second)
    fmt.Println("stop all goroutines")
    // 执行cancel操作
    cancel()
    time.Sleep(2 * time.Second)
}

3.2 WithDeadline

设置了deadlinecontext

这个deadline(最终期限) 表示context在指定的时刻结束

源码如下

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() {
    // 设置deadline为当前时间之后的5秒那个时刻
    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

实际就是调用了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() {
   // 设置为6秒后context结束
   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 基础上添加了 key : value 的键值对

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

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

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