How do you implement and use Context.Done?

1,650 views
Skip to first unread message

so.q...@gmail.com

unread,
Feb 17, 2017, 4:34:58 PM2/17/17
to golang-nuts
I'm not sure how to implement and use the Done function and its returned channel for contexts.
https://golang.org/pkg/context/#Context

The comments say to refer to https://blog.golang.org/pipelines, but I didn't see any example there.

Can you provide an example of how to implement a context that supports cancellation with Done?



Ian Lance Taylor

unread,
Feb 17, 2017, 5:27:29 PM2/17/17
to so.q...@gmail.com, golang-nuts
You aren't really expected to write your own implementations of the
Context interface. You are expected to start with context.Background
and modify that context using WithCancel, WithTimeout, WithValue, etc.

For example, if you write
ctx, cancel := ctx.WithCancel(context.Background())
Then ctx.Done() will be closed when you call cancel().

Ian

so.q...@gmail.com

unread,
Feb 19, 2017, 5:57:03 PM2/19/17
to golang-nuts, so.q...@gmail.com
Thanks, I see you build it up with decorators on a standard prototype.
For example: https://play.golang.org/p/PJy5lE9QqF

So context.Done is a convenience function for those that require it? Otherwise a context will expire after it leaves scope, so Done does not need to be called?

Matt Harden

unread,
Feb 19, 2017, 6:13:26 PM2/19/17
to so.q...@gmail.com, golang-nuts
Done does not need to be called unless you want to detect when the context is either canceled or times out. It doesn't have any effect on the context itself.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sina Siadat

unread,
Feb 20, 2017, 2:39:04 AM2/20/17
to golang-nuts, so.q...@gmail.com
Done function returns a channel which is closed when our context is done, i.e., it is either timed out, its deadline is exceeded, or explicitly canceled.

This channel closing is a pattern used for broadcasting a signal to multiple goroutines that are receiving from it. Let's say we have 2 goroutines that are receiving from the channel returned by Done. They are both blocked. When our context is done, both goroutines will unblock. They should interpret this channel closing as a notification that the context is done.

The reason it is designed like this, is that with the alternative normal send/receive to a channel, only one receiver receives the value sent to a channel. But with this pattern, all goroutines that are receiving from that channel will be notified.

Sina Siadat

Ian Lance Taylor

unread,
Feb 20, 2017, 2:42:48 PM2/20/17
to so.q...@gmail.com, golang-nuts
On Sun, Feb 19, 2017 at 2:57 PM, <so.q...@gmail.com> wrote:
> Thanks, I see you build it up with decorators on a standard prototype.
> For example: https://play.golang.org/p/PJy5lE9QqF
>
> So context.Done is a convenience function for those that require it?
> Otherwise a context will expire after it leaves scope, so Done does not need
> to be called?

Cancelling the context just marks the context as cancelled. It does
not actually stop any goroutines using the context. Those goroutines
must themselves periodically check the context to see whether it has
been cancelled, and, if so, stop working. They do that by calling
either the Done or Err method; it's much more common to call the Done
method (and check whether the channel is closed).

Ian

so.q...@gmail.com

unread,
Feb 20, 2017, 7:52:55 PM2/20/17
to golang-nuts, so.q...@gmail.com
I see thanks for the additional detail.

Thomas Bushnell, BSG

unread,
Feb 21, 2017, 2:15:34 PM2/21/17
to Ian Lance Taylor, so.q...@gmail.com, golang-nuts
Calling the Err() method to see if the context has been cancelled is incorrect.  See https://godoc.corp.google.com/pkg/google3/go/context/context#Context.Err: "Err's return value is unspecified before Done is closed."

Thomas

Axel Wagner

unread,
Feb 21, 2017, 2:39:39 PM2/21/17
to Thomas Bushnell, BSG, Ian Lance Taylor, so.q...@gmail.com, golang-nuts
Wrong list :) You meant to link here:

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Ian Lance Taylor

unread,
Feb 21, 2017, 3:46:02 PM2/21/17
to Thomas Bushnell, BSG, so.q...@gmail.com, golang-nuts
On Tue, Feb 21, 2017 at 11:15 AM, Thomas Bushnell, BSG
<tbus...@google.com> wrote:
> On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor <ia...@golang.org> wrote:
>>
>> On Sun, Feb 19, 2017 at 2:57 PM, <so.q...@gmail.com> wrote:
>> > Thanks, I see you build it up with decorators on a standard prototype.
>> > For example: https://play.golang.org/p/PJy5lE9QqF
>> >
>> > So context.Done is a convenience function for those that require it?
>> > Otherwise a context will expire after it leaves scope, so Done does not
>> > need
>> > to be called?
>>
>> Cancelling the context just marks the context as cancelled. It does
>> not actually stop any goroutines using the context. Those goroutines
>> must themselves periodically check the context to see whether it has
>> been cancelled, and, if so, stop working. They do that by calling
>> either the Done or Err method; it's much more common to call the Done
>> method (and check whether the channel is closed).
>
>
> Calling the Err() method to see if the context has been cancelled is
> incorrect.

...

> "Err's return value is unspecified before Done is closed."

Hmmm, that text is not in the version of the context package included
in the Go standard library. Perhaps it should be.

Ian

Ian Lance Taylor

unread,
Feb 21, 2017, 3:51:01 PM2/21/17
to Thomas Bushnell, BSG, so.q...@gmail.com, golang-nuts

Thomas Bushnell, BSG

unread,
Feb 21, 2017, 4:26:41 PM2/21/17
to Axel Wagner, Ian Lance Taylor, so.q...@gmail.com, golang-nuts
Oops! ::blush::

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages