blob: d3cb95175203818172743b6a2eb4a1e49bda17f1 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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,
mgouda64be8822025-05-30 10:44:00 +05306// cancellation signals, and other request-scoped values across API boundaries
khenaidooac637102019-01-14 15:44:34 -05007// and between processes.
8// As of Go 1.7 this package is available in the standard library under the
Abhay Kumara2ae5992025-11-10 14:02:24 +00009// name [context].
khenaidooac637102019-01-14 15:44:34 -050010//
mgouda64be8822025-05-30 10:44:00 +053011// Incoming requests to a server should create a [Context], and outgoing
12// calls to servers should accept a Context. The chain of function
13// calls between them must propagate the Context, optionally replacing
14// it with a derived Context created using [WithCancel], [WithDeadline],
15// [WithTimeout], or [WithValue].
khenaidooac637102019-01-14 15:44:34 -050016//
17// Programs that use Contexts should follow these rules to keep interfaces
18// consistent across packages and enable static analysis tools to check context
19// propagation:
20//
21// Do not store Contexts inside a struct type; instead, pass a Context
mgouda64be8822025-05-30 10:44:00 +053022// explicitly to each function that needs it. This is discussed further in
23// https://go.dev/blog/context-and-structs. The Context should be the first
khenaidooac637102019-01-14 15:44:34 -050024// parameter, typically named ctx:
25//
mgouda64be8822025-05-30 10:44:00 +053026// func DoSomething(ctx context.Context, arg Arg) error {
27// // ... use ctx ...
28// }
khenaidooac637102019-01-14 15:44:34 -050029//
mgouda64be8822025-05-30 10:44:00 +053030// Do not pass a nil [Context], even if a function permits it. Pass [context.TODO]
khenaidooac637102019-01-14 15:44:34 -050031// if you are unsure about which Context to use.
32//
33// Use context Values only for request-scoped data that transits processes and
34// APIs, not for passing optional parameters to functions.
35//
36// The same Context may be passed to functions running in different goroutines;
37// Contexts are safe for simultaneous use by multiple goroutines.
38//
mgouda64be8822025-05-30 10:44:00 +053039// See https://go.dev/blog/context for example code for a server that uses
khenaidooac637102019-01-14 15:44:34 -050040// Contexts.
mgouda64be8822025-05-30 10:44:00 +053041package context
42
43import (
44 "context" // standard library's context, as of Go 1.7
45 "time"
46)
47
48// A Context carries a deadline, a cancellation signal, and other values across
49// API boundaries.
50//
51// Context's methods may be called by multiple goroutines simultaneously.
Abhay Kumara2ae5992025-11-10 14:02:24 +000052//
53//go:fix inline
mgouda64be8822025-05-30 10:44:00 +053054type Context = context.Context
55
56// Canceled is the error returned by [Context.Err] when the context is canceled
57// for some reason other than its deadline passing.
Abhay Kumara2ae5992025-11-10 14:02:24 +000058//
59//go:fix inline
mgouda64be8822025-05-30 10:44:00 +053060var Canceled = context.Canceled
61
62// DeadlineExceeded is the error returned by [Context.Err] when the context is canceled
63// due to its deadline passing.
Abhay Kumara2ae5992025-11-10 14:02:24 +000064//
65//go:fix inline
mgouda64be8822025-05-30 10:44:00 +053066var DeadlineExceeded = context.DeadlineExceeded
khenaidooac637102019-01-14 15:44:34 -050067
68// Background returns a non-nil, empty Context. It is never canceled, has no
69// values, and has no deadline. It is typically used by the main function,
70// initialization, and tests, and as the top-level Context for incoming
71// requests.
Abhay Kumara2ae5992025-11-10 14:02:24 +000072//
73//go:fix inline
74func Background() Context { return context.Background() }
khenaidooac637102019-01-14 15:44:34 -050075
76// TODO returns a non-nil, empty Context. Code should use context.TODO when
77// it's unclear which Context to use or it is not yet available (because the
78// surrounding function has not yet been extended to accept a Context
mgouda64be8822025-05-30 10:44:00 +053079// parameter).
Abhay Kumara2ae5992025-11-10 14:02:24 +000080//
81//go:fix inline
82func TODO() Context { return context.TODO() }
mgouda64be8822025-05-30 10:44:00 +053083
84// A CancelFunc tells an operation to abandon its work.
85// A CancelFunc does not wait for the work to stop.
86// A CancelFunc may be called by multiple goroutines simultaneously.
87// After the first call, subsequent calls to a CancelFunc do nothing.
88type CancelFunc = context.CancelFunc
89
90// WithCancel returns a derived context that points to the parent context
91// but has a new Done channel. The returned context's Done channel is closed
92// when the returned cancel function is called or when the parent context's
93// Done channel is closed, whichever happens first.
94//
95// Canceling this context releases resources associated with it, so code should
96// call cancel as soon as the operations running in this [Context] complete.
Abhay Kumara2ae5992025-11-10 14:02:24 +000097//
98//go:fix inline
mgouda64be8822025-05-30 10:44:00 +053099func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
100 return context.WithCancel(parent)
101}
102
103// WithDeadline returns a derived context that points to the parent context
104// but has the deadline adjusted to be no later than d. If the parent's
105// deadline is already earlier than d, WithDeadline(parent, d) is semantically
106// equivalent to parent. The returned [Context.Done] channel is closed when
107// the deadline expires, when the returned cancel function is called,
108// or when the parent context's Done channel is closed, whichever happens first.
109//
110// Canceling this context releases resources associated with it, so code should
111// call cancel as soon as the operations running in this [Context] complete.
Abhay Kumara2ae5992025-11-10 14:02:24 +0000112//
113//go:fix inline
mgouda64be8822025-05-30 10:44:00 +0530114func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
115 return context.WithDeadline(parent, d)
116}
117
118// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
119//
120// Canceling this context releases resources associated with it, so code should
121// call cancel as soon as the operations running in this [Context] complete:
122//
123// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
124// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
125// defer cancel() // releases resources if slowOperation completes before timeout elapses
126// return slowOperation(ctx)
127// }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000128//
129//go:fix inline
mgouda64be8822025-05-30 10:44:00 +0530130func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
131 return context.WithTimeout(parent, timeout)
132}
133
134// WithValue returns a derived context that points to the parent Context.
135// In the derived context, the value associated with key is val.
136//
137// Use context Values only for request-scoped data that transits processes and
138// APIs, not for passing optional parameters to functions.
139//
140// The provided key must be comparable and should not be of type
141// string or any other built-in type to avoid collisions between
142// packages using context. Users of WithValue should define their own
143// types for keys. To avoid allocating when assigning to an
144// interface{}, context keys often have concrete type
145// struct{}. Alternatively, exported context key variables' static
146// type should be a pointer or interface.
Abhay Kumara2ae5992025-11-10 14:02:24 +0000147//
148//go:fix inline
mgouda64be8822025-05-30 10:44:00 +0530149func WithValue(parent Context, key, val interface{}) Context {
150 return context.WithValue(parent, key, val)
151}