• 【golang】context详解


    简介

    context.Context 是golang中独特的涉及,可以用来用来设置截止日期、同步信号,传递请求相关值的结构体
    与 Goroutine 有比较密切的关系。

    在web程序中,每个Request都需要开启一个goroutine做一些事情,这些goroutine又可能会开启其他的
    goroutine去访问后端资源,比如数据库、RPC服务等,它们需要访问一些共享的资源,比如用户身份信息、认证token、请求截止时间等
    这时候可以通过Context,来跟踪这些goroutine,并且通过Context来控制它们,
    这就是Go语言为我们提供的Context,中文可以称之为“上下文”。

    Context 定义

    type Context interface {
    
        Deadline() (deadline time.Time, ok bool)
    
        Done() <-chan struct{}
    
        Err() error
    
        Value(key interface{}) interface{}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. Deadline方法是获取设置的截止时间的意思,第一个返回值是截止时间,到了这个时间点,Context会自动发起取消请求;
      第二个返回值ok==false时表示没有设置截止时间,如果需要取消的话,需要调用取消函数进行取消。
    2. Done方法返回一个只读的chan,类型为struct{},在goroutine中,如果该方法返回的chan可以读取,则意味着parent context已经发起了取消请求,
      我们通过Done方法收到这个信号后,就应该做清理操作,然后退出goroutine,释放资源。之后,Err 方法会返回一个错误,告知为什么 Context 被取消。
    3. Err方法返回取消的错误原因,因为什么Context被取消。
    4. Value方法获取该Context上绑定的值,是一个键值对,通过一个Key才可以获取对应的值,这个值一般是线程安全的。

    默认上下文

    context 包中最常用的方法还是 context.Background、context.TODO,这两个方法都会返回预先初始化好的私有变量 background 和 todo,
    它们会在同一个 Go 程序中被复用:

    func Background() Context {
    	return background
    }
    
    func TODO() Context {
    	return todo
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这两个私有变量都是通过 new(emptyCtx) 语句初始化的,它们是指向私有结构体 context.emptyCtx 的指针,这是最简单、最常用的上下文类型:

    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    从上述代码,我们不难发现 context.emptyCtx 通过返回 nil 实现了 context.Context 接口,它没有任何特殊的功能。
    从源代码来看,context.Background 和 context.TODO 函数其实也只是互为别名,没有太大的差别。它们只是在使用和语义上稍有不同:

    context.Background 是上下文的默认值,所有其他的上下文都应该从它衍生(Derived)出来;
    context.TODO 应该只在不确定应该使用哪种上下文时使用;
    在多数情况下,如果当前函数没有上下文作为入参,我们都会使用 context.Background 作为起始的上下文向下传递

    With 系列函数详解

    // 传递一个父Context作为参数,返回子Context,以及一个取消函数用来取消Context。
    func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
    // 和WithCancel差不多,它会多传递一个截止时间参数,意味着到了这个时间点,会自动取消Context,
    // 当然我们也可以不等到这个时候,可以提前通过取消函数进行取消。
    func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
    
    // WithTimeout和WithDeadline基本上一样,这个表示是超时自动取消,是多少时间后自动取消Context的意思
    func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
    
    //WithValue函数和取消Context无关,它是为了生成一个绑定了一个键值对数据的Context,
    // 绑定的数据可以通过Context.Value方法访问到,这是我们实际用经常要用到的技巧,一般我们想要通过上下文来传递数据时,可以通过这个方法,
    // 如我们需要tarce追踪系统调用栈的时候。
    func WithValue(parent Context, key, val interface{}) Context
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    WithCancel

    context.WithCancel 函数能够从 context.Context 中衍生出一个新的子上下文并返回用于取消该上下文的函数(CancelFunc)。
    一旦我们执行返回的取消函数,当前上下文以及它的子上下文都会被取消,所有的 Goroutine 都会同步收到这一取消信号。

    func newCancelCtx(parent Context) cancelCtx {
    	return cancelCtx{
    		Context: parent,
    		done:    make(chan struct{}),
    	}
    }
    
    func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    	c := newCancelCtx(parent)
    	propagateCancel(parent, &c)
    	return &c, func() { c.cancel(true, Canceled) }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    context.newCancelCtx 将传入的上下文包装成私有结构体 context.cancelCtx;
    context.propagateCancel 会构建父子上下文之间的关联,当父上下文被取消时,子上下文也会被取消:

    func propagateCancel(parent Context, child canceler) {
    	done := parent.Done()
    	if done == nil {
    		return // 父上下文不会触发取消信号
    	}
    	select {
    	case <-done:
    		child.cancel(false, parent.Err()) // 父上下文已经被取消
    		return
    	default:
    	}
    
    	if p, ok := parentCancelCtx(parent); ok {
    		p.mu.Lock()
    		if p.err != nil {
    			child.cancel(false, p.err)
    		} else {
    			p.children[child] = struct{}{}
    		}
    		p.mu.Unlock()
    	} else {
    		go func() {
    			select {
    			case <-parent.Done():
    				child.cancel(false, parent.Err())
    			case <-child.Done():
    			}
    		}()
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    上述函数总共与父上下文相关的三种不同的情况:

    1. 当 parent.Done() == nil,也就是 parent 不会触发取消事件时,当前函数会直接返回;
    2. 当 child 的继承链包含可以取消的上下文时,会判断 parent 是否已经触发了取消信号;
      • 如果已经被取消,child 会立刻被取消;
      • 如果没有被取消,child 会被加入 parent 的 children 列表中,等待 parent 释放取消信号;
    3. 在默认情况下
      运行一个新的 Goroutine 同时监听 parent.Done() 和 child.Done() 两个 Channel
      在 parent.Done() 关闭时调用 child.cancel 取消子上下文;
      context.propagateCancel 的作用是在 parent 和 child 之间同步取消和结束的信号,保证在 parent 被取消时,child 也会收到对应的信号,不会发生状态不一致的问题。

    context.cancelCtx 实现的几个接口方法也没有太多值得分析的地方,该结构体最重要的方法是 cancel,这个方法会关闭上下文中的 Channel 并向所有的子上下文同步取消信号:

    func (c *cancelCtx) cancel(removeFromParent bool, err error) {
    	c.mu.Lock()
    	if c.err != nil {
    		c.mu.Unlock()
    		return
    	}
    	c.err = err
    	if c.done == nil {
    		c.done = closedchan
    	} else {
    		close(c.done)
    	}
    	for child := range c.children {
    		child.cancel(false, err)
    	}
    	c.children = nil
    	c.mu.Unlock()
    
    	if removeFromParent {
    		removeChild(c.Context, c)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    WithTimeout

    在这段代码中,我们创建了一个过期时间为 1s 的上下文,并向上下文传入 handle 函数,该方法会使用 500ms 的时间处理传入的『请求』:

    func main() {
    	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    	defer cancel()
    
    	go handle(ctx, 500*time.Millisecond)
    	select {
    	case <-ctx.Done():
    		fmt.Println("main", ctx.Err())
    	}
    }
    
    func handle(ctx context.Context, duration time.Duration) {
    	select {
    	case <-ctx.Done():
    		fmt.Println("handle", ctx.Err())
    	case <-time.After(duration):
    		fmt.Println("process request with", duration)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    因为过期时间大于处理时间,所以我们有足够的时间处理该『请求』,运行上述代码会打印出如下所示的内容:

    $ go run context.go
    process request with 500ms
    main context deadline exceeded
    
    • 1
    • 2
    • 3

    handle 函数没有进入超时的 select 分支,但是 main 函数的 select 却会等待 context.Context 的超时并打印出 main context deadline exceeded。

    如果我们将处理『请求』时间增加至 1500ms,整个程序都会因为上下文的过期而被中止,:

    $ go run context.go
    main context deadline exceeded
    handle context deadline exceeded
    
    • 1
    • 2
    • 3

    多个 Goroutine 同时订阅 ctx.Done() 管道中的消息,一旦接收到取消信号就立刻停止当前正在执行的工作。

    使用原则

    • 不要把Context放在结构体中,要以参数的方式传递,parent Context一般为Background
    • 应该要把Context作为第一个参数传递给入口请求和出口请求链路上的每一个函数,放在第一位,变量名建议都统一,如ctx。
    • 给一个函数方法传递Context的时候,不要传递nil,否则在tarce追踪的时候,就会断了连接
    • Context的Value相关方法应该传递必须的数据,不要什么数据都使用这个传递
    • Context是线程安全的,可以放心的在多个goroutine中传递
    • 可以把一个 Context 对象传递给任意个数的 gorotuine,对它执行 取消 操作时,所有
      goroutine 都会接收到取消信号。

    小结

    Go 语言中的 context.Context 的主要作用还是在多个 Goroutine 组成的树中同步取消信号以减少
    对资源的消耗和占用,虽然它也有传值的功能,但是这个功能我们还是很少用到。

    在真正使用传值的功能时我们也应该非常谨慎,使用 context.Context 进行传递参数请求的所有参数一
    种非常差的设计,比较常见的使用场景是传递请求对应用户的认证令牌以及用于进行分布式追踪的请求 ID。

  • 相关阅读:
    QML 带框最大化显示方法
    JavaWeb-JDBC-Mybatis-Junit-Maven-Lombok
    Web前端-Vue2+Vue3基础入门到实战项目-Day3(生命周期, 案例-小黑记账清单, 工程化开发入门)
    C语言字符金字塔的输出方法
    ImageMagick命令注入漏洞(CVE-2016–3714)
    【Java 基础篇】Java Properties 详解:配置文件和键值对存储
    【教3妹学算法】特殊数组的特征值
    【MySQL】字符串截取函数 SUBSTR() 详解
    第十届蓝桥杯省赛C++C/研究生组,第十届蓝桥杯省赛JAVAC/研究生组——扫地机器人题解(二分)
    .net反射(Reflection)
  • 原文地址:https://blog.csdn.net/Merlin_feng/article/details/127939648