文章目录

  • 1. context
  • 2. context.go
    • 2.0 结构图
    • 2.1 Context interface
    • 2.2 emptyCtx
    • 2.3 cancelCtx
    • 2.4 valueCtx
    • 2.5 timerCtx
  • 3. 使用示例
    • 3.1 WithCancel
    • 3.2 WithDeadline
    • 3.3 WithTimeout
    • 3.4 WithValue

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.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.TimeDeadline() (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 intfunc (*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 {Contextmu       sync.Mutex            // protects following fieldsdone     chan struct{}         // created lazily, closed by first cancel callchildren map[canceler]struct{} // set to nil by the first cancel callerr      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.donec.mu.Unlock()return d
}func (c *cancelCtx) Err() error {c.mu.Lock()err := c.errc.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 = errif 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 = nilc.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 {Contextkey, 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 {cancelCtxtimer *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 mainimport ("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())returndefault: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())// 向协程中传递ctxgo 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 passedreturn 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 mainimport ("context""fmt""time"
)func dl2(ctx context.Context) {n := 1for {select {case <-ctx.Done():fmt.Println(ctx.Err())returndefault:fmt.Println("dl2 : ", n)n++time.Sleep(time.Second)}}
}func dl1(ctx context.Context) {n := 1for {select {case <-ctx.Done():fmt.Println(ctx.Err())returndefault: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 mainimport ("context""fmt""time"
)func to1(ctx context.Context) {n := 1for {select {case <-ctx.Done():fmt.Println("to1 is over")returndefault: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 := 1for {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 {Contextkey, val interface{}
}

使用示例 :

package mainimport ("context""fmt""time"
)func v3(ctx context.Context) {for {select {case <-ctx.Done():fmt.Println("v3 Done : ", ctx.Err())returndefault: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

Golang 之context用法相关推荐

  1. 深入理解Golang之context

    深入理解Golang之context context是Go并发编程中常用到一种编程模式.本文将从为什么需要context,深入了解context的实现原理,以了解如何使用context. 作者:Tur ...

  2. Golang的context理解

    使用方法 context用于表示一个请求的上下文.一个网络请求,一般开启一个协程处理,而这个协程内部还会开启其它的协程继续处理.为了传递一个请求在不同协程中的处理情况(比如是否超时等),我们利用con ...

  3. Golang中context实现原理剖析

    转载: Go 并发控制context实现原理剖析 1. 前言 Golang context是Golang应用开发常用的并发控制技术,它与WaitGroup最大的不同点是context对于派生gorou ...

  4. golang库context学习

    context库 context最早的背景说明还是来源于官方的 博客,说明如下: 在Go服务器中,每个传入请求都在其自己的goroutine中进行处理. 请求处理程序通常会启动其他goroutine来 ...

  5. golang 上下文 Context

    上下文 context.Context Go 语言中用来设置截止日期.同步信号,传递请求相关值的结构体.上下文与 Goroutine 有比较密切的关系,是 Go 语言中独特的设计,在其他编程语言中我们 ...

  6. golang:context介绍

    我参与11月更文挑战的第10天,活动详情查看:2021最后一次更文挑战 1 前言 最近实现系统的分布式日志与事务管理时,在寻求所谓的全局唯一Goroutine ID无果之后,决定还是简单利用Conte ...

  7. golang包time用法详解

    在我们编程过程中,经常会用到与时间相关的各种务需求,下面来介绍 golang 中有关时间的一些基本用法,我们从 time 的几种 type 来开始介绍. 时间可分为时间点与时间段,golang 也不例 ...

  8. 深入解析Golang之Context

    ​context是什么 context翻译成中文就是上下文,在软件开发环境中,是指接口之间或函数调用之间,除了传递业务参数之外的额外信息,像在微服务环境中,传递追踪信息traceID, 请求接收和返回 ...

  9. golang 中fmt用法

    fmt包实现了格式化的I/O函数,这点类似C语言中的printf和scanf,但是更加简单. 占位符: 通用占位符: %v 值的默认格式.当打印结构体时,"加号"标记(%+v)会添 ...

最新文章

  1. 【译】五个ES6功能,让你编写代码更容易
  2. QEMU支持的网络模式
  3. 入行php 四年多了,写点自评.
  4. stm32使用stlink烧录后jlik烧不进去_【MCU实战经验】+用stm32单片机做J-Link和ST-Link...
  5. 饿了么上架iPhone 12:最快花呗10分钟拿到手
  6. ssm+安卓APP校园学生疫情防范监控系统毕业设计源码281444
  7. java数据清洗_做数据分析必须了解的获取数据与清洗数据技巧
  8. vue 后台管理系统富文本组件(四)UEditor(集成 135 编辑器插件)
  9. Javascript MS题蓄力:
  10. Storm概念详解和工作原理,topology、spout、bolt的细节和API讲解之一
  11. HDU 6638 Snowy Smile(线段树)
  12. html判断手机浏览器,JS判断浏览器iOS(iPhone)、Android手机移动端
  13. PLC PLSY 指令
  14. linux服务器清除cdn,Linux服务器中查找并删除大文件的五种方法,Linux系统清除文件内容的命令分享...
  15. 传统IDC为什么要转型?这里解释的很清楚了
  16. 灯泡(信息学奥赛一本通 1438)
  17. CSS的三种引入方式:外部样式、内部样式和行内样式
  18. 漏洞复现----42、Spring Cloud Gateway Actuator API SpEL表达式注入命令执行(CVE-2022-22947)
  19. 索尼笔记本 触摸屏 fn 快捷键处理
  20. 3.8寻找字符串中的手机号

热门文章

  1. 再见2020!送300本技术书籍&机械键盘
  2. Java堆是如何划分的?
  3. 推好单-好券app代理分享赚钱免费开通合伙人申请入口
  4. JS:1~100以内的质数和(2021-09-18 s)
  5. 小米未来将搭载鸿蒙系统,荣耀发布智慧屏将搭载鸿蒙系统小米电视的江湖老大地位还能保得住吗...
  6. android lun usb,LUN已映射至ESXi主机的场景
  7. Elixir 函数式编程语言
  8. 吞噬颜色html5游戏在线玩,《堡垒之夜》被黑洞吞噬,或将迎来大型更新
  9. Win10开机自动登录的方法
  10. python写入列表数据_python列表写入数据库