Go concurrency patterns — Rob Pike talks and advanced techniques

Contents

  1. 1. Concurrency Is Not Parallelism
    1. 1.1. Concurrency
    2. 1.2. Parallelism
    3. 1.3. VS
    4. 1.4. Go Concurrency Support
  2. 2. Go Concurrency Patterns
    1. 2.1. Service Channel
    2. 2.2. Multiplexing
    3. 2.3. Sequencing
    4. 2.4. for-select
    5. 2.5. timeout-select
    6. 2.6. quit channel
    7. 2.7. Daisy-chain
  3. 3. Advanced Go Concurrency Patterns
    1. 3.1. Three techniques
    2. 3.2. other improvement

Go’s concurrency patterns are worth collecting in one place. Mostly notes from official talks: definitions, idioms, and a few advanced techniques.

Sources:
Advanced Go Concurrency Patterns

Go Concurrency Patterns

Concurrency Is Not Parallelism

Concurrency Is Not Parallelism

Concurrency is how a program organizes work so it can handle many things at once.

Parallelism is when a program actually runs many things at the same time.

Rob Pike: concurrency is about structure; parallelism is about execution.

Concurrency

Concurrency is the composition of independently executing computations.

Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.

Concurrency is about dealing with lots of things at once.

Parallelism

Parallelism is the simultaneous execution of (possibly related) computations.

Parallelism is about doing lots of things at once.

VS

Concurrency is about structure, parallelism is about execution.

Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.

Go Concurrency Support

  • Concurrent execution (goroutines)
  • Synchronization and messaging (channels) — share memory by communicating (CSP)
  • See also the Go memory model
  • Multi-way control (select)
    • All channel operations in a select are evaluated.
    • The select blocks until one case can proceed, then runs that case.
    • If several cases are ready, the choice is pseudo-random.
    • A default clause runs immediately when no channel is ready.

Go Concurrency Patterns

Service Channel

Channels are first-class values in Go, like strings. Return a channel from a function and interact through it — it behaves like a little service.

Run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func main() {
joe := boring("Joe")
ann := boring("Ann")
for i := 0; i < 5; i++ {
fmt.Println(<-joe)
fmt.Println(<-ann)
}
fmt.Println("You're both boring; I'm leaving.")
}
func boring(msg string) <-chan string { // Returns receive-only channel of strings.
c := make(chan string)
go func() { // We launch the goroutine from inside the function.
for i := 0; ; i++ {
c <- fmt.Sprintf("%s %d", msg, i)
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}()
return c // Return the channel to the caller.
}

Multiplexing

Merge several inputs into one. Common approaches: spin goroutines per input, or use for + select.

Run

1
2
3
4
5
6
7
8
9
10
11
12
13
func main() {
c := fanIn(boring("Joe"), boring("Ann"))
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
fmt.Println("You're both boring; I'm leaving.")
}
func fanIn(input1, input2 <-chan string) <-chan string {
c := make(chan string)
go func() { for { c <- <-input1} }()
go func() { for { c <- <-input2} }()
return c
}

fan-in: multiplex input channels onto one output channel until inputs close.
fan-out: multiple goroutines read from the same channel until it closes.
Pipelines

Sequencing

Force strict order with a shared wait channel so each step hands off to the next.

Run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type Message struct {
str string
wait chan bool
}
//main
for i := 0; i < 5; i++ {
msg1 := <-c; fmt.Println(msg1.str)
msg2 := <-c; fmt.Println(msg2.str)
msg1.wait <- true
msg2.wait <- true
}

//boring
waitForIt := make(chan bool) // Give main control over our execution.
go func() { // Launch the goroutine from inside the function. Function Literal.
for i := 0; ; i++ {
c <- Message{fmt.Sprintf("%s %d", msg, i), waitForIt}
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)

<-waitForIt // Block until main tells us to go again.
}
}()

for-select

Same fan-in as above, but one goroutine and a for + select instead of two helper goroutines.

1
2
3
4
5
6
7
8
9
10
11
12
func fanIn(input1, input2 <-chan string) <-chan string {
c := make(chan string)
go func() {
for {
select {
case s := <-input1: c <- s
case s := <-input2: c <- s
}
}
}()
return c
}

timeout-select

Add a timeout case so a blocked select can bail out.

1
2
3
4
5
6
7
8
9
10
11
12
func main() {
c := boring("Joe")
for {
select {
case s := <-c:
fmt.Println(s)
case <-time.After(1 * time.Second):
fmt.Println("You're too slow.")
return
}
}
}

quit channel

Shut down early with a channel.

  • just quit
1
2
3
4
5
6
7
8
9
10
11
12
//main
quit := make(chan struct{})
c := boring("Joe", quit)
for i := rand.Intn(10); i >= 0; i-- { fmt.Println(<-c) }
quit <- struct{}

select {
case c <- fmt.Sprintf("%s: %d", msg, i):
// do nothing
case <-quit:
return
}
  • use quit to deliver a message: quit := make(chan string)

Daisy-chain

StackOverflow has a thread on this pattern.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func f(left, right chan int) {
left <- 1 + <-right
}

func main() {
const n = 10000
leftmost := make(chan int)
right := leftmost
left := leftmost
for i := 0; i < n; i++ {
right = make(chan int)
go f(left, right)
left = right
}
go func(c chan int) { c <- 1 }(right)
fmt.Println(<-leftmost)
}

Advanced Go Concurrency Patterns

Three techniques

  • for-select loop — keeps the loop from sticking on one case so you can juggle multiple responsibilities.
  • Service channel, reply channels (chan chan error) — the service pattern; reply channels let Close ask the loop to shut down and return any error without a data race.
  • Nil channels in select cases — send/receive on a nil channel block forever; in select you can use that to disable cases dynamically.

Close via reply channel: the loop receives a chan error, sends back the error, then closes resources.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func (s *sub) Close() error {
errc := make(chan error)
s.closing <- errc // HLchan — request shutdown
return <-errc // HLchan — wait for result
}
//in loop
var err error
for{
select{
case errc := <-s.closing:
errc <- err
close(s.updates)
return
}
}

Other tips from the talk:

other improvement

  • limit — cap pending work so queues and memory do not grow without bound.
  • async I/O — split fetch from result handling; sync with state channels so a slow fetch does not stall the rest of the loop.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
case <-startFetch:
var fetched []Item
fetched, next, err = s.fetcher.Fetch()
if err != nil {
next = time.Now().Add(10 * time.Second)
break
}
for _, item := range fetched {
if !seen[item.GUID] {
pending = append(pending, item)
seen[item.GUID] = true
}
}

After — at most one of startFetch and fetchDone is non-nil at a time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type fetchResult struct{ fetched []Item; next time.Time; err error }

var fetchDone chan fetchResult // if non-nil, Fetch is running

var startFetch <-chan time.Time
if fetchDone == nil && len(pending) < maxPending {
startFetch = time.After(fetchDelay) // enable fetch case
}
select {
case <-startFetch:
fetchDone = make(chan fetchResult, 1)
go func() {
fetched, next, err := s.fetcher.Fetch()
fetchDone <- fetchResult{fetched, next, err}
}()
case result := <-fetchDone:
fetchDone = nil
// Use result.fetched, result.next, result.err

An RSS aggregator demo pulls these patterns together — worth reading through.

Run

Questions? Leave a comment below or email: newbvirgil@gmail.com