From a34c967eaf2e1bda05eadc5c1af59fe8060ca0dc Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Thu, 5 Sep 2019 13:30:16 -0700 Subject: [PATCH 1/2] Clarify use of buffered channel in the timeouts example. The buffered channel prevents goroutine leaks in case the channel doesn't end up being read (as indeed happens to c1). Updates #207 --- examples/timeouts/timeouts.go | 5 ++++- examples/timeouts/timeouts.hash | 4 ++-- public/timeouts | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/timeouts/timeouts.go b/examples/timeouts/timeouts.go index ccc9ecb..ce1d1cd 100644 --- a/examples/timeouts/timeouts.go +++ b/examples/timeouts/timeouts.go @@ -12,7 +12,10 @@ func main() { // For our example, suppose we're executing an external // call that returns its result on a channel `c1` - // after 2s. + // after 2s. Note that the channel is buffered, so the + // send in the goroutine is nonblocking. This is a + // common pattern to prevent goroutine leaks in case the + // channel is never read. c1 := make(chan string, 1) go func() { time.Sleep(2 * time.Second) diff --git a/examples/timeouts/timeouts.hash b/examples/timeouts/timeouts.hash index 64d1c5a..f510bfa 100644 --- a/examples/timeouts/timeouts.hash +++ b/examples/timeouts/timeouts.hash @@ -1,2 +1,2 @@ -93343e1aacb14f818c87732914c29ba57afab245 -MgcfA-xpJO9 +b1e8d0efbabd0c52271a85fad5ad58dcd1c7c476 +gyY_qDsRVUe diff --git a/public/timeouts b/public/timeouts index c00732d..532dbc0 100644 --- a/public/timeouts +++ b/public/timeouts @@ -44,7 +44,7 @@ elegant thanks to channels and select.

- +
package main
 
@@ -80,7 +80,10 @@ elegant thanks to channels and select.

For our example, suppose we’re executing an external call that returns its result on a channel c1 -after 2s.

+after 2s. Note that the channel is buffered, so the +send in the goroutine is nonblocking. This is a +common pattern to prevent goroutine leaks in case the +channel is never read.

From 9889d7f702a06b0d0b58d37279aa2976c0ca3993 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 6 Sep 2019 07:37:49 -0700 Subject: [PATCH 2/2] This transition paragraph no longer makes sense --- examples/timeouts/timeouts.sh | 6 ------ public/timeouts | 19 ++----------------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/examples/timeouts/timeouts.sh b/examples/timeouts/timeouts.sh index 8a9dfb4..3c4344f 100644 --- a/examples/timeouts/timeouts.sh +++ b/examples/timeouts/timeouts.sh @@ -3,9 +3,3 @@ $ go run timeouts.go timeout 1 result 2 - -# Using this `select` timeout pattern requires -# communicating results over channels. This is a good -# idea in general because other important Go features are -# based on channels and `select`. We'll look at two -# examples of this next: timers and tickers. diff --git a/public/timeouts b/public/timeouts index 532dbc0..8142985 100644 --- a/public/timeouts +++ b/public/timeouts @@ -156,7 +156,7 @@ from c2 will succeed and we’ll print the result.

out and the second succeeding.

- +
$ go run timeouts.go 
 timeout 1
@@ -166,21 +166,6 @@ out and the second succeeding.

- - -

Using this select timeout pattern requires -communicating results over channels. This is a good -idea in general because other important Go features are -based on channels and select. We’ll look at two -examples of this next: timers and tickers.

- - - - - - - - @@ -194,7 +179,7 @@ examples of this next: timers and tickers.