diff options
author | Russ Cox <rsc@golang.org> | 2020-03-09 22:11:04 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2020-03-11 14:10:08 +0000 |
commit | 482079d678d84e207dd9ae63266c4bd4e653886b (patch) | |
tree | 62aa3b630bbe982904f5495fe2cc53d60a87c92d /content/go-concurrency-patterns-timing-out-and.article | |
parent | 0b4fcd39865e575704b5928c9a8f1cd21e18e8b2 (diff) |
content: wrap long lines using new program wrap.go
Wrapping long lines will make diffs easier to read
for the eventual conversion to Markdown.
For golang/go#33955.
Change-Id: Ibcc1b5a84ccc9144b5fcdc9266f2da3e2cf3c5a3
Reviewed-on: https://go-review.googlesource.com/c/blog/+/222839
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'content/go-concurrency-patterns-timing-out-and.article')
-rw-r--r-- | content/go-concurrency-patterns-timing-out-and.article | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/content/go-concurrency-patterns-timing-out-and.article b/content/go-concurrency-patterns-timing-out-and.article index 93411fd..0789006 100644 --- a/content/go-concurrency-patterns-timing-out-and.article +++ b/content/go-concurrency-patterns-timing-out-and.article @@ -6,7 +6,13 @@ Andrew Gerrand * Introduction -Concurrent programming has its own idioms. A good example is timeouts. Although Go's channels do not support them directly, they are easy to implement. Say we want to receive from the channel `ch`, but want to wait at most one second for the value to arrive. We would start by creating a signalling channel and launching a goroutine that sleeps before sending on the channel: +Concurrent programming has its own idioms. +A good example is timeouts. Although Go's channels do not support them directly, +they are easy to implement. +Say we want to receive from the channel `ch`, +but want to wait at most one second for the value to arrive. +We would start by creating a signalling channel and launching a goroutine +that sleeps before sending on the channel: timeout := make(chan bool, 1) go func() { @@ -14,7 +20,9 @@ Concurrent programming has its own idioms. A good example is timeouts. Although timeout <- true }() -We can then use a `select` statement to receive from either `ch` or `timeout`. If nothing arrives on `ch` after one second, the timeout case is selected and the attempt to read from ch is abandoned. +We can then use a `select` statement to receive from either `ch` or `timeout`. +If nothing arrives on `ch` after one second, +the timeout case is selected and the attempt to read from ch is abandoned. select { case <-ch: @@ -23,13 +31,24 @@ We can then use a `select` statement to receive from either `ch` or `timeout`. I // the read from ch has timed out } -The `timeout` channel is buffered with space for 1 value, allowing the timeout goroutine to send to the channel and then exit. The goroutine doesn't know (or care) whether the value is received. This means the goroutine won't hang around forever if the `ch` receive happens before the timeout is reached. The `timeout` channel will eventually be deallocated by the garbage collector. +The `timeout` channel is buffered with space for 1 value, +allowing the timeout goroutine to send to the channel and then exit. +The goroutine doesn't know (or care) whether the value is received. +This means the goroutine won't hang around forever if the `ch` receive happens +before the timeout is reached. +The `timeout` channel will eventually be deallocated by the garbage collector. -(In this example we used `time.Sleep` to demonstrate the mechanics of goroutines and channels. In real programs you should use ` [[https://golang.org/pkg/time/#After][time.After]]`, a function that returns a channel and sends on that channel after the specified duration.) +(In this example we used `time.Sleep` to demonstrate the mechanics of goroutines and channels. +In real programs you should use ` [[https://golang.org/pkg/time/#After][time.After]]`, +a function that returns a channel and sends on that channel after the specified duration.) -Let's look at another variation of this pattern. In this example we have a program that reads from multiple replicated databases simultaneously. The program needs only one of the answers, and it should accept the answer that arrives first. +Let's look at another variation of this pattern. +In this example we have a program that reads from multiple replicated databases simultaneously. +The program needs only one of the answers, +and it should accept the answer that arrives first. -The function `Query` takes a slice of database connections and a `query` string. It queries each of the databases in parallel and returns the first response it receives: +The function `Query` takes a slice of database connections and a `query` string. +It queries each of the databases in parallel and returns the first response it receives: func Query(conns []Conn, query string) Result { ch := make(chan Result) @@ -44,8 +63,20 @@ The function `Query` takes a slice of database connections and a `query` string. return <-ch } -In this example, the closure does a non-blocking send, which it achieves by using the send operation in `select` statement with a `default` case. If the send cannot go through immediately the default case will be selected. Making the send non-blocking guarantees that none of the goroutines launched in the loop will hang around. However, if the result arrives before the main function has made it to the receive, the send could fail since no one is ready. +In this example, the closure does a non-blocking send, +which it achieves by using the send operation in `select` statement with a `default` case. +If the send cannot go through immediately the default case will be selected. +Making the send non-blocking guarantees that none of the goroutines launched +in the loop will hang around. +However, if the result arrives before the main function has made it to the receive, +the send could fail since no one is ready. -This problem is a textbook example of what is known as a [[https://en.wikipedia.org/wiki/Race_condition][race condition]], but the fix is trivial. We just make sure to buffer the channel `ch` (by adding the buffer length as the second argument to [[https://golang.org/pkg/builtin/#make][make]]), guaranteeing that the first send has a place to put the value. This ensures the send will always succeed, and the first value to arrive will be retrieved regardless of the order of execution. +This problem is a textbook example of what is known as a [[https://en.wikipedia.org/wiki/Race_condition][race condition]], +but the fix is trivial. +We just make sure to buffer the channel `ch` (by adding the buffer length +as the second argument to [[https://golang.org/pkg/builtin/#make][make]]), +guaranteeing that the first send has a place to put the value. +This ensures the send will always succeed, +and the first value to arrive will be retrieved regardless of the order of execution. These two examples demonstrate the simplicity with which Go can express complex interactions between goroutines. |