Contents
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 PatternsGo 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
selectare evaluated. - The
selectblocks until one case can proceed, then runs that case. - If several cases are ready, the choice is pseudo-random.
- A
defaultclause runs immediately when no channel is ready.
- All channel operations in a
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.
1 | func main() { |
Multiplexing
Merge several inputs into one. Common approaches: spin goroutines per input, or use for + select.
1 | func main() { |
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.
1 | type Message struct { |
for-select
Same fan-in as above, but one goroutine and a for + select instead of two helper goroutines.
1 | func fanIn(input1, input2 <-chan string) <-chan string { |
timeout-select
Add a timeout case so a blocked select can bail out.
1 | func main() { |
quit channel
Shut down early with a channel.
- just quit
1 | //main |
- use quit to deliver a message:
quit := make(chan string)
Daisy-chain
StackOverflow has a thread on this pattern.
1 | func f(left, right chan int) { |
Advanced Go Concurrency Patterns
Three techniques
for-selectloop — 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 letCloseask the loop to shut down and return any error without a data race. - Nil channels in
selectcases — send/receive on a nil channel block forever; inselectyou 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 | func (s *sub) Close() error { |
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 | case <-startFetch: |
After — at most one of startFetch and fetchDone is non-nil at a time.
1 | type fetchResult struct{ fetched []Item; next time.Time; err error } |
An RSS aggregator demo pulls these patterns together — worth reading through.
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
