Go Package context: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(64 intermediate revisions by the same user not shown)
Line 11: Line 11:
The <code>context</code> package provides two, somewhat unrelated, features.
The <code>context</code> package provides two, somewhat unrelated, features.


One is the capability to [[#Preemption|preempt]], or cancel, blocking code executed by goroutines running downstream in the [[#Call_Graph|call graph]]. The <code>context</code> package provides an API for cancelling branches of the call graph. Cancellation can be [[#Preemption|programmatic]], on [[#Timeout|timeout]] or on [[#Deadline|deadline]]. This is an idiomatic preemption pattern, equivalent, but preferred to [[Go_Concurrency_Patterns_The_done_Channel#Overview|the Done channel pattern]]. It is preferred because its implementation is available in the [[Go_Language_Modularization#Standard_Library|standard library]], making it a standard Go idiom to consider when working with concurrent code. The implementation is also more expressive.  
One is the capability to [[#Preemption|preempt]], or cancel, blocking code executed by goroutines running downstream in the [[#Call_Graph|call graph]]. The <code>context</code> package provides an API for cancelling branches of the call graph. Cancellation can be [[#Preemption|programmatic]], on [[#Timeout|timeout]] or on [[#Deadline|deadline]]. This is an idiomatic preemption pattern, equivalent, but preferred to [[Go_Channels#The_Done_Channel|the Done channel pattern]]. It is preferred because its implementation is available in the [[Go_Language_Modularization#Standard_Library|standard library]], making it a standard Go idiom to consider when working with concurrent code. The implementation is also more expressive.  


The second feature is the capability to store [[#Request-Scoped_Data|request-scoped data]] and propagate this state through the call graph.
The second feature is the capability to store [[#Request-Scoped_Data|request-scoped data]] and propagate this state through the call graph.
Line 20: Line 20:
==Context==
==Context==
{{External|https://pkg.go.dev/context#Context}}
{{External|https://pkg.go.dev/context#Context}}
<code>context.Context</code> is an interface exposed by the <code>context</code> package. The <code>Context</code> instances flow across API boundaries and through the system in the same way [[Go_Concurrency_Patterns_The_done_Channel#Overview|"done" channels]] do. The role of the Context instances is to carry [[#Cancellation_Signal|cancellation signals]], [[#Deadline|deadlines]] and [[#Request-Scoped_Data|request-scoped values]]. To implement the pattern, each function downstream from the top level concurrent call must take a Context as parameter. Conventionally, is the first parameter of the function. For more rules of using Contexts, see [[#Programming_Model|Programming Model]]. The implementations must ensure that they are concurrent-safe, so their methods may be called by multiple goroutines simultaneously.
<code>context.Context</code> is an interface exposed by the <code>context</code> package. The <code>Context</code> instances flow across API boundaries and through the system in the same way [[Go_Channels#The_Done_Channel|Done channels]] do. The role of the Context instances is to carry [[#Cancellation_Signal|cancellation signals]], [[#Deadline|deadlines]] and [[#Request-Scoped_Data|request-scoped values]]. To implement the pattern, each function downstream from the top level concurrent call must take a Context as parameter. Conventionally, is the first parameter of the function. For more rules of using Contexts, see [[#Programming_Model|Programming Model]]. The implementations must ensure that they are concurrent-safe, so their methods may be called by multiple goroutines simultaneously.
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
type Context interface {
type Context interface {
Line 38: Line 38:
}
}
</syntaxhighlight>
</syntaxhighlight>
The Context interface does not expose any method that can mutate the state of the underlying structure, nor any method that allows cancelling the Context. This protects functions up the call stack from children cancelling the context - only the parent can cancel the children functions, as shown in the [[#Programmatic_Preemption_(Cancellation)|Programmatic Preemption (Cancellation)]] section.
<span id='Context_Cannot_Be_Canceled_Directly'></span>The Context interface does not expose any method that allows cancelling the Context, and the Done channel is read only, it cannot be closed. This protects functions up the call stack from children cancelling the context: only the parent can cancel the children functions, as shown in the [[#Programmatic_Preemption_(Cancellation)|Programmatic Preemption (Cancellation)]] section. The function receiving the cancellation signal is not the one that sends the signal.
The context is immutable, so when new values are stored, the context is actually cloned, with extra metadata.  
 
The Context interface does not expose any method that can mutate the state of the underlying structure, either. The context is immutable, so when new values are stored, the context is actually cloned, with extra metadata.  
===<tt>Err()</tt>===
===<tt>Err()</tt>===
<code>ctx.Err()</code> returns a standard response, as follows:
<code>ctx.Err()</code> returns an error indicating why the Context was canceled, as follows:
* <code>nil</code> is the Done channel is not closed yet.
* <code>nil</code> is the Done channel is not closed yet.
* the <code>context.Canceled</code> error, if the context was canceled.
* the <code>context.Canceled</code> error, if the context was programmatically canceled by invoking the [[#Cancel_Function|cancel function]].
* the <code>context.DeadlineExceeded</code> error, if the context's deadline has passed.
* the <code>context.DeadlineExceeded</code> error, if the context timed out or the context's deadline has passed.


===The Background Context===
===The Background Context===
{{External|https://pkg.go.dev/context#Background}}
{{External|https://pkg.go.dev/context#Background}}
The background Context is a non-null, empty, no-deadline and no-values Context returned by the <code>context.Background()</code> function. This context is never canceled, and it is typically used by the main function, initialization and tests, and as the top-level Context for incoming [[#Request|requests]].
The background Context is a non-null, empty, no-deadline and no-values Context returned by the <code>context.Background()</code> function. This context is never canceled, and it is typically used by the main function, initialization and tests, and as the top-level Context for incoming [[#Request|requests]]. The background Context is the root of the [[#Derived_Context_and_the_Context_Hierarchy|context tree]].
===Derived Context and the Context Hierarchy===
 
Context hierarchy.
===The TODO Context===
TODO is intended to serve as a placeholder for when you don't know which Context to utilize, or if you expect your code to be provided with a Context, but the upstream code hasn't yet furnished one. TODO is not meant for use in production.
 
===<span id='Derived_Context'></span>Derived Context and the Context Hierarchy===
 
Cancellation or data storage can only be performed on a '''derived context'''. The <code>context</code> package provide functions that derive new Context instances from an existing context: <code>WithCancel()</code>, <code>WithTimeout()</code>, <code>WithDeadline()</code> and <code>WithValue()</code>. The Context instances created this way form a '''tree'''. The [[#The_Background_Context|Background context]] is the root of this tree. When a Context instance is canceled, all Context instances derived from it are also canceled.


==<span id='Preemption'></span><span id='Cancellation'></span>Preemption (Cancellation)==
==<span id='Preemption'></span><span id='Cancellation'></span>Preemption (Cancellation)==
Preemption, or cancellation of a function executing a blocking operation on a channel, or any other blocking operation (disk, network IO, user), consists in the immediate return from the blocking operation, and usually return from the function with a [[#Err()|cancellation error]]. Programmatic cancelation is useful in stopping processing or handling application shutdown.
Preemption, or cancellation of a function executing a blocking operation on a channel, or any other blocking operation (disk, network IO, user), consists in the immediate return from the blocking operation, and usually return from the function with a [[#Err()|cancellation error]]. Programmatic cancelation is useful in stopping processing or handling application shutdown. Cancellation is initiated by a parent goroutine in the [[#Call_Graph|call graph]].
===<span id='Cancellation_Signal'></span>Cancellation Signal - the Done Channel===
The "cancellation signal" carried by the context is a [[Go_Channels#The_Done_Channel|Done channel]] that is closed when the functions associated with the context need to be preempted. The channel is returned by the context's <code>Done()</code> method. The functions typically read the channel in a <code>[[Go_Channels#The_select_Statement|select]]</code> statement, and when the read returns, because the channel was closed, the functions should abandon their work and return. For an example of how to do that, see [[#Programmatic_Preemption_(Cancellation)|Programmatic Preemption]] below.


Cancellation is initiated by a parent goroutine in the [[#Call_Graph|call graph]].
Also see: {{Internal|Go_Channels#The_Done_Channel|Go Channels &#124; The Done Channel}}
===Cancellation Signal===
The "cancellation signal" carried by the context is a Done channel that is closed when the function associated with the context needs to be prepped. The channel is returned by the context's <code>Done()</code> method.


===Cancel Function===
===Cancel Function===
The cancel function is a function returned by the invocation of the <code>WithCancel</code> function, which then can be used to cancel the context returned by the same invocation of the <code>WithCancel</code> function. For an example, see [[#Programmatic_Preemption| Programmatic Preemption]] below.


==Deadline==
==Deadline==
A deadline is communicated to the user by the result of the Context's <code>Deadline()</code> method. The method returns the time when work done on behalf of this Context should be cancelled, or a <code>false</code> value if no deadline is set.
A deadline is communicated to the user by the result of the Context's <code>Deadline()</code> method. The method returns the time when work done on behalf of this Context should be cancelled, or a <code>false</code> value if no deadline is set. Checking the deadline programmatically allows the function to fail fast, which, in programs that may have a high cost for the next bit of functionality, this may save a significant amount of time. This approach implies that we need to have some idea of how long the subordinate call graph will take, which may not be trivial.
<syntaxhighlight lang='go'>
func someFunc(ctx context.Context) error {
if deadline, ok := ctx.Deadline(); ok {
// the context has a deadline
if deadline.Sub(time.Now().Add(1 * time.Minute)) <= 0 {
return context.DeadlineExceeded
}
}
// the context does not have a deadline
...
}
</syntaxhighlight>
 
Also see [[#Programmatic_Deadline|Programmatic Deadline]] section, below.


==Timeout==
==Timeout==
See [[#Programmatic_Timeout|Programmatic Timeout]] section, below.
==Call Graph==
==Call Graph==
==<span id='Request'><span><span id='Request-Scoped_Data'><span>Requests and Request-Scoped Data==
==<span id='Request'><span><span id='Request-Scoped_Data'><span>Requests and Request-Scoped Data==
One of the primary use of goroutines is to service '''requests'''. Usually, in these programs, request-specific information needs to be. passed along in addition to information about preemption. The Context exposes the <code>Value()</code> method, which returns a request-scoped value for a given key.
One of the primary use of goroutines is to service '''requests'''. Each incoming request is handled in its own goroutine. Request handlers often start additional goroutines to access backends such as databases and RPC services. The set of goroutines working on a request typically need access to request-specific information, such as user identity, authorization tokens, and the request's deadline, in addition to the capability to cancel the goroutine tree.  
 
The Context exposes the <code>Value()</code> method, which returns a request-scoped value for a given key. For an example of how to do this programmatically, see [[#Storing_Request-Scoped_Data|Storing Request-Scoped Data]] below.
 
The package documentation is quite vague on what kind of data should be stored in the Context: "Use context values only for request-scoped data that transits processes and API boundaries, not for passing optional parameters to functions." The following heuristics may help:
# The data should transit process or API boundaries. If you generate the data in the process' memory, it's probably not a good candidate, unless you also pass it across an API boundary.
# The data should be immutable. If it is not, then by definition what you're storing did not come from a request.
# The data should trend toward simple types. This way, it is much easier for the other side to pull the data if it does not have to import a complex graph of packages.
# The data should be data, not types with methods.
# The data should help decorate operations, not drive them. If your algorithm behaves differently based on what is or isn't included in the Context, you have likely crossed over into the territory of optional parameters.


Using the value propagation feature of the Context is dubious. Don't use Context values for stuff that should be regular dependencies between [[Go Component Design|components]]. Use Context values for data that can't be passed to your program in any other way: only data that is request-scoped, stuff that is created at the beginning of a request lifecycle, like request ID, etc. If the information is available when the program starts or at any point prior to when the request starts, do not use Context values. This is the case for database handles, loggers, etc.
Using the value propagation feature of the Context is dubious. Don't use Context values for stuff that should be regular dependencies between [[Go Component Design|components]]. Use Context values for data that can't be passed to your program in any other way: only data that is request-scoped, stuff that is created at the beginning of a request lifecycle, like request ID, etc. If the information is available when the program starts or at any point prior to when the request starts, do not use Context values. This is the case for database handles, loggers, etc.
===Context Keys, Values and Type Safety===
Since Context's keys and values are defined as <code>interface{}</code>, we lose Go's type safety when attempting to retrieve values. A specific technique can be employed to restore type safety, as described in the [[#Restoring_Key/Value_Type_Safety_for_Request-Scoped_Data|Restoring Key/Value Type Safety for Request-Scoped Data]] section.
===Passing Loggers in Context===
See: {{Internal|Zap_Concepts#Loggers_and_context.Context|Zap Concepts &#124; Loggers and <tt>context.Context</tt>}}


=Programming Model=
=Programming Model=
* Each function downstream from the top level concurrent call must take a Context as '''the first''' parameter.
* Each function downstream from the top level concurrent call must take a Context as '''the first''' parameter.
* Context instances cannot be mutated or cancelled directly, as explained [[#Context_Cannot_Be_Canceled_Directly|above]].
* Context instances cannot be mutated or cancelled directly, as explained [[#Context_Cannot_Be_Canceled_Directly|above]].
==<span id='Preemption'></span><span id='Cancellation'></span>Programmatic Preemption (Cancellation)==
* To allow a parent goroutine to cancel its children, create a new Context instance with one of the <code>WithCancel()</code>, <code>WithTimeout()</code> and <code>WithDeadline()</code> methods and pass '''that''' instance to the function executing on the child goroutine. The derived Context instance can be canceled sooner than the parent context. See:
** [[#Programmatic_Preemption|Programmatic Preemption (Cancellation)]]
** [[#Programmatic_Timeout|Programmatic Timeout]]
** [[#Programmatic_Deadline|Programmatic Deadline]]
* If the parent goroutine does not need to modify the cancellation behavior of the functions running on children goroutines, it will pass the unmodified Context. In this way, successive layers of the call graph can create Context instances that adhere to their needs, without affecting the parents. This composability enables writing large system without mixing concerns through the call graph.
* The Context associated with an incoming [[#Request|request]] is typically canceled when the request handler returns.
* Only pass Context instances as parameters to functions. Do not store references to Context instances. These instances may look equivalent from the outside, but internally may change at every stack frame.
==<span id='Preemption'></span><span id='Cancellation'></span><span id='Programmatic_Preemption'></span>Programmatic Preemption (Cancellation)==
{{External|https://pkg.go.dev/context#WithCancel}}
{{External|https://pkg.go.dev/context#WithCancelCause}}
 
This section documents the idiomatic pattern to [[#Preemption|preempt]], or [[#Cancellation|cancel]], a blocking function.
This section documents the idiomatic pattern to [[#Preemption|preempt]], or [[#Cancellation|cancel]], a blocking function.


Line 84: Line 130:
select {
select {
case <-ctx.Done():
case <-ctx.Done():
return ctx.Err() // context.Canceled is the context was externally canceled  
return ctx.Err() // context.Canceled if the context was externally canceled  
case <-c: // this will ensure the goroutine will block, as we will never write on that channel
case <-c: // this will ensure the goroutine will block, as we will never write on that channel
}
}
Line 90: Line 136:
}
}
</syntaxhighlight>
</syntaxhighlight>
The parent goroutine must use <code>WithCancel()</code> function to create a new [[#Derived_Context|derived context]] to be passed to functions executed on children goroutines. The function also returns a [[#Cancel_Function|cancel function]].
Alternatively, <code>WithCancelCause()</code> can be used. The function behaves like <code>WithCancel()</code> but returns a cancel function that allows specifying the error (the "cause") when cancelling the context, which can be retrieved using <code>Cause()</code>.


<span id='Cancelling_Context'></span>To '''externally cancel''' the context, and implicitly all functions listening on its Done channel, invoke the [[#Cancel_Function|cancel function]] returned by the <code>context.WithCancel()</code> function:
<span id='Cancelling_Context'></span>To '''externally cancel''' the context, and implicitly all functions listening on its Done channel, invoke the cancel function:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.Background())
Line 106: Line 155:
cancel() // Calling the cancel() function cancels the context
cancel() // Calling the cancel() function cancels the context
}()
}()
</syntaxhighlight>
==Programmatic Timeout==
The <code>WithTimeout()</code> function creates a new [[#Derived_Context|derived context]] that closes its Done channel after the given timeout. The Context error becomes <code>context.DeadlineExceeded</code>. The <code>WithTimeout()</code> invocation also returns a [[#Cancel_Function|cancel function]] that can be used independently of the timeout.
<code>someFunc()</code> function gets a Context instance as its first argument. The function will <code>select</code> reading the Done channel returned by the Context's <code>Done()</code> method. When the context times out, the Done channel is closed, reading operation on the Done channel unblocks and allows the function to return. The recommended return value in this case is the error returned by <code>[[#Err()|ctx.Err()]]</code>, which, in this case, is <code>context.DeadlineExceeded</code>. At the same time, the Context can be externally cancelled by inferring the [[#Cancel_Function|cancel function]], and in that case, the error will be <code>context.Canceled</code>.
<syntaxhighlight lang='go'>
// someFunc will return a context.DeadlineExceeded error if the context timed out.
func someFunc(ctx context.Context, c <-chan interface{}) error {
for {
select {
case <-ctx.Done():
return ctx.Err() // context.DeadlineExceeded if the context timed out
case <-c: // this will ensure the goroutine will block, as we will never write on that channel
}
}
}
</syntaxhighlight>
The parent goroutine must use <code>WithTimeout()</code> function to create a new [[#Derived_Context|derived context]] to be passed to functions executed on children goroutines. The function also returns a [[#Cancel_Function|cancel function]] that can be optionally used, independent of the timeout. Typically, the function is deferred.
Alternatively, <code>WithTimeoutCause()</code> function can be used. This function allows setting the error (the "cause") returned by the context with the timeout expires, instead of the default <code>context.DeadlineExceeded</code>.
<syntaxhighlight lang='go'>
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// spin off someFunc() on its own thread and get it to block by reading on the channel we will never write on
go func() {
err := someFunc(ctx, make(chan interface{}))
fmt.Printf("someFunc() errored out because %v\n", err)
}()
</syntaxhighlight>
===Simple Timeout on a Channel===
A simpler pattern to time out on a <code>time</code> channel is available here: {{Internal|Go_Channels#Timing_Out_select|Timing-out <tt>select</tt>}}
==Programmatic Deadline==
The <code>WithDeadline()</code> function creates a new [[#Derived_Context|derived context]] that closes its Done channel when the machine's clock advances past the given deadline.
==Storing Request-Scoped Data==
To store the data in the context, create an immutable clone with <code>context.WithValue()</code>.
The keys must satisfy Go's notion of [[Go_Language#Identity,_Equality,_Comparability|comparability]]. The values must be safe to access concurrently.
<syntaxhighlight lang='go'>
// storing data in Context
ctx := context.WithValue(context.Background(), "auth-token", "a023k27l")
ctx = context.WithValue(ctx, "user-id", "a@example.com")
go someFunc(ctx)
</syntaxhighlight>
To retrieve data from the context, use the <code>Value()</code> method:
<syntaxhighlight lang='go'>
func someFunc(ctx context.Context) {
// retrieving data from Context
fmt.Printf("auth-token: %v\n", ctx.Value("auth-token"))
fmt.Printf("user-id:    %v\n", ctx.Value("user-id"))
}
</syntaxhighlight>
===Restoring Key/Value Type Safety for Request-Scoped Data===
As described in [[#Context_Keys,_Values_and_Type_Safety|Context Keys, Values and Type Safety]] section, the fact that the keys and values stored by the Context are declared as <code>interface{}</code> makes them lose the type safety. This technique restores the type safety.
Define an unexported custom key type in the package, usually an alias for <code>int</code>. All keys to be stored within the context must be values of that type. They can be declared as an [[Go_Enumerations#Overview|enumeration]], also package private (unexported). As long as other packages do the same, this prevents collision within the context, even if the underlying key instances are the same int values:
<syntaxhighlight lang='go'>
type contextKey int
const (
userID contextKey = iota
authToken
traceID
)
func requestHandler(ctx context.Context) {
fmt.Printf("userID:    %v\n", ctx.Value(userID))
fmt.Printf("authToken: %v\n", ctx.Value(authToken))
fmt.Printf("traceID:  %v\n", ctx.Value(traceID))
}
...
ctx := context.WithValue(context.Background(), userID, "a@example.com")
ctx = context.WithValue(ctx, authToken, "AB00BC")
ctx = context.WithValue(ctx, traceID, int64(100000))
go requestHandler(ctx)
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 19:56, 14 March 2024

External

Internal

Overview

The context package provides two, somewhat unrelated, features.

One is the capability to preempt, or cancel, blocking code executed by goroutines running downstream in the call graph. The context package provides an API for cancelling branches of the call graph. Cancellation can be programmatic, on timeout or on deadline. This is an idiomatic preemption pattern, equivalent, but preferred to the Done channel pattern. It is preferred because its implementation is available in the standard library, making it a standard Go idiom to consider when working with concurrent code. The implementation is also more expressive.

The second feature is the capability to store request-scoped data and propagate this state through the call graph.

Explicit context propagation, as an argument of functions implementing the pattern, is a key difference between Go and other languages.

Concepts

Context

https://pkg.go.dev/context#Context

context.Context is an interface exposed by the context package. The Context instances flow across API boundaries and through the system in the same way Done channels do. The role of the Context instances is to carry cancellation signals, deadlines and request-scoped values. To implement the pattern, each function downstream from the top level concurrent call must take a Context as parameter. Conventionally, is the first parameter of the function. For more rules of using Contexts, see Programming Model. The implementations must ensure that they are concurrent-safe, so their methods may be called by multiple goroutines simultaneously.

type Context interface {

	// Deadline returns the time when work done on behalf of this context should be canceled. 
	Deadline() (deadline time.Time, ok bool)

	// Done returns a channel that's closed when work done on behalf of this context should be canceled. 
	Done() <-chan struct{}

	// If Done is not yet closed, Err returns nil. If Done is closed, Err returns a non-nil error 
	// explaining why: Canceled if the context was canceled or DeadlineExceeded if the context's deadline passed.
	Err() error

	// Value returns the value associated with this context for key, or nil if no value is associated with key. 
	Value(key any) any
}

The Context interface does not expose any method that allows cancelling the Context, and the Done channel is read only, it cannot be closed. This protects functions up the call stack from children cancelling the context: only the parent can cancel the children functions, as shown in the Programmatic Preemption (Cancellation) section. The function receiving the cancellation signal is not the one that sends the signal.

The Context interface does not expose any method that can mutate the state of the underlying structure, either. The context is immutable, so when new values are stored, the context is actually cloned, with extra metadata.

Err()

ctx.Err() returns an error indicating why the Context was canceled, as follows:

  • nil is the Done channel is not closed yet.
  • the context.Canceled error, if the context was programmatically canceled by invoking the cancel function.
  • the context.DeadlineExceeded error, if the context timed out or the context's deadline has passed.

The Background Context

https://pkg.go.dev/context#Background

The background Context is a non-null, empty, no-deadline and no-values Context returned by the context.Background() function. This context is never canceled, and it is typically used by the main function, initialization and tests, and as the top-level Context for incoming requests. The background Context is the root of the context tree.

The TODO Context

TODO is intended to serve as a placeholder for when you don't know which Context to utilize, or if you expect your code to be provided with a Context, but the upstream code hasn't yet furnished one. TODO is not meant for use in production.

Derived Context and the Context Hierarchy

Cancellation or data storage can only be performed on a derived context. The context package provide functions that derive new Context instances from an existing context: WithCancel(), WithTimeout(), WithDeadline() and WithValue(). The Context instances created this way form a tree. The Background context is the root of this tree. When a Context instance is canceled, all Context instances derived from it are also canceled.

Preemption (Cancellation)

Preemption, or cancellation of a function executing a blocking operation on a channel, or any other blocking operation (disk, network IO, user), consists in the immediate return from the blocking operation, and usually return from the function with a cancellation error. Programmatic cancelation is useful in stopping processing or handling application shutdown. Cancellation is initiated by a parent goroutine in the call graph.

Cancellation Signal - the Done Channel

The "cancellation signal" carried by the context is a Done channel that is closed when the functions associated with the context need to be preempted. The channel is returned by the context's Done() method. The functions typically read the channel in a select statement, and when the read returns, because the channel was closed, the functions should abandon their work and return. For an example of how to do that, see Programmatic Preemption below.

Also see:

Go Channels | The Done Channel

Cancel Function

The cancel function is a function returned by the invocation of the WithCancel function, which then can be used to cancel the context returned by the same invocation of the WithCancel function. For an example, see Programmatic Preemption below.

Deadline

A deadline is communicated to the user by the result of the Context's Deadline() method. The method returns the time when work done on behalf of this Context should be cancelled, or a false value if no deadline is set. Checking the deadline programmatically allows the function to fail fast, which, in programs that may have a high cost for the next bit of functionality, this may save a significant amount of time. This approach implies that we need to have some idea of how long the subordinate call graph will take, which may not be trivial.

func someFunc(ctx context.Context) error {
	if deadline, ok := ctx.Deadline(); ok {
		// the context has a deadline
		if deadline.Sub(time.Now().Add(1 * time.Minute)) <= 0 {
			return context.DeadlineExceeded
		}
	}
	// the context does not have a deadline
	...
}

Also see Programmatic Deadline section, below.

Timeout

See Programmatic Timeout section, below.

Call Graph

Requests and Request-Scoped Data

One of the primary use of goroutines is to service requests. Each incoming request is handled in its own goroutine. Request handlers often start additional goroutines to access backends such as databases and RPC services. The set of goroutines working on a request typically need access to request-specific information, such as user identity, authorization tokens, and the request's deadline, in addition to the capability to cancel the goroutine tree.

The Context exposes the Value() method, which returns a request-scoped value for a given key. For an example of how to do this programmatically, see Storing Request-Scoped Data below.

The package documentation is quite vague on what kind of data should be stored in the Context: "Use context values only for request-scoped data that transits processes and API boundaries, not for passing optional parameters to functions." The following heuristics may help:

  1. The data should transit process or API boundaries. If you generate the data in the process' memory, it's probably not a good candidate, unless you also pass it across an API boundary.
  2. The data should be immutable. If it is not, then by definition what you're storing did not come from a request.
  3. The data should trend toward simple types. This way, it is much easier for the other side to pull the data if it does not have to import a complex graph of packages.
  4. The data should be data, not types with methods.
  5. The data should help decorate operations, not drive them. If your algorithm behaves differently based on what is or isn't included in the Context, you have likely crossed over into the territory of optional parameters.

Using the value propagation feature of the Context is dubious. Don't use Context values for stuff that should be regular dependencies between components. Use Context values for data that can't be passed to your program in any other way: only data that is request-scoped, stuff that is created at the beginning of a request lifecycle, like request ID, etc. If the information is available when the program starts or at any point prior to when the request starts, do not use Context values. This is the case for database handles, loggers, etc.

Context Keys, Values and Type Safety

Since Context's keys and values are defined as interface{}, we lose Go's type safety when attempting to retrieve values. A specific technique can be employed to restore type safety, as described in the Restoring Key/Value Type Safety for Request-Scoped Data section.

Passing Loggers in Context

See:

Zap Concepts | Loggers and context.Context

Programming Model

  • Each function downstream from the top level concurrent call must take a Context as the first parameter.
  • Context instances cannot be mutated or cancelled directly, as explained above.
  • To allow a parent goroutine to cancel its children, create a new Context instance with one of the WithCancel(), WithTimeout() and WithDeadline() methods and pass that instance to the function executing on the child goroutine. The derived Context instance can be canceled sooner than the parent context. See:
  • If the parent goroutine does not need to modify the cancellation behavior of the functions running on children goroutines, it will pass the unmodified Context. In this way, successive layers of the call graph can create Context instances that adhere to their needs, without affecting the parents. This composability enables writing large system without mixing concerns through the call graph.
  • The Context associated with an incoming request is typically canceled when the request handler returns.
  • Only pass Context instances as parameters to functions. Do not store references to Context instances. These instances may look equivalent from the outside, but internally may change at every stack frame.

Programmatic Preemption (Cancellation)

https://pkg.go.dev/context#WithCancel
https://pkg.go.dev/context#WithCancelCause

This section documents the idiomatic pattern to preempt, or cancel, a blocking function.

It consists in a someFunc() function that gets a Context instance as its first argument. The function will select reading the Done channel returned by the Context's Done() method. When the context is externally cancelled, the Done channel is closed, reading operation on the Done channel unblocks and allows the function to return. The recommended return value in this case is the error returned by ctx.Err().

// someFunc will return a context.Canceled error if the context was externally canceled
func someFunc(ctx context.Context, c <-chan interface{}) error {
	for {
		select {
		case <-ctx.Done():
			return ctx.Err() // context.Canceled if the context was externally canceled 
		case <-c: // this will ensure the goroutine will block, as we will never write on that channel
		}
	}
}

The parent goroutine must use WithCancel() function to create a new derived context to be passed to functions executed on children goroutines. The function also returns a cancel function.

Alternatively, WithCancelCause() can be used. The function behaves like WithCancel() but returns a cancel function that allows specifying the error (the "cause") when cancelling the context, which can be retrieved using Cause().

To externally cancel the context, and implicitly all functions listening on its Done channel, invoke the cancel function:

ctx, cancel := context.WithCancel(context.Background())

// spin off someFunc() on its own thread and get it to block by reading on the channel we will never write on
go func() {
	err := someFunc(ctx, make(chan interface{}))
	fmt.Printf("someFunc() errored out because %v\n", err)
}()

// spin off the anonymous function that will cancel someFunc() after 5 seconds
go func() {
	time.Sleep(5 * time.Second)
	cancel() // Calling the cancel() function cancels the context
}()

Programmatic Timeout

The WithTimeout() function creates a new derived context that closes its Done channel after the given timeout. The Context error becomes context.DeadlineExceeded. The WithTimeout() invocation also returns a cancel function that can be used independently of the timeout.

someFunc() function gets a Context instance as its first argument. The function will select reading the Done channel returned by the Context's Done() method. When the context times out, the Done channel is closed, reading operation on the Done channel unblocks and allows the function to return. The recommended return value in this case is the error returned by ctx.Err(), which, in this case, is context.DeadlineExceeded. At the same time, the Context can be externally cancelled by inferring the cancel function, and in that case, the error will be context.Canceled.

// someFunc will return a context.DeadlineExceeded error if the context timed out.
func someFunc(ctx context.Context, c <-chan interface{}) error {
	for {
		select {
		case <-ctx.Done():
			return ctx.Err() // context.DeadlineExceeded if the context timed out
		case <-c: // this will ensure the goroutine will block, as we will never write on that channel
		}
	}
}

The parent goroutine must use WithTimeout() function to create a new derived context to be passed to functions executed on children goroutines. The function also returns a cancel function that can be optionally used, independent of the timeout. Typically, the function is deferred.

Alternatively, WithTimeoutCause() function can be used. This function allows setting the error (the "cause") returned by the context with the timeout expires, instead of the default context.DeadlineExceeded.

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

// spin off someFunc() on its own thread and get it to block by reading on the channel we will never write on
go func() {
	err := someFunc(ctx, make(chan interface{}))
	fmt.Printf("someFunc() errored out because %v\n", err)
}()

Simple Timeout on a Channel

A simpler pattern to time out on a time channel is available here:

Timing-out select

Programmatic Deadline

The WithDeadline() function creates a new derived context that closes its Done channel when the machine's clock advances past the given deadline.

Storing Request-Scoped Data

To store the data in the context, create an immutable clone with context.WithValue().

The keys must satisfy Go's notion of comparability. The values must be safe to access concurrently.

// storing data in Context
ctx := context.WithValue(context.Background(), "auth-token", "a023k27l")
ctx = context.WithValue(ctx, "user-id", "a@example.com")
go someFunc(ctx)

To retrieve data from the context, use the Value() method:

func someFunc(ctx context.Context) {
	// retrieving data from Context
	fmt.Printf("auth-token: %v\n", ctx.Value("auth-token"))
	fmt.Printf("user-id:    %v\n", ctx.Value("user-id"))
}

Restoring Key/Value Type Safety for Request-Scoped Data

As described in Context Keys, Values and Type Safety section, the fact that the keys and values stored by the Context are declared as interface{} makes them lose the type safety. This technique restores the type safety.

Define an unexported custom key type in the package, usually an alias for int. All keys to be stored within the context must be values of that type. They can be declared as an enumeration, also package private (unexported). As long as other packages do the same, this prevents collision within the context, even if the underlying key instances are the same int values:

type contextKey int

const (
	userID contextKey = iota
	authToken
	traceID
)

func requestHandler(ctx context.Context) {
	fmt.Printf("userID:    %v\n", ctx.Value(userID))
	fmt.Printf("authToken: %v\n", ctx.Value(authToken))
	fmt.Printf("traceID:   %v\n", ctx.Value(traceID))
}

...

ctx := context.WithValue(context.Background(), userID, "a@example.com")
ctx = context.WithValue(ctx, authToken, "AB00BC")
ctx = context.WithValue(ctx, traceID, int64(100000))
go requestHandler(ctx)