Commit 59b0df9b authored by Bryan C. Mills's avatar Bryan C. Mills Committed by Bryan Mills

net/context: Don't leak goroutines in Done example.

The current example leaks the DoSomethingSlow goroutine for an
arbitrarily long time.  In a real server, that can lead to an
out-of-memory failure during events such as network outages; a
more careful version of that example would be too long for a
simple package doc.

Fortunately, there are other short, common patterns using Done
that don't leak and don't require a lot of explanation.  Let's
use one of those instead.

Change-Id: I0ad0c6121d06b757a397e0e71be9e01ccfd75f77
Reviewed-on: https://go-review.googlesource.com/4490Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
parent ec180793
......@@ -64,16 +64,19 @@ type Context interface {
//
// Done is provided for use in select statements:
//
// // CancelableOperation calls UncancelableOperation and returns as soon as
// // it returns or ctx.Done is closed.
// func CancelableOperation(ctx context.Context) (Result, error) {
// c := make(chan Result, 1)
// go func() { c <- UncancelableOperation() }()
// // Stream generates values with DoSomething and sends them to out
// // until DoSomething returns an error or ctx.Done is closed.
// func Stream(ctx context.Context, out <-chan Value) error {
// for {
// v, err := DoSomething(ctx)
// if err != nil {
// return err
// }
// select {
// case res := <-c:
// return res, nil
// case <-ctx.Done():
// return nil, ctx.Err()
// return ctx.Err()
// case out <- v:
// }
// }
// }
//
......
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