// 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 int
func(*emptyCtx) Deadline() (deadline time.Time, ok bool) { return }
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. funcBackground() 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). funcTODO() Context { return todo }
// A cancelCtx can be canceled. When canceled, it also cancels any children // that implement canceler. type cancelCtx struct { Context
mu sync.Mutex // protects following fields done chanstruct{} // created lazily, closed by first cancel call children map[canceler]struct{} // set to nil by the first cancel call err error// set to non-nil by the first cancel call }
func(c *cancelCtx) Value(key interface{}) interface{} { if key == &cancelCtxKey { return c } return c.Context.Value(key) }
func(c *cancelCtx) Done() <-chanstruct{} { c.mu.Lock() if c.done == nil { c.done = make(chanstruct{}) } d := c.done c.mu.Unlock() return d }
// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to // implement Done and Err. It implements cancel by stopping its timer then // delegating to cancelCtx.cancel. type timerCtx struct { cancelCtx timer *time.Timer // Under cancelCtx.mu.
// 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 { Context key, val interface{} }
// stringify tries a bit to stringify v, without using fmt, since we don't // want context depending on the unicode tables. This is only used by // *valueCtx.String(). funcstringify(v interface{})string { switch s := v.(type) { case stringer: return s.String() casestring: return s } return"<not Stringer>" }
// parentCancelCtx returns the underlying *cancelCtx for parent. // It does this by looking up parent.Value(&cancelCtxKey) to find // the innermost enclosing *cancelCtx and then checking whether // parent.Done() matches that *cancelCtx. (If not, the *cancelCtx // has been wrapped in a custom implementation providing a // different done channel, in which case we should not bypass it.) // parentCancelCtx 负责从 parent Context 中取出底层的 cancelCtx funcparentCancelCtx(parent Context) (*cancelCtx, bool) { // 如果 parent context 的 done 为 nil 说明不支持 cancel,那么就不可能是 cancelCtx // 如果 parent context 的 done 为 可复用的 closedchan 说明 parent context 已经 cancel 了 // 此时取出 cancelCtx 没有意义(具体为啥没意义后面章节会有分析) done := parent.Done() if done == closedchan || done == nil { returnnil, false }
// WithDeadline returns a copy of the parent context with the deadline adjusted // to be no later than d. If the parent's deadline is already earlier than d, // WithDeadline(parent, d) is semantically equivalent to parent. The returned // context's Done channel is closed when the deadline expires, when the returned // cancel function is called, or when the parent context's Done channel is // closed, whichever happens first. // // Canceling this context releases resources associated with it, so code should // call cancel as soon as the operations running in this Context complete. funcWithDeadline(parent Context, d time.Time) (Context, CancelFunc) { // 与 cancelCtx 一样先检查一下 parent Context if parent == nil { panic("cannot create context from nil parent") }
// 判断 parent Context 是否支持 Deadline,如果支持的话需要判断 parent Context 的截止时间 // 假设 parent Context 的截止时间早于当前设置的截止时间,那就意味着 parent Context 肯定会先 // 被 cancel,同样由于 parent Context 的 cancel 会导致当前这个 child Context 也会被 cancel // 所以这时候直接返回一个 cancelCtx 就行了,计时器已经没有必要存在了 if cur, ok := parent.Deadline(); ok && cur.Before(d) { // The current deadline is already sooner than the new one. return WithCancel(parent) }
// WithValue returns a copy of parent in which the value associated with key is // val. // // Use context Values only for request-scoped data that transits processes and // APIs, not for passing optional parameters to functions. // // The provided key must be comparable and should not be of type // string or any other built-in type to avoid collisions between // packages using context. Users of WithValue should define their own // types for keys. To avoid allocating when assigning to an // interface{}, context keys often have concrete type // struct{}. Alternatively, exported context key variables' static // type should be a pointer or interface. // WithValue 方法负责创建 valueCtx funcWithValue(parent Context, key, val interface{}) Context { // parent 检测 if parent == nil { panic("cannot create context from nil parent") } // key 检测 if key == nil { panic("nil key") } // key 必须是可比较的 if !reflectlite.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 { Context key, val interface{} }
// stringify tries a bit to stringify v, without using fmt, since we don't // want context depending on the unicode tables. This is only used by // *valueCtx.String(). funcstringify(v interface{})string { switch s := v.(type) { case stringer: return s.String() casestring: return s } return"<not Stringer>" }