Context
上下文context.Context
是用来设置截止时间、同步信号,传递请求相关值的结构体。
context.Context
定义了四个需要实现的方法:
Deadline
: 返回 context.Context 被取消的时间。Done
: 返回一个 Channel,这个 Channel 会在当前工作完成或者上下文被取消后关闭,多次调用 Done 方法会返回同一个 Channel。Err
: 返回 context.Context 结束的原因,它只会在 Done 方法对应的 Channel 关闭时返回非空的值。- 如果 context.Context 被取消,会返回 Canceled 错误。
-如果 context.Context 超时,会返回 DeadlineExceeded 错误。
- 如果 context.Context 被取消,会返回 Canceled 错误。
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.WithDeadline
和context.WithTimeout
能创建被取消的的计时器上下文context.timerCtx
。context.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*/
}