Re: [go-nuts] How do you implement and use Context.Done?

2017-02-21 Thread 'Thomas Bushnell, BSG' via golang-nuts
Oops! ::blush::

On Tue, Feb 21, 2017 at 11:39 AM Axel Wagner 
wrote:

> Wrong list :) You meant to link here:
> https://godoc.org/context#Context.Err
>
> On Tue, Feb 21, 2017 at 8:15 PM, 'Thomas Bushnell, BSG' via golang-nuts <
> golang-nuts@googlegroups.com> wrote:
>
> On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor  wrote:
>
> On Sun, Feb 19, 2017 at 2:57 PM,   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.  See
> https://godoc.corp.google.com/pkg/google3/go/context/context#Context.Err:
> "Err's return value is unspecified before Done is closed."
>
> Thomas
>
> --
>
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-21 Thread Ian Lance Taylor
On Tue, Feb 21, 2017 at 12:45 PM, Ian Lance Taylor  wrote:
> On Tue, Feb 21, 2017 at 11:15 AM, Thomas Bushnell, BSG
>  wrote:
>> On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor  wrote:
>>>
>>> On Sun, Feb 19, 2017 at 2:57 PM,   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.

Sent https://golang.org/cl/37375 .

Ian

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-21 Thread Ian Lance Taylor
On Tue, Feb 21, 2017 at 11:15 AM, Thomas Bushnell, BSG
 wrote:
> On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor  wrote:
>>
>> On Sun, Feb 19, 2017 at 2:57 PM,   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

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-21 Thread 'Axel Wagner' via golang-nuts
Wrong list :) You meant to link here:
https://godoc.org/context#Context.Err

On Tue, Feb 21, 2017 at 8:15 PM, 'Thomas Bushnell, BSG' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor  wrote:
>
>> On Sun, Feb 19, 2017 at 2:57 PM,   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.  See https://godoc.corp.google.com/pkg/google3/go/context/
> context#Context.Err: "Err's return value is unspecified before Done is
> closed."
>
> Thomas
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-21 Thread 'Thomas Bushnell, BSG' via golang-nuts
On Mon, Feb 20, 2017 at 11:42 AM Ian Lance Taylor  wrote:

> On Sun, Feb 19, 2017 at 2:57 PM,   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.  See
https://godoc.corp.google.com/pkg/google3/go/context/context#Context.Err:
"Err's return value is unspecified before Done is closed."

Thomas

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-20 Thread so . query
I see thanks for the additional detail.



On Monday, February 20, 2017 at 11:42:48 AM UTC-8, Ian Lance Taylor wrote:
>
> On Sun, Feb 19, 2017 at 2:57 PM,   
> 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 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-20 Thread Ian Lance Taylor
On Sun, Feb 19, 2017 at 2:57 PM,   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

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-19 Thread Sina Siadat
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




On Monday, February 20, 2017 at 2:43:26 AM UTC+3:30, Matt Harden wrote:
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.

On Sun, Feb 19, 2017 at 2:57 PM  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?




On Friday, February 17, 2017 at 2:27:29 PM UTC-8, Ian Lance Taylor wrote:

On Fri, Feb 17, 2017 at 1:34 PM,  wrote: 
> 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? 

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 


-- 
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.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-19 Thread Matt Harden
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.

On Sun, Feb 19, 2017 at 2:57 PM  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?
>
>
>
>
> On Friday, February 17, 2017 at 2:27:29 PM UTC-8, Ian Lance Taylor wrote:
>
> On Fri, Feb 17, 2017 at 1:34 PM,   wrote:
> > 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?
>
> 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
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-19 Thread so . query
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?




On Friday, February 17, 2017 at 2:27:29 PM UTC-8, Ian Lance Taylor wrote:
>
> On Fri, Feb 17, 2017 at 1:34 PM,   
> wrote: 
> > 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? 
>
> 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 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How do you implement and use Context.Done?

2017-02-17 Thread Ian Lance Taylor
On Fri, Feb 17, 2017 at 1:34 PM,   wrote:
> 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?

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

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.