Skip to content

Commit 918d64e

Browse files
dmitshurgopherbot
authored andcommitted
context: delete dead code, sync docs with upstream context package
The go directive is now at 1.23.0, so the go1.7 and go1.9 build constraints are guaranteed to always be satisfied, and their inverse will never be satisfied. Delete all the dead code and merge everything that's left in a single context.go file. Also update docs to match the upstream context package. For golang/go#49506. Change-Id: I317550767838a93af2c2d3dbc7b61f2e37e6fe1c Reviewed-on: https://go-review.googlesource.com/c/net/+/650155 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
1 parent 5095d0c commit 918d64e

File tree

7 files changed

+101
-1097
lines changed

7 files changed

+101
-1097
lines changed

context/context.go

+100-12
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
// license that can be found in the LICENSE file.
44

55
// Package context defines the Context type, which carries deadlines,
6-
// cancelation signals, and other request-scoped values across API boundaries
6+
// cancellation signals, and other request-scoped values across API boundaries
77
// and between processes.
88
// As of Go 1.7 this package is available in the standard library under the
9-
// name context. https://golang.org/pkg/context.
9+
// name [context], and migrating to it can be done automatically with [go fix].
1010
//
11-
// Incoming requests to a server should create a Context, and outgoing calls to
12-
// servers should accept a Context. The chain of function calls between must
13-
// propagate the Context, optionally replacing it with a modified copy created
14-
// using WithDeadline, WithTimeout, WithCancel, or WithValue.
11+
// 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].
1516
//
1617
// Programs that use Contexts should follow these rules to keep interfaces
1718
// consistent across packages and enable static analysis tools to check context
1819
// propagation:
1920
//
2021
// Do not store Contexts inside a struct type; instead, pass a Context
21-
// explicitly to each function that needs it. The Context should be the first
22+
// 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
2224
// parameter, typically named ctx:
2325
//
2426
// func DoSomething(ctx context.Context, arg Arg) error {
2527
// // ... use ctx ...
2628
// }
2729
//
28-
// Do not pass a nil Context, even if a function permits it. Pass context.TODO
30+
// Do not pass a nil [Context], even if a function permits it. Pass [context.TODO]
2931
// if you are unsure about which Context to use.
3032
//
3133
// Use context Values only for request-scoped data that transits processes and
@@ -34,9 +36,30 @@
3436
// The same Context may be passed to functions running in different goroutines;
3537
// Contexts are safe for simultaneous use by multiple goroutines.
3638
//
37-
// See http://blog.golang.org/context for example code for a server that uses
39+
// See https://go.dev/blog/context for example code for a server that uses
3840
// Contexts.
39-
package context // import "golang.org/x/net/context"
41+
//
42+
// [go fix]: https://go.dev/cmd/go#hdr-Update_packages_to_use_new_APIs
43+
package context
44+
45+
import (
46+
"context" // standard library's context, as of Go 1.7
47+
"time"
48+
)
49+
50+
// A Context carries a deadline, a cancellation signal, and other values across
51+
// API boundaries.
52+
//
53+
// Context's methods may be called by multiple goroutines simultaneously.
54+
type 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.
58+
var Canceled = context.Canceled
59+
60+
// DeadlineExceeded is the error returned by [Context.Err] when the context is canceled
61+
// due to its deadline passing.
62+
var DeadlineExceeded = context.DeadlineExceeded
4063

4164
// Background returns a non-nil, empty Context. It is never canceled, has no
4265
// values, and has no deadline. It is typically used by the main function,
@@ -49,8 +72,73 @@ func Background() Context {
4972
// TODO returns a non-nil, empty Context. Code should use context.TODO when
5073
// it's unclear which Context to use or it is not yet available (because the
5174
// surrounding function has not yet been extended to accept a Context
52-
// parameter). TODO is recognized by static analysis tools that determine
53-
// whether Contexts are propagated correctly in a program.
75+
// parameter).
5476
func TODO() Context {
5577
return todo
5678
}
79+
80+
var (
81+
background = context.Background()
82+
todo = context.TODO()
83+
)
84+
85+
// A CancelFunc tells an operation to abandon its work.
86+
// A CancelFunc does not wait for the work to stop.
87+
// A CancelFunc may be called by multiple goroutines simultaneously.
88+
// After the first call, subsequent calls to a CancelFunc do nothing.
89+
type CancelFunc = context.CancelFunc
90+
91+
// WithCancel returns a derived context that points to the parent context
92+
// but has a new Done channel. The returned context's Done channel is closed
93+
// when the returned cancel function is called or when the parent context's
94+
// Done channel is closed, whichever happens first.
95+
//
96+
// Canceling this context releases resources associated with it, so code should
97+
// call cancel as soon as the operations running in this [Context] complete.
98+
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
99+
return context.WithCancel(parent)
100+
}
101+
102+
// WithDeadline returns a derived context that points to the parent context
103+
// but has the deadline adjusted to be no later than d. If the parent's
104+
// deadline is already earlier than d, WithDeadline(parent, d) is semantically
105+
// equivalent to parent. The returned [Context.Done] channel is closed when
106+
// the deadline expires, when the returned cancel function is called,
107+
// or when the parent context's Done channel is closed, whichever happens first.
108+
//
109+
// Canceling this context releases resources associated with it, so code should
110+
// call cancel as soon as the operations running in this [Context] complete.
111+
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
112+
return context.WithDeadline(parent, d)
113+
}
114+
115+
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
116+
//
117+
// Canceling this context releases resources associated with it, so code should
118+
// call cancel as soon as the operations running in this [Context] complete:
119+
//
120+
// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
121+
// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
122+
// defer cancel() // releases resources if slowOperation completes before timeout elapses
123+
// return slowOperation(ctx)
124+
// }
125+
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
126+
return context.WithTimeout(parent, timeout)
127+
}
128+
129+
// WithValue returns a derived context that points to the parent Context.
130+
// In the derived context, the value associated with key is val.
131+
//
132+
// Use context Values only for request-scoped data that transits processes and
133+
// APIs, not for passing optional parameters to functions.
134+
//
135+
// The provided key must be comparable and should not be of type
136+
// string or any other built-in type to avoid collisions between
137+
// packages using context. Users of WithValue should define their own
138+
// types for keys. To avoid allocating when assigning to an
139+
// interface{}, context keys often have concrete type
140+
// struct{}. Alternatively, exported context key variables' static
141+
// type should be a pointer or interface.
142+
func WithValue(parent Context, key, val interface{}) Context {
143+
return context.WithValue(parent, key, val)
144+
}

0 commit comments

Comments
 (0)