context包来源和作用
context包最早在golang.org/x/net/context中,在Go1.7时,正式被官方收入,进入标准库,目前路径为src/context/,目前context包已经在Go各个项目中被广泛使用。并且在Co中Context和并发编程有着密切的关系(context ,chan ,select,go这些个词经常密不可分)
其主要共能我列举如下:
- 跨服务,方法,进程的key,value传递
- 跨服务,方法,进程的超时控制
- 跨服务,方法,进程的取消执行
其主要的应用场景也非常多,我列举如下几个
- 全链路服务,日志追踪,记录
- 客户端,服务端方法调用超时控制
- 跨进程间延迟,取消信号,截至时间
在一些常见的Web服务中,比如Go自身携带的Http服务器中,客户端每发生一个请求,服务端都会开一个goroutine,在开启的这个goroutine中又会开启多goroutine处理不同的逻辑,Context就是把所有的goroutine串联起来,使之有一个统一的上下文,通过这么一个上下文,从而达到在多个goroutine之间携带Key,Value,资源控制,超时处理。
Context的定义
type Context interface {Deadline() (deadline time.Time, ok bool)Done() <-chan struct{}Err() errorValue(key interface{}) interface{}}type canceler interface {cancel(removeFromParent bool, err error)Done() <-chan struct{}}
Context是一个接口,定义了四个方法,这四个方法作用如下:
- Deadline,第一个出参 获取设置的截至时间点,第二个参数表示是否设置截至时间点
- Done,返回一个通道,如果这个通道可读,代表Context已经发起了取消,说白了这是一个信号传递,当从Done中能够读取出数据时,就该做一些工作了
- Err,返回Context取消的原因
- Value(key),返回Context的绑定的值,就是根据Key获取Value
canceler也是接口,这是一个取消器,定义了两个方法,作用如下:
- cancel(),取消Context,参数支持传入是否取消父Context,以及取消的原因
- Done 返回一个通道,如果这个通道可读,代表Context已经发起了取消。
Context的实现
Context接口的实现总共有四种(emptyCtx,timerCtx,cancelCtx,valueCtx),先从结构体定义看一下这几个的关系,这四个实现结构体彼此通过组合的关系可以实现功能的复用。timerCtx中包含了cancelCtx,所以timerCtx具备了取消的功能,cancelCtx以及valueCtx包含了Context,这个Context一般是根Context。type emptyCtx inttype timerCtx struct {cancelCtx //一个取消的cancelCtx(包含了cancelCtx)timer *time.Timer //计时的timerdeadline time.Time //超时时间}type cancelCtx struct {Context //包含了空的Context,这个一般是入参传入的emptyCtxmu sync.Mutex // 同步锁done chan struct{} //children map[canceler]struct{} // 子context的取消器err error // 第一个取消器取消的原因}type valueCtx struct {Context //包含了空的Context,这个一般是入参传入的emptyCtx,作为根Context//存储kv的结构,就是两个变量,不是想象中的map,因为只能存储一堆kv。key, val interface{} ,}
emptyCtx分析
一个emptyCtx,什么也不做,没有取消,没有超时,没有Value,为什么这个是int类型而不是struct?因为struct{}是一个空的指针,所有struct{}的地址都是一样的,但emptyCtx类型的变量需要有确切的地址,所有采用了int类型,emptyCtx实现的四个方法都是空的方法体,同时还有两个通过emptyCtx创建的全局Context,如下代码,backgroud一般是作为根的Context,用来派生其它子Context,todo比较好理解,就是你也没想好用来做什么,但总觉得需要一个context,就先用todo代理,后期再处理。
如下代码,就context包中提供的两个方法,用来返回background,todovar (background = new(emptyCtx)todo = new(emptyCtx))
func Background() Context {return background}func TODO() Context {return todo}
cancelCtx分析
cancelCtx的创建方法主要是WithCancel(),这个方法创建出一个子的上下文,并返回一个能够取消该上下文的函数,主要是方便开发人员手动在未来想要的某一时刻执行取消,从而达到信号通知,让其它协程能够给做一些例如资源清理或者中断的工作。
我们看cancelCtx实现的Done方法,这个方法是cancelCtx和timerCtx通用的,因为timerCtx结构体中包含了cancelCtx,所以timerCtx就不需要再实现了,下文讲到的cancelCtx就只会实现Deadline方法了。func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {c := newCancelCtx(parent)//检测父Context是否执行取消,如果执行,则该父Context的的所有子Context也执行cancel()方法,负责,开启协程等待父Context执行cancelpropagateCancel(parent, &c)//返回子上下文,和取消函数return &c, func() { c.cancel(true, Canceled) }}func newCancelCtx(parent Context) cancelCtx {return cancelCtx{Context: parent}}
我们主要看看返回的cancel函数,这个函数非常重,因为在他里面实现了取消的逻辑,它是congtext能够取消的核心,核心代码如下func (c *cancelCtx) Done() <-chan struct{} {c.mu.Lock()//创建一个chan通道,然后返回,很简单的if c.done == nil {c.done = make(chan struct{})}d := c.donec.mu.Unlock()return d}func (c *cancelCtx) Err() error {c.mu.Lock()//返回context取消的原因err := c.errc.mu.Unlock()return err}
func (c *cancelCtx) cancel(removeFromParent bool, err error) {c.mu.Lock()// 只要err不为空,就代表一定取消了,因为只有取消了,才会有取消的原因if c.err != nil {c.mu.Unlock()return}c.err = err//关闭done()方法返回的通道if c.done == nil {c.done = closedchan} else {//取消context,关闭chan时,会返回一个空的结构体{}close(c.done)}//遍历congtext的子context,循环取消for child := range c.children {// NOTE: acquiring the child's lock while holding parent's lock.child.cancel(false, err)}c.children = nilc.mu.Unlock()//是否从父context的把自己移除,这个一搬是true,除非很明确没有父contextif removeFromParent {removeChild(c.Context, c)}}
timerCtx分析
timeCtx的创建有两种方法:WithTimeout和WithDeadline,其i中WithTimeout中单纯的调用了WithDeadline,并没有做其它处理,从名字大家或多或少可以猜出timeCtx的核心就是做超时,延迟。两个比较相似,但具体实现上略有不同,WithDeadline创建过程的代码如下:
看完代码,核心其实比较简单,一判断父context是否也是deadline,时间是否在传入的时间之前,二判断父context是否已经取消,如果取消,则子的context也全部取消,两个条件判断完成之后,启动计时器,返回deadline和cancel,一个是用户主动调用cancel取消,一个是时间到达之后回调cancel取消。func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {if cur, ok := parent.Deadline(); ok && cur.Before(d) {// 当前的截止日期已经比新的截止日期早,则直接取消return WithCancel(parent)}//创建一个取消的cancelCtxc := &timerCtx{//从这可以看出timerCtx是包含了cancelCtx的功能的cancelCtx: newCancelCtx(parent),deadline: d,}propagateCancel(parent, c) //传播canceldur := time.Until(d)if dur <= 0 {c.cancel(true, DeadlineExceeded) // 截至时间已经过去return c, func() { c.cancel(false, Canceled) }}c.mu.Lock()defer c.mu.Unlock()if c.err == nil {c.timer = time.AfterFunc(dur, func() {//开启计时,到时间点后回调funcc.cancel(true, DeadlineExceeded)})}return c, func() { c.cancel(true, Canceled) }}
我们再来看一看timerCtx实现的Deadline方法,其实很简单,就是返回到期的时间以及一个true标识符
这个其实是emptyCtx的空实现,因为timerCtx本身没有存储key,value的设计//只有这个方法采是timerCtx特有实现的的方法func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {return c.deadline, true}
我们再来看看timerCtx的取消方法是如何实现的,首先明确timerCtx有两种取消的办法,一个是手动取消,一个是计时器到时间了自动取消,调用的都是这个方法.func (*emptyCtx) Value(key interface{}) interface{} {return nil}
func (c *timerCtx) cancel(removeFromParent bool, err error) {//内部还是调用了cancelCtx的取消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()}
valueCtx分析
valueCtx的创建方法是WithValue(key,value interface{}),这个方法只会返回一个子的上下文,方法入参是一对kv,这个kv会被存储到这个子上下文中,其存储结构就是两个变量,可以看上面的valueCtx结构体,这个子上下文可以调用Value(key interface{})根据key获取value。
通过context的Value方法可以根据key获取value,看看这个方法的实现,其实很简单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}}
func (c *valueCtx) Value(key interface{}) interface{} {//如果key相等,返回vif c.key == key {return c.val}//递归调用,直到最后调用到emptyCtx,找不到返回nilreturn c.Context.Value(key)}
Context包Demo实践
我们通过4个实践context各种功能,以便大家能够理解
context的超时
context的取消func ContextWithTimeOut() {ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)//开启新的协程go func(ctx context.Context) {//模拟处理任务time.Sleep(10 * time.Second)fmt.Println("任务处理完成")}(ctx)select {case <-ctx.Done():fmt.Println("context timeout")}}
context获取key valuefunc ContextWithCancel() {wg := sync.WaitGroup{}wg.Add(1)ctx, cancel := context.WithCancel(context.Background())go func(ctx context.Context) {select {case c := <-ctx.Done():{fmt.Println("context被手动取消了")wg.Done()}}}(ctx)//手动取消cancel()wg.Wait()}
context的延迟取消//上下文key value,根据key获取,//当子context 获取不到对应key时,就取父的context获取func ContextWithValue() {wg := sync.WaitGroup{}wg.Add(1)ctx := context.WithValue(nil, "key", "value")go func(ctx context.Context) {ctxB := context.WithValue(ctx, "key1", "value1")v := ctx.Value("key")v1 := ctxB.Value("key1")v2 := ctxB.Value("key") //这个时候会获取ctxA的keyfmt.Println(v, v1, v2)wg.Done()}(ctx)wg.Wait()}
//可延迟一定的时间取消,也可以手动编码取消func ContextWithDeadLine() {wg := &sync.WaitGroup{}wg.Add(1)ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(3))go func(wg *sync.WaitGroup, ctx context.Context) {select {case <-ctx.Done():fmt.Println("ctx deadline done")wg.Done()}}(wg, ctx)wg.Wait()}
欢迎大家关注微信公众号:“技术人技术事”,更多精彩期待你的到来
**
