go context 源码刨析(二)

Context

上下文context.Context 是用来设置截止时间、同步信号,传递请求相关值的结构体。

context.Context 定义了四个需要实现的方法:

  • Deadline: 返回 context.Context 被取消的时间。
  • Done: 返回一个 Channel,这个 Channel 会在当前工作完成或者上下文被取消后关闭,多次调用 Done 方法会返回同一个 Channel。
  • Err: 返回 context.Context 结束的原因,它只会在 Done 方法对应的 Channel 关闭时返回非空的值。
    • 如果 context.Context 被取消,会返回 Canceled 错误。
      -如果 context.Context 超时,会返回 DeadlineExceeded 错误。
  • Value: 从 context.Context 中获取键对应的值,对于同一个上下文来说,多次调用 Value 并传入相同的 Key 会返回相同的结果,该方法可以用来传递请求特定的数据。
go 复制代码
type Context interface {
        Deadline() (deadline time.Time, ok bool)
        Done() <-chan struct{}
        Err() error
        Value(key any) any
}

过期信号

context.WithDeadlinecontext.WithTimeout能创建被取消的的计时器上下文context.timerCtxcontext.WithTimeout只是调用了context.WithDeadline

go 复制代码
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
        return WithDeadline(parent, time.Now().Add(timeout))
}
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
        if parent == nil {
                panic("cannot create context from nil parent")
        }
        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) }
}

context.WithDeadline 在创建 context.timerCtx 的过程中判断了父上下文的截止日期与当前日期,并通过 time.AfterFunc 创建定时器,当时间超过了截止日期后会调用 context.timerCtx.cancel 同步取消信号。

context.timerCtx 内部不仅通过嵌入 context.cancelCtx 结构体继承了相关的变量和方法,还通过持有的定时器 timer 和截止时间 deadline 实现了定时取消的功能。

go 复制代码
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) 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()
}

一般用 withTimeout比较多:

go 复制代码
		ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
		defer cancel()

传值方法

context.WithValue能从父上下文中创建一个子上下文,传值的子上下文使用context.valueCtx类型。

这个方法笔者没有在真实生产中用过,用的都是 grpc 的 metadata

go 复制代码
func WithValue(parent Context, key, val any) Context {
        if parent == nil {
                panic("cannot create context from nil parent")
        }
        if key == nil {
                panic("nil key")
        }
        if !reflectlite.TypeOf(key).Comparable() {
                panic("key is not comparable")
        }
        return &valueCtx{parent, key, val}
}

key 必须是可比较的,并且不应该是string或者内部自带的类型。

go 复制代码
摘自: https: //github.com/golang/lint/blob/0da02fd607244e7cc2c7dd759075c1ac3ebfa759/testdata/contextkeytypes.go
func _() {
    c := context.Background()
    context.WithValue(c, "foo", "bar")          // MATCH /should not use basic type( untyped|)? string as key in context.WithValue/
    context.WithValue(c, true, "bar")           // MATCH /should not use basic type( untyped|)? bool as key in context.WithValue/
    context.WithValue(c, 1, "bar")              // MATCH /should not use basic type( untyped|)? int as key in context.WithValue/
    context.WithValue(c, int8(1), "bar")        // MATCH /should not use basic type int8 as key in context.WithValue/
    context.WithValue(c, int16(1), "bar")       // MATCH /should not use basic type int16 as key in context.WithValue/
    context.WithValue(c, int32(1), "bar")       // MATCH /should not use basic type int32 as key in context.WithValue/
    context.WithValue(c, rune(1), "bar")        // MATCH /should not use basic type rune as key in context.WithValue/
    context.WithValue(c, int64(1), "bar")       // MATCH /should not use basic type int64 as key in context.WithValue/
    context.WithValue(c, uint(1), "bar")        // MATCH /should not use basic type uint as key in context.WithValue/
    context.WithValue(c, uint8(1), "bar")       // MATCH /should not use basic type uint8 as key in context.WithValue/
    context.WithValue(c, byte(1), "bar")        // MATCH /should not use basic type byte as key in context.WithValue/
    context.WithValue(c, uint16(1), "bar")      // MATCH /should not use basic type uint16 as key in context.WithValue/
    context.WithValue(c, uint32(1), "bar")      // MATCH /should not use basic type uint32 as key in context.WithValue/
    context.WithValue(c, uint64(1), "bar")      // MATCH /should not use basic type uint64 as key in context.WithValue/
    context.WithValue(c, uintptr(1), "bar")     // MATCH /should not use basic type uintptr as key in context.WithValue/
    context.WithValue(c, float32(1.0), "bar")   // MATCH /should not use basic type float32 as key in context.WithValue/
    context.WithValue(c, float64(1.0), "bar")   // MATCH /should not use basic type float64 as key in context.WithValue/
    context.WithValue(c, complex64(1i), "bar")  // MATCH /should not use basic type complex64 as key in context.WithValue/
    context.WithValue(c, complex128(1i), "bar") // MATCH /should not use basic type complex128 as key in context.WithValue/
    context.WithValue(c, ctxKey{}, "bar")       // ok
    context.WithValue(c, &ctxKey{}, "bar")      // ok*/
}
相关推荐
Change is good3 分钟前
python: 数字类型的一些函数
开发语言·python·算法
卡卡卡卡罗特3 分钟前
naocs注册中心,配置管理,openfeign在idea中实现模块间的调用,getway的使用
java·开发语言
星迹日7 分钟前
Java: 数据类型与变量和运算符
java·开发语言·经验分享·笔记
六点半8888 分钟前
【C++】vector 常用成员函数的模拟实现
开发语言·c++·算法
Kalika0-09 分钟前
输出不能被3整除的数-C语言
c语言·开发语言
knoci1 小时前
【Go】-基于Gin框架的IM通信项目
开发语言·后端·学习·golang·gin
RaidenQ1 小时前
2024.9.27 Python面试八股文
linux·开发语言·python
Thomas_YXQ3 小时前
Unity3D PostLateUpdate为何突然占用大量时间详解
开发语言·数码相机·游戏·unity·架构·unity3d
高高要努力3 小时前
SpringBoot日志集成-LogBack
spring boot·后端·logback
Pandaconda5 小时前
【计算机网络 - 基础问题】每日 3 题(二十七)
开发语言·经验分享·笔记·后端·计算机网络·面试·职场和发展