github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/context/context.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package context defines the Context type, which carries deadlines,
     6  // cancellation signals, and other request-scoped values across API boundaries
     7  // and between processes.
     8  //
     9  // Incoming requests to a server should create a Context, and outgoing
    10  // calls to servers should accept a Context. The chain of function
    11  // calls between them must propagate the Context, optionally replacing
    12  // it with a derived Context created using WithCancel, WithDeadline,
    13  // WithTimeout, or WithValue. When a Context is canceled, all
    14  // Contexts derived from it are also canceled.
    15  //
    16  // The WithCancel, WithDeadline, and WithTimeout functions take a
    17  // Context (the parent) and return a derived Context (the child) and a
    18  // CancelFunc. Calling the CancelFunc cancels the child and its
    19  // children, removes the parent's reference to the child, and stops
    20  // any associated timers. Failing to call the CancelFunc leaks the
    21  // child and its children until the parent is canceled or the timer
    22  // fires. The go vet tool checks that CancelFuncs are used on all
    23  // control-flow paths.
    24  //
    25  // Programs that use Contexts should follow these rules to keep interfaces
    26  // consistent across packages and enable static analysis tools to check context
    27  // propagation:
    28  //
    29  // Do not store Contexts inside a struct type; instead, pass a Context
    30  // explicitly to each function that needs it. The Context should be the first
    31  // parameter, typically named ctx:
    32  //
    33  // 	func DoSomething(ctx context.Context, arg Arg) error {
    34  // 		// ... use ctx ...
    35  // 	}
    36  //
    37  // Do not pass a nil Context, even if a function permits it. Pass context.TODO
    38  // if you are unsure about which Context to use.
    39  //
    40  // Use context Values only for request-scoped data that transits processes and
    41  // APIs, not for passing optional parameters to functions.
    42  //
    43  // The same Context may be passed to functions running in different goroutines;
    44  // Contexts are safe for simultaneous use by multiple goroutines.
    45  //
    46  // See https://blog.golang.org/context for example code for a server that uses
    47  // Contexts.
    48  package context
    49  
    50  import (
    51  	"errors"
    52  	"internal/reflectlite"
    53  	"sync"
    54  	"sync/atomic"
    55  	"time"
    56  )
    57  
    58  // A Context carries a deadline, a cancellation signal, and other values across
    59  // API boundaries.
    60  //
    61  // Context's methods may be called by multiple goroutines simultaneously.
    62  type Context interface {
    63  	// Deadline returns the time when work done on behalf of this context
    64  	// should be canceled. Deadline returns ok==false when no deadline is
    65  	// set. Successive calls to Deadline return the same results.
    66  	Deadline() (deadline time.Time, ok bool)
    67  
    68  	// Done returns a channel that's closed when work done on behalf of this
    69  	// context should be canceled. Done may return nil if this context can
    70  	// never be canceled. Successive calls to Done return the same value.
    71  	// The close of the Done channel may happen asynchronously,
    72  	// after the cancel function returns.
    73  	//
    74  	// WithCancel arranges for Done to be closed when cancel is called;
    75  	// WithDeadline arranges for Done to be closed when the deadline
    76  	// expires; WithTimeout arranges for Done to be closed when the timeout
    77  	// elapses.
    78  	//
    79  	// Done is provided for use in select statements:
    80  	//
    81  	//  // Stream generates values with DoSomething and sends them to out
    82  	//  // until DoSomething returns an error or ctx.Done is closed.
    83  	//  func Stream(ctx context.Context, out chan<- Value) error {
    84  	//  	for {
    85  	//  		v, err := DoSomething(ctx)
    86  	//  		if err != nil {
    87  	//  			return err
    88  	//  		}
    89  	//  		select {
    90  	//  		case <-ctx.Done():
    91  	//  			return ctx.Err()
    92  	//  		case out <- v:
    93  	//  		}
    94  	//  	}
    95  	//  }
    96  	//
    97  	// See https://blog.golang.org/pipelines for more examples of how to use
    98  	// a Done channel for cancellation.
    99  	Done() <-chan struct{}
   100  
   101  	// If Done is not yet closed, Err returns nil.
   102  	// If Done is closed, Err returns a non-nil error explaining why:
   103  	// Canceled if the context was canceled
   104  	// or DeadlineExceeded if the context's deadline passed.
   105  	// After Err returns a non-nil error, successive calls to Err return the same error.
   106  	Err() error
   107  
   108  	// Value returns the value associated with this context for key, or nil
   109  	// if no value is associated with key. Successive calls to Value with
   110  	// the same key returns the same result.
   111  	//
   112  	// Use context values only for request-scoped data that transits
   113  	// processes and API boundaries, not for passing optional parameters to
   114  	// functions.
   115  	//
   116  	// A key identifies a specific value in a Context. Functions that wish
   117  	// to store values in Context typically allocate a key in a global
   118  	// variable then use that key as the argument to context.WithValue and
   119  	// Context.Value. A key can be any type that supports equality;
   120  	// packages should define keys as an unexported type to avoid
   121  	// collisions.
   122  	//
   123  	// Packages that define a Context key should provide type-safe accessors
   124  	// for the values stored using that key:
   125  	//
   126  	// 	// Package user defines a User type that's stored in Contexts.
   127  	// 	package user
   128  	//
   129  	// 	import "context"
   130  	//
   131  	// 	// User is the type of value stored in the Contexts.
   132  	// 	type User struct {...}
   133  	//
   134  	// 	// key is an unexported type for keys defined in this package.
   135  	// 	// This prevents collisions with keys defined in other packages.
   136  	// 	type key int
   137  	//
   138  	// 	// userKey is the key for user.User values in Contexts. It is
   139  	// 	// unexported; clients use user.NewContext and user.FromContext
   140  	// 	// instead of using this key directly.
   141  	// 	var userKey key
   142  	//
   143  	// 	// NewContext returns a new Context that carries value u.
   144  	// 	func NewContext(ctx context.Context, u *User) context.Context {
   145  	// 		return context.WithValue(ctx, userKey, u)
   146  	// 	}
   147  	//
   148  	// 	// FromContext returns the User value stored in ctx, if any.
   149  	// 	func FromContext(ctx context.Context) (*User, bool) {
   150  	// 		u, ok := ctx.Value(userKey).(*User)
   151  	// 		return u, ok
   152  	// 	}
   153  	Value(key interface{}) interface{}
   154  }
   155  
   156  // Canceled is the error returned by Context.Err when the context is canceled.
   157  var Canceled = errors.New("context canceled")
   158  
   159  // DeadlineExceeded is the error returned by Context.Err when the context's
   160  // deadline passes.
   161  var DeadlineExceeded error = deadlineExceededError{}
   162  
   163  type deadlineExceededError struct{}
   164  
   165  func (deadlineExceededError) Error() string   { return "context deadline exceeded" }
   166  func (deadlineExceededError) Timeout() bool   { return true }
   167  func (deadlineExceededError) Temporary() bool { return true }
   168  
   169  // An emptyCtx is never canceled, has no values, and has no deadline. It is not
   170  // struct{}, since vars of this type must have distinct addresses.
   171  type emptyCtx int
   172  
   173  func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
   174  	return
   175  }
   176  
   177  func (*emptyCtx) Done() <-chan struct{} {
   178  	return nil
   179  }
   180  
   181  func (*emptyCtx) Err() error {
   182  	return nil
   183  }
   184  
   185  func (*emptyCtx) Value(key interface{}) interface{} {
   186  	return nil
   187  }
   188  
   189  func (e *emptyCtx) String() string {
   190  	switch e {
   191  	case background:
   192  		return "context.Background"
   193  	case todo:
   194  		return "context.TODO"
   195  	}
   196  	return "unknown empty Context"
   197  }
   198  
   199  var (
   200  	background = new(emptyCtx)
   201  	todo       = new(emptyCtx)
   202  )
   203  
   204  // Background returns a non-nil, empty Context. It is never canceled, has no
   205  // values, and has no deadline. It is typically used by the main function,
   206  // initialization, and tests, and as the top-level Context for incoming
   207  // requests.
   208  func Background() Context {
   209  	return background
   210  }
   211  
   212  // TODO returns a non-nil, empty Context. Code should use context.TODO when
   213  // it's unclear which Context to use or it is not yet available (because the
   214  // surrounding function has not yet been extended to accept a Context
   215  // parameter).
   216  func TODO() Context {
   217  	return todo
   218  }
   219  
   220  // A CancelFunc tells an operation to abandon its work.
   221  // A CancelFunc does not wait for the work to stop.
   222  // A CancelFunc may be called by multiple goroutines simultaneously.
   223  // After the first call, subsequent calls to a CancelFunc do nothing.
   224  type CancelFunc func()
   225  
   226  // WithCancel returns a copy of parent with a new Done channel. The returned
   227  // context's Done channel is closed when the returned cancel function is called
   228  // or when the parent context's Done channel is closed, whichever happens first.
   229  //
   230  // Canceling this context releases resources associated with it, so code should
   231  // call cancel as soon as the operations running in this Context complete.
   232  func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
   233  	if parent == nil {
   234  		panic("cannot create context from nil parent")
   235  	}
   236  	c := newCancelCtx(parent)
   237  	propagateCancel(parent, &c)
   238  	return &c, func() { c.cancel(true, Canceled) }
   239  }
   240  
   241  // newCancelCtx returns an initialized cancelCtx.
   242  func newCancelCtx(parent Context) cancelCtx {
   243  	return cancelCtx{Context: parent}
   244  }
   245  
   246  // goroutines counts the number of goroutines ever created; for testing.
   247  var goroutines int32
   248  
   249  // propagateCancel arranges for child to be canceled when parent is.
   250  func propagateCancel(parent Context, child canceler) {
   251  	done := parent.Done()
   252  	if done == nil {
   253  		return // parent is never canceled
   254  	}
   255  
   256  	select {
   257  	case <-done:
   258  		// parent is already canceled
   259  		child.cancel(false, parent.Err())
   260  		return
   261  	default:
   262  	}
   263  
   264  	if p, ok := parentCancelCtx(parent); ok {
   265  		p.mu.Lock()
   266  		if p.err != nil {
   267  			// parent has already been canceled
   268  			child.cancel(false, p.err)
   269  		} else {
   270  			if p.children == nil {
   271  				p.children = make(map[canceler]struct{})
   272  			}
   273  			p.children[child] = struct{}{}
   274  		}
   275  		p.mu.Unlock()
   276  	} else {
   277  		atomic.AddInt32(&goroutines, +1)
   278  		go func() {
   279  			select {
   280  			case <-parent.Done():
   281  				child.cancel(false, parent.Err())
   282  			case <-child.Done():
   283  			}
   284  		}()
   285  	}
   286  }
   287  
   288  // &cancelCtxKey is the key that a cancelCtx returns itself for.
   289  var cancelCtxKey int
   290  
   291  // parentCancelCtx returns the underlying *cancelCtx for parent.
   292  // It does this by looking up parent.Value(&cancelCtxKey) to find
   293  // the innermost enclosing *cancelCtx and then checking whether
   294  // parent.Done() matches that *cancelCtx. (If not, the *cancelCtx
   295  // has been wrapped in a custom implementation providing a
   296  // different done channel, in which case we should not bypass it.)
   297  func parentCancelCtx(parent Context) (*cancelCtx, bool) {
   298  	done := parent.Done()
   299  	if done == closedchan || done == nil {
   300  		return nil, false
   301  	}
   302  	p, ok := parent.Value(&cancelCtxKey).(*cancelCtx)
   303  	if !ok {
   304  		return nil, false
   305  	}
   306  	p.mu.Lock()
   307  	ok = p.done == done
   308  	p.mu.Unlock()
   309  	if !ok {
   310  		return nil, false
   311  	}
   312  	return p, true
   313  }
   314  
   315  // removeChild removes a context from its parent.
   316  func removeChild(parent Context, child canceler) {
   317  	p, ok := parentCancelCtx(parent)
   318  	if !ok {
   319  		return
   320  	}
   321  	p.mu.Lock()
   322  	if p.children != nil {
   323  		delete(p.children, child)
   324  	}
   325  	p.mu.Unlock()
   326  }
   327  
   328  // A canceler is a context type that can be canceled directly. The
   329  // implementations are *cancelCtx and *timerCtx.
   330  type canceler interface {
   331  	cancel(removeFromParent bool, err error)
   332  	Done() <-chan struct{}
   333  }
   334  
   335  // closedchan is a reusable closed channel.
   336  var closedchan = make(chan struct{})
   337  
   338  func init() {
   339  	close(closedchan)
   340  }
   341  
   342  // A cancelCtx can be canceled. When canceled, it also cancels any children
   343  // that implement canceler.
   344  type cancelCtx struct {
   345  	Context
   346  
   347  	mu       sync.Mutex            // protects following fields
   348  	done     chan struct{}         // created lazily, closed by first cancel call
   349  	children map[canceler]struct{} // set to nil by the first cancel call
   350  	err      error                 // set to non-nil by the first cancel call
   351  }
   352  
   353  func (c *cancelCtx) Value(key interface{}) interface{} {
   354  	if key == &cancelCtxKey {
   355  		return c
   356  	}
   357  	return c.Context.Value(key)
   358  }
   359  
   360  func (c *cancelCtx) Done() <-chan struct{} {
   361  	c.mu.Lock()
   362  	if c.done == nil {
   363  		c.done = make(chan struct{})
   364  	}
   365  	d := c.done
   366  	c.mu.Unlock()
   367  	return d
   368  }
   369  
   370  func (c *cancelCtx) Err() error {
   371  	c.mu.Lock()
   372  	err := c.err
   373  	c.mu.Unlock()
   374  	return err
   375  }
   376  
   377  type stringer interface {
   378  	String() string
   379  }
   380  
   381  func contextName(c Context) string {
   382  	if s, ok := c.(stringer); ok {
   383  		return s.String()
   384  	}
   385  	return reflectlite.TypeOf(c).String()
   386  }
   387  
   388  func (c *cancelCtx) String() string {
   389  	return contextName(c.Context) + ".WithCancel"
   390  }
   391  
   392  // cancel closes c.done, cancels each of c's children, and, if
   393  // removeFromParent is true, removes c from its parent's children.
   394  func (c *cancelCtx) cancel(removeFromParent bool, err error) {
   395  	if err == nil {
   396  		panic("context: internal error: missing cancel error")
   397  	}
   398  	c.mu.Lock()
   399  	if c.err != nil {
   400  		c.mu.Unlock()
   401  		return // already canceled
   402  	}
   403  	c.err = err
   404  	if c.done == nil {
   405  		c.done = closedchan
   406  	} else {
   407  		close(c.done)
   408  	}
   409  	for child := range c.children {
   410  		// NOTE: acquiring the child's lock while holding parent's lock.
   411  		child.cancel(false, err)
   412  	}
   413  	c.children = nil
   414  	c.mu.Unlock()
   415  
   416  	if removeFromParent {
   417  		removeChild(c.Context, c)
   418  	}
   419  }
   420  
   421  // WithDeadline returns a copy of the parent context with the deadline adjusted
   422  // to be no later than d. If the parent's deadline is already earlier than d,
   423  // WithDeadline(parent, d) is semantically equivalent to parent. The returned
   424  // context's Done channel is closed when the deadline expires, when the returned
   425  // cancel function is called, or when the parent context's Done channel is
   426  // closed, whichever happens first.
   427  //
   428  // Canceling this context releases resources associated with it, so code should
   429  // call cancel as soon as the operations running in this Context complete.
   430  func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
   431  	if parent == nil {
   432  		panic("cannot create context from nil parent")
   433  	}
   434  	if cur, ok := parent.Deadline(); ok && cur.Before(d) {
   435  		// The current deadline is already sooner than the new one.
   436  		return WithCancel(parent)
   437  	}
   438  	c := &timerCtx{
   439  		cancelCtx: newCancelCtx(parent),
   440  		deadline:  d,
   441  	}
   442  	propagateCancel(parent, c)
   443  	dur := time.Until(d)
   444  	if dur <= 0 {
   445  		c.cancel(true, DeadlineExceeded) // deadline has already passed
   446  		return c, func() { c.cancel(false, Canceled) }
   447  	}
   448  	c.mu.Lock()
   449  	defer c.mu.Unlock()
   450  	if c.err == nil {
   451  		c.timer = time.AfterFunc(dur, func() {
   452  			c.cancel(true, DeadlineExceeded)
   453  		})
   454  	}
   455  	return c, func() { c.cancel(true, Canceled) }
   456  }
   457  
   458  // A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
   459  // implement Done and Err. It implements cancel by stopping its timer then
   460  // delegating to cancelCtx.cancel.
   461  type timerCtx struct {
   462  	cancelCtx
   463  	timer *time.Timer // Under cancelCtx.mu.
   464  
   465  	deadline time.Time
   466  }
   467  
   468  func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
   469  	return c.deadline, true
   470  }
   471  
   472  func (c *timerCtx) String() string {
   473  	return contextName(c.cancelCtx.Context) + ".WithDeadline(" +
   474  		c.deadline.String() + " [" +
   475  		time.Until(c.deadline).String() + "])"
   476  }
   477  
   478  func (c *timerCtx) cancel(removeFromParent bool, err error) {
   479  	c.cancelCtx.cancel(false, err)
   480  	if removeFromParent {
   481  		// Remove this timerCtx from its parent cancelCtx's children.
   482  		removeChild(c.cancelCtx.Context, c)
   483  	}
   484  	c.mu.Lock()
   485  	if c.timer != nil {
   486  		c.timer.Stop()
   487  		c.timer = nil
   488  	}
   489  	c.mu.Unlock()
   490  }
   491  
   492  // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
   493  //
   494  // Canceling this context releases resources associated with it, so code should
   495  // call cancel as soon as the operations running in this Context complete:
   496  //
   497  // 	func slowOperationWithTimeout(ctx context.Context) (Result, error) {
   498  // 		ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
   499  // 		defer cancel()  // releases resources if slowOperation completes before timeout elapses
   500  // 		return slowOperation(ctx)
   501  // 	}
   502  func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
   503  	return WithDeadline(parent, time.Now().Add(timeout))
   504  }
   505  
   506  // WithValue returns a copy of parent in which the value associated with key is
   507  // val.
   508  //
   509  // Use context Values only for request-scoped data that transits processes and
   510  // APIs, not for passing optional parameters to functions.
   511  //
   512  // The provided key must be comparable and should not be of type
   513  // string or any other built-in type to avoid collisions between
   514  // packages using context. Users of WithValue should define their own
   515  // types for keys. To avoid allocating when assigning to an
   516  // interface{}, context keys often have concrete type
   517  // struct{}. Alternatively, exported context key variables' static
   518  // type should be a pointer or interface.
   519  func WithValue(parent Context, key, val interface{}) Context {
   520  	if parent == nil {
   521  		panic("cannot create context from nil parent")
   522  	}
   523  	if key == nil {
   524  		panic("nil key")
   525  	}
   526  	if !reflectlite.TypeOf(key).Comparable() {
   527  		panic("key is not comparable")
   528  	}
   529  	return &valueCtx{parent, key, val}
   530  }
   531  
   532  // A valueCtx carries a key-value pair. It implements Value for that key and
   533  // delegates all other calls to the embedded Context.
   534  type valueCtx struct {
   535  	Context
   536  	key, val interface{}
   537  }
   538  
   539  // stringify tries a bit to stringify v, without using fmt, since we don't
   540  // want context depending on the unicode tables. This is only used by
   541  // *valueCtx.String().
   542  func stringify(v interface{}) string {
   543  	switch s := v.(type) {
   544  	case stringer:
   545  		return s.String()
   546  	case string:
   547  		return s
   548  	}
   549  	return "<not Stringer>"
   550  }
   551  
   552  func (c *valueCtx) String() string {
   553  	return contextName(c.Context) + ".WithValue(type " +
   554  		reflectlite.TypeOf(c.key).String() +
   555  		", val " + stringify(c.val) + ")"
   556  }
   557  
   558  func (c *valueCtx) Value(key interface{}) interface{} {
   559  	if c.key == key {
   560  		return c.val
   561  	}
   562  	return c.Context.Value(key)
   563  }