Skip to content

Commit 26dd2cd

Browse files
Cleaned up the example to show more of the performance gains for buffered channels.
1 parent ed2a7ea commit 26dd2cd

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// All material is licensed under the GNU Free Documentation License
22
// https://github.com/ArdanStudios/gotraining/blob/master/LICENSE
33

4-
// http://play.golang.org/p/T3QupG_7_X
4+
// http://play.golang.org/p/yOn3nZU5rf
55

66
// This sample program demonstrates how to use a buffered
77
// channel to receive results from other goroutines in a guaranteed way.
@@ -14,6 +14,13 @@ import (
1414
"time"
1515
)
1616

17+
// result is what is sent back from each operation.
18+
type result struct {
19+
id int
20+
op string
21+
err error
22+
}
23+
1724
// init called before main.
1825
func init() {
1926
// Seed the random number generator.
@@ -34,30 +41,30 @@ func performInserts() {
3441
const inserts = routines * 2
3542

3643
// Buffered channel to receive information about any possible insert.
37-
ch := make(chan error, inserts)
44+
ch := make(chan result, inserts)
3845

3946
// Number of responses we need to handle.
4047
waitInserts := inserts
4148

4249
// Perform all the inserts.
4350
for i := 0; i < routines; i++ {
4451
go func(id int) {
45-
ch <- insertDoc1(id)
46-
ch <- insertDoc2(id)
52+
ch <- insertUser(id)
53+
54+
// We don't need to wait to start the second insert
55+
// thanks to the buffered channel. The first send
56+
// will happen immediately.
57+
ch <- insertTrans(id)
4758
}(i)
4859
}
4960

5061
// Process the insert results as they complete.
5162
for waitInserts > 0 {
5263
// Wait for a response from a goroutine.
53-
err := <-ch
64+
r := <-ch
5465

5566
// Display the result.
56-
if err != nil {
57-
log.Println("Received error:", err)
58-
} else {
59-
log.Println("Received nil error")
60-
}
67+
log.Printf("ID: %d OP: %s ERR: %v", r.id, r.op, r.err)
6168

6269
// Decrement the wait count and determine if we are done.
6370
waitInserts--
@@ -66,26 +73,32 @@ func performInserts() {
6673
log.Println("Inserts Complete")
6774
}
6875

69-
// insertDoc1 simulates a database operation.
70-
func insertDoc1(id int) error {
71-
log.Println("Insert document 1: ", id)
76+
// insertUser simulates a database operation.
77+
func insertUser(id int) result {
78+
r := result{
79+
id: id,
80+
op: fmt.Sprintf("insert USERS value (%d)", id),
81+
}
7282

7383
// Randomize if the insert fails or not.
7484
if rand.Intn(10) == 0 {
75-
return fmt.Errorf("Document ID: %d", id)
85+
r.err = fmt.Errorf("Unable to insert %d into USER table", id)
7686
}
7787

78-
return nil
88+
return r
7989
}
8090

81-
// insertDoc2 simulates a database operation.
82-
func insertDoc2(id int) error {
83-
log.Println("Insert document 2: ", id)
91+
// insertTrans simulates a database operation.
92+
func insertTrans(id int) result {
93+
r := result{
94+
id: id,
95+
op: fmt.Sprintf("insert TRANS value (%d)", id),
96+
}
8497

8598
// Randomize if the insert fails or not.
8699
if rand.Intn(10) == 0 {
87-
return fmt.Errorf("Document ID: %d", id)
100+
r.err = fmt.Errorf("Unable to insert %d into USER table", id)
88101
}
89102

90-
return nil
103+
return r
91104
}

07-concurrency_channels/03-channels/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html
2626

2727
[Unbuffered channels - Relay race](example2/example2.go) ([Go Playground](http://play.golang.org/p/AzJuQsPG3a))
2828

29-
[Buffered channels - Retrieving results](example3/example3.go) ([Go Playground](http://play.golang.org/p/T3QupG_7_X))
29+
[Buffered channels - Retrieving results](example3/example3.go) ([Go Playground](http://play.golang.org/p/yOn3nZU5rf))
3030

3131
[Timer channels and Select](example4/example4.go) ([Go Playground](http://play.golang.org/p/KuMG3o_7-C))
3232

0 commit comments

Comments
 (0)