You have reached the world's edge, none but devils play past here
0%
golang context
Posted onEdited on Symbols count in article: 9.9kReading time ≈9 mins.
author
title
version
TheRiver
golang reflect
go version go1.15.2 darwin/amd64
context.Context
1 2 3 4 5 6 7 8 9 10
// A Context carries a deadline, a cancellation signal, and other values across // API boundaries. // // Context's methods may be called by multiple goroutines simultaneously. type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chanstruct{} Err() error Value(key interface{}) interface{} }
// 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. funcWithValue(parent Context, key, val interface{})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} }
type valueCtx struct { Context key, val interface{} }
// WithCancel returns a copy of parent with a new Done channel. The returned // context's Done channel is closed 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. funcWithCancel(parent Context)(ctx Context, cancel CancelFunc) { if parent == nil { panic("cannot create context from nil parent") } c := newCancelCtx(parent) propagateCancel(parent, &c) //var Canceled = errors.New("context canceled") return &c, func() { c.cancel(true, Canceled) } }
// 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)Done() <-chanstruct{} { c.mu.Lock() if c.done == nil { c.done = make(chanstruct{}) } d := c.done c.mu.Unlock() return d }
// A canceler is a context type that can be canceled directly. The // implementations are *cancelCtx and *timerCtx. type canceler interface { cancel(removeFromParent bool, err error) Done() <-chanstruct{} }
// 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 = err if c.done == nil { //var closedchan = make(chan struct{}) 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 = nil c.mu.Unlock()
if removeFromParent { removeChild(c.Context, c) } }
// removeChild removes a context from its parent. funcremoveChild(parent Context, child canceler) { p, ok := parentCancelCtx(parent) //第2层以下的为真 if !ok { return } p.mu.Lock() if p.children != nil { delete(p.children, child) } p.mu.Unlock() }
// 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.) funcparentCancelCtx(parent Context)(*cancelCtx, bool) { done := parent.Done() if done == closedchan || done == nil { returnnil, false } //这里value返回parent,转换成cancelCtx,按说本来就是cancelCtx。 //对于root的ctx这里类型转换会panic,不过前面if已经拒绝了root的ctx //也就是说这里是第2层以下的parent的ctx p, ok := parent.Value(&cancelCtxKey).(*cancelCtx) if !ok { returnnil, false } p.mu.Lock() ok = p.done == done p.mu.Unlock() if !ok { returnnil, false } return p, true }
func(c *cancelCtx)Value(key interface{})interface{} { if key == &cancelCtxKey { return c } return c.Context.Value(key) }
// propagateCancel arranges for child to be canceled when parent is. funcpropagateCancel(parent Context, child canceler) { done := parent.Done() if done == nil { return// parent is never canceled }
select { case <-done: // parent is already canceled child.cancel(false, parent.Err()) return default: }
if p, ok := parentCancelCtx(parent); ok { p.mu.Lock() if p.err != nil { // parent has already been canceled child.cancel(false, p.err) } else { if p.children == nil { p.children = make(map[canceler]struct{}) } p.children[child] = struct{}{} } p.mu.Unlock() } else { atomic.AddInt32(&goroutines, +1) gofunc() { select { case <-parent.Done(): child.cancel(false, parent.Err()) case <-child.Done(): } }() } }
// 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) { 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) } }
// 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.
deadline time.Time }
func(c *timerCtx)Deadline()(deadline time.Time, ok bool) { return c.deadline, true }