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.
5 // Package context defines the Context type, which carries deadlines,
6 // cancelation signals, and other request-scoped values across API boundaries
7 // and between processes.
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.
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.
25 // Programs that use Contexts should follow these rules to keep interfaces
26 // consistent across packages and enable static analysis tools to check context
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:
33 // func DoSomething(ctx context.Context, arg Arg) error {
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.
40 // Use context Values only for request-scoped data that transits processes and
41 // APIs, not for passing optional parameters to functions.
43 // The same Context may be passed to functions running in different goroutines;
44 // Contexts are safe for simultaneous use by multiple goroutines.
46 // See https://blog.golang.org/context for example code for a server that uses
58 // A Context carries a deadline, a cancelation signal, and other values across
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)
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.
72 // WithCancel arranges for Done to be closed when cancel is called;
73 // WithDeadline arranges for Done to be closed when the deadline
74 // expires; WithTimeout arranges for Done to be closed when the timeout
77 // Done is provided for use in select statements:
79 // // Stream generates values with DoSomething and sends them to out
80 // // until DoSomething returns an error or ctx.Done is closed.
81 // func Stream(ctx context.Context, out chan<- Value) error {
83 // v, err := DoSomething(ctx)
95 // See https://blog.golang.org/pipelines for more examples of how to use
96 // a Done channel for cancelation.
97 Done() <-chan struct{}
99 // Err returns a non-nil error value after Done is closed. Err returns
100 // Canceled if the context was canceled or DeadlineExceeded if the
101 // context's deadline passed. No other values for Err are defined.
102 // After Done is closed, successive calls to Err return the same value.
105 // Value returns the value associated with this context for key, or nil
106 // if no value is associated with key. Successive calls to Value with
107 // the same key returns the same result.
109 // Use context values only for request-scoped data that transits
110 // processes and API boundaries, not for passing optional parameters to
113 // A key identifies a specific value in a Context. Functions that wish
114 // to store values in Context typically allocate a key in a global
115 // variable then use that key as the argument to context.WithValue and
116 // Context.Value. A key can be any type that supports equality;
117 // packages should define keys as an unexported type to avoid
120 // Packages that define a Context key should provide type-safe accessors
121 // for the values stored using that key:
123 // // Package user defines a User type that's stored in Contexts.
128 // // User is the type of value stored in the Contexts.
129 // type User struct {...}
131 // // key is an unexported type for keys defined in this package.
132 // // This prevents collisions with keys defined in other packages.
135 // // userKey is the key for user.User values in Contexts. It is
136 // // unexported; clients use user.NewContext and user.FromContext
137 // // instead of using this key directly.
138 // var userKey key = 0
140 // // NewContext returns a new Context that carries value u.
141 // func NewContext(ctx context.Context, u *User) context.Context {
142 // return context.WithValue(ctx, userKey, u)
145 // // FromContext returns the User value stored in ctx, if any.
146 // func FromContext(ctx context.Context) (*User, bool) {
147 // u, ok := ctx.Value(userKey).(*User)
150 Value(key
interface{}) interface{}
153 // Canceled is the error returned by Context.Err when the context is canceled.
154 var Canceled
= errors
.New("context canceled")
156 // DeadlineExceeded is the error returned by Context.Err when the context's
158 var DeadlineExceeded error
= deadlineExceededError
{}
160 type deadlineExceededError
struct{}
162 func (deadlineExceededError
) Error() string { return "context deadline exceeded" }
163 func (deadlineExceededError
) Timeout() bool { return true }
164 func (deadlineExceededError
) Temporary() bool { return true }
166 // An emptyCtx is never canceled, has no values, and has no deadline. It is not
167 // struct{}, since vars of this type must have distinct addresses.
170 func (*emptyCtx
) Deadline() (deadline time
.Time
, ok
bool) {
174 func (*emptyCtx
) Done() <-chan struct{} {
178 func (*emptyCtx
) Err() error
{
182 func (*emptyCtx
) Value(key
interface{}) interface{} {
186 func (e
*emptyCtx
) String() string {
189 return "context.Background"
191 return "context.TODO"
193 return "unknown empty Context"
197 background
= new(emptyCtx
)
201 // Background returns a non-nil, empty Context. It is never canceled, has no
202 // values, and has no deadline. It is typically used by the main function,
203 // initialization, and tests, and as the top-level Context for incoming
205 func Background() Context
{
209 // TODO returns a non-nil, empty Context. Code should use context.TODO when
210 // it's unclear which Context to use or it is not yet available (because the
211 // surrounding function has not yet been extended to accept a Context
212 // parameter). TODO is recognized by static analysis tools that determine
213 // whether Contexts are propagated correctly in a program.
214 func TODO() Context
{
218 // A CancelFunc tells an operation to abandon its work.
219 // A CancelFunc does not wait for the work to stop.
220 // After the first call, subsequent calls to a CancelFunc do nothing.
221 type CancelFunc
func()
223 // WithCancel returns a copy of parent with a new Done channel. The returned
224 // context's Done channel is closed when the returned cancel function is called
225 // or when the parent context's Done channel is closed, whichever happens first.
227 // Canceling this context releases resources associated with it, so code should
228 // call cancel as soon as the operations running in this Context complete.
229 func WithCancel(parent Context
) (ctx Context
, cancel CancelFunc
) {
230 c
:= newCancelCtx(parent
)
231 propagateCancel(parent
, &c
)
232 return &c
, func() { c
.cancel(true, Canceled
) }
235 // newCancelCtx returns an initialized cancelCtx.
236 func newCancelCtx(parent Context
) cancelCtx
{
239 done
: make(chan struct{}),
243 // propagateCancel arranges for child to be canceled when parent is.
244 func propagateCancel(parent Context
, child canceler
) {
245 if parent
.Done() == nil {
246 return // parent is never canceled
248 if p
, ok
:= parentCancelCtx(parent
); ok
{
251 // parent has already been canceled
252 child
.cancel(false, p
.err
)
254 if p
.children
== nil {
255 p
.children
= make(map[canceler
]struct{})
257 p
.children
[child
] = struct{}{}
263 case <-parent
.Done():
264 child
.cancel(false, parent
.Err())
271 // parentCancelCtx follows a chain of parent references until it finds a
272 // *cancelCtx. This function understands how each of the concrete types in this
273 // package represents its parent.
274 func parentCancelCtx(parent Context
) (*cancelCtx
, bool) {
276 switch c
:= parent
.(type) {
280 return &c
.cancelCtx
, true
289 // removeChild removes a context from its parent.
290 func removeChild(parent Context
, child canceler
) {
291 p
, ok
:= parentCancelCtx(parent
)
296 if p
.children
!= nil {
297 delete(p
.children
, child
)
302 // A canceler is a context type that can be canceled directly. The
303 // implementations are *cancelCtx and *timerCtx.
304 type canceler
interface {
305 cancel(removeFromParent
bool, err error
)
306 Done() <-chan struct{}
309 // A cancelCtx can be canceled. When canceled, it also cancels any children
310 // that implement canceler.
311 type cancelCtx
struct {
314 done
chan struct{} // closed by the first cancel call.
317 children
map[canceler
]struct{} // set to nil by the first cancel call
318 err error
// set to non-nil by the first cancel call
321 func (c
*cancelCtx
) Done() <-chan struct{} {
325 func (c
*cancelCtx
) Err() error
{
331 func (c
*cancelCtx
) String() string {
332 return fmt
.Sprintf("%v.WithCancel", c
.Context
)
335 // cancel closes c.done, cancels each of c's children, and, if
336 // removeFromParent is true, removes c from its parent's children.
337 func (c
*cancelCtx
) cancel(removeFromParent
bool, err error
) {
339 panic("context: internal error: missing cancel error")
344 return // already canceled
348 for child
:= range c
.children
{
349 // NOTE: acquiring the child's lock while holding parent's lock.
350 child
.cancel(false, err
)
355 if removeFromParent
{
356 removeChild(c
.Context
, c
)
360 // WithDeadline returns a copy of the parent context with the deadline adjusted
361 // to be no later than d. If the parent's deadline is already earlier than d,
362 // WithDeadline(parent, d) is semantically equivalent to parent. The returned
363 // context's Done channel is closed when the deadline expires, when the returned
364 // cancel function is called, or when the parent context's Done channel is
365 // closed, whichever happens first.
367 // Canceling this context releases resources associated with it, so code should
368 // call cancel as soon as the operations running in this Context complete.
369 func WithDeadline(parent Context
, deadline time
.Time
) (Context
, CancelFunc
) {
370 if cur
, ok
:= parent
.Deadline(); ok
&& cur
.Before(deadline
) {
371 // The current deadline is already sooner than the new one.
372 return WithCancel(parent
)
375 cancelCtx
: newCancelCtx(parent
),
378 propagateCancel(parent
, c
)
379 d
:= time
.Until(deadline
)
381 c
.cancel(true, DeadlineExceeded
) // deadline has already passed
382 return c
, func() { c
.cancel(true, Canceled
) }
387 c
.timer
= time
.AfterFunc(d
, func() {
388 c
.cancel(true, DeadlineExceeded
)
391 return c
, func() { c
.cancel(true, Canceled
) }
394 // A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
395 // implement Done and Err. It implements cancel by stopping its timer then
396 // delegating to cancelCtx.cancel.
397 type timerCtx
struct {
399 timer
*time
.Timer
// Under cancelCtx.mu.
404 func (c
*timerCtx
) Deadline() (deadline time
.Time
, ok
bool) {
405 return c
.deadline
, true
408 func (c
*timerCtx
) String() string {
409 return fmt
.Sprintf("%v.WithDeadline(%s [%s])", c
.cancelCtx
.Context
, c
.deadline
, time
.Until(c
.deadline
))
412 func (c
*timerCtx
) cancel(removeFromParent
bool, err error
) {
413 c
.cancelCtx
.cancel(false, err
)
414 if removeFromParent
{
415 // Remove this timerCtx from its parent cancelCtx's children.
416 removeChild(c
.cancelCtx
.Context
, c
)
426 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
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:
431 // func slowOperationWithTimeout(ctx context.Context) (Result, error) {
432 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
433 // defer cancel() // releases resources if slowOperation completes before timeout elapses
434 // return slowOperation(ctx)
436 func WithTimeout(parent Context
, timeout time
.Duration
) (Context
, CancelFunc
) {
437 return WithDeadline(parent
, time
.Now().Add(timeout
))
440 // WithValue returns a copy of parent in which the value associated with key is
443 // Use context Values only for request-scoped data that transits processes and
444 // APIs, not for passing optional parameters to functions.
446 // The provided key must be comparable and should not be of type
447 // string or any other built-in type to avoid collisions between
448 // packages using context. Users of WithValue should define their own
449 // types for keys. To avoid allocating when assigning to an
450 // interface{}, context keys often have concrete type
451 // struct{}. Alternatively, exported context key variables' static
452 // type should be a pointer or interface.
453 func WithValue(parent Context
, key
, val
interface{}) Context
{
457 if !reflect
.TypeOf(key
).Comparable() {
458 panic("key is not comparable")
460 return &valueCtx
{parent
, key
, val
}
463 // A valueCtx carries a key-value pair. It implements Value for that key and
464 // delegates all other calls to the embedded Context.
465 type valueCtx
struct {
470 func (c
*valueCtx
) String() string {
471 return fmt
.Sprintf("%v.WithValue(%#v, %#v)", c
.Context
, c
.key
, c
.val
)
474 func (c
*valueCtx
) Value(key
interface{}) interface{} {
478 return c
.Context
.Value(key
)