Commit 8734c452 authored by Sameer Ajmani's avatar Sameer Ajmani

go.net/context: update some comments, add a TODO.

Use consistent spelling.

LGTM=bcmills
R=bcmills
CC=golang-codereviews
https://golang.org/cl/117170044
parent 2d7673cd
...@@ -137,6 +137,11 @@ var DeadlineExceeded = errors.New("context deadline exceeded") ...@@ -137,6 +137,11 @@ var DeadlineExceeded = errors.New("context deadline exceeded")
// A ctx is a Context that automatically propagates cancellation signals to // A ctx is a Context that automatically propagates cancellation signals to
// other ctxs (those created using this ctx as their parent). A ctx also // other ctxs (those created using this ctx as their parent). A ctx also
// manages its own deadline timer. // manages its own deadline timer.
//
// TODO(sameer): split this into separate concrete types for WithValue,
// WithCancel, and WithTimeout/Deadline. This reduces the size of the structs;
// for example, we don't need a timer field when creating a Context using
// WithValue.
type ctx struct { type ctx struct {
parent Context // set by newCtx parent Context // set by newCtx
done chan struct{} // closed by the first cancel call. nil if uncancelable. done chan struct{} // closed by the first cancel call. nil if uncancelable.
...@@ -181,20 +186,19 @@ func (c *ctx) Value(key interface{}) interface{} { ...@@ -181,20 +186,19 @@ func (c *ctx) Value(key interface{}) interface{} {
// The background context for this process. // The background context for this process.
var background = newCtx(nil, neverCanceled) var background = newCtx(nil, neverCanceled)
// Background returns an ambient background context, which is never nil. This // Background returns a non-nil, empty Context. It is never canceled, has no
// context represents the intrinsic state of the application at startup time, // values, and has no deadline. It is typically used by the main function,
// independent of any incoming request state. // initialization, and tests, and as the top-level Context for incoming
// // requests.
// The Background context is typically only used by the main function and tests.
func Background() Context { func Background() Context {
return background return background
} }
// TODO returns an ambient background context, which is never nil. Code should // TODO returns a non-nil, empty Context. Code should use context.TODO when
// use context.TODO when it's unclear which Context to use or it's is not yet // it's unclear which Context to use or it's is not yet available (because the
// available (because the surrounding function has not yet been extended to // surrounding function has not yet been extended to accept a Context
// accept a Context parameter). TODO is recognized by static analysis tools // parameter). TODO is recognized by static analysis tools that determine
// that determine whether Contexts are propagated correctly in a program. // whether Contexts are propagated correctly in a program.
func TODO() Context { func TODO() Context {
return Background() return Background()
} }
...@@ -223,7 +227,7 @@ func withCancel(parent Context) (*ctx, CancelFunc) { ...@@ -223,7 +227,7 @@ func withCancel(parent Context) (*ctx, CancelFunc) {
// cancel function is called, or when the parent context's Done channel is // cancel function is called, or when the parent context's Done channel is
// closed, whichever happens first. // closed, whichever happens first.
// //
// Cancelling this context releases resources associated with the deadline // Canceling this context releases resources associated with the deadline
// timer, so code should call cancel as soon as the operations running in this // timer, so code should call cancel as soon as the operations running in this
// Context complete. // Context complete.
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
...@@ -250,7 +254,7 @@ func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { ...@@ -250,7 +254,7 @@ func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
// //
// Cancelling this context releases resources associated with the deadline // Canceling this context releases resources associated with the deadline
// timer, so code should call cancel as soon as the operations running in this // timer, so code should call cancel as soon as the operations running in this
// Context complete: // Context complete:
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment