These are the notes for Go episode 52. A token counter that prints all 3000 tickets before handing over the first leads to the generator. Breaking out of its loop early leaves a goroutine blocked forever, a goroutine leak, fixed with the done channel pattern (ctx.Done()). Chopping and cooking become stages joined by channels: a pipeline. A slow cooking stage is a bottleneck: fan-out puts four cooks on one channel, and fan-in (merging) collects their plates, 4 times faster but out of order. A worker pool with job and result channels carries an index to serve the plates in order. Last, a hundred goroutines rush a stove with three burners, until a semaphore made from a buffered channel gives bounded parallelism.
All outputs and errors here are from real runs (Go 1.24.7, linux/amd64, a machine with 2 CPUs). Timings and the order of some lines change between runs.
Facts and sources
From the Go blog post Go Concurrency Patterns: Pipelines and cancellation (Sameer Ajmani, 13 March 2014):
- "Informally, a pipeline is a series of stages connected by channels, where each stage is a group of goroutines running the same function."
- "Multiple functions can read from the same channel until that channel is closed; this is called fan-out."
- Fan-in: "A function can read from multiple inputs and proceed until all are closed by multiplexing the input channels onto a single channel that's closed when all the inputs are closed."
- The guidelines: "stages close their outbound channels when all the send operations are done" and "stages keep receiving values from inbound channels until those channels are closed or the senders are unblocked."
- "Goroutines are not garbage collected; they must exit on their own."
What is a pattern?
A pattern is a reusable shape of code that solves a problem that keeps coming up. Today's patterns are all built from parts you already know: goroutines (episode 43), channels (47, 48), select (49), sync (45, 50) and context (51).
Generators
| At the token counter | In Go |
|---|---|
| customers need tokens 1, 2, 3 ... | a stream of values |
| print all 3000 first, then open | return a full slice |
| customer 1 waits for tickets nobody needs yet | slow start, wasted work |
| hand out each token as it's printed | a generator |
The obvious way: build the whole slice
package main
import (
"fmt"
"time"
)
// the obvious way: write ALL the tickets first, then hand them over
func allTickets(n int) []int {
t := make([]int, 0)
for i := 1; i <= n; i++ {
t = append(t, i)
time.Sleep(time.Microsecond) // printing one ticket takes a moment
}
return t
}
func main() {
start := time.Now()
for _, t := range allTickets(3000) {
fmt.Println("first ticket", t, "ready after", time.Since(start).Round(time.Millisecond))
break // we only needed the first one!
}
}
Real output:
first ticket 1 ready after 1.165s
Why it's slow: a function that returns a slice can't give you anything until the whole slice is built.
The fix: return a channel
A generator is a function that returns a (receive-only) channel straight away and fills it from its own goroutine. When it has no more values it closes the channel, so the receiver's range loop ends.
package main
import (
"fmt"
"time"
)
// a generator: returns a channel at once, and fills it from its own goroutine
func tickets(n int) <-chan int {
out := make(chan int)
go func() {
defer close(out) // no more tickets: close, so range ends
for i := 1; i <= n; i++ {
time.Sleep(time.Microsecond)
out <- i
}
}()
return out
}
func main() {
start := time.Now()
for t := range tickets(3000) {
fmt.Println("first ticket", t, "ready after", time.Since(start).Round(time.Millisecond))
break
}
}
Real output:
first ticket 1 ready after 0s
The consumer (the code that receives the values) starts working on the first value while the producer (the code that sends them) is still making the rest.
Goroutine leaks and the done channel pattern
In the generator above, main takes one ticket and breaks out of the loop. The generator's goroutine is still trying to send ticket 2 on a channel that nobody will ever read again, so it's blocked forever. That's a goroutine leak: a goroutine that never ends, holding its memory (and anything it references) until the program exits. As the Go blog says, "Goroutines are not garbage collected; they must exit on their own."
The done channel pattern: every send also listens for a stop signal, using select. Today the stop signal is ctx.Done() from episode 51 (before context, programs passed a done chan struct{} and closed it; ctx.Done() is exactly such a channel).
package main
import (
"context"
"fmt"
"runtime"
"time"
)
func tickets(ctx context.Context, n int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for i := 1; i <= n; i++ {
select {
case out <- i: // send the next ticket...
case <-ctx.Done(): // ...or stop if nobody wants more
return
}
}
}()
return out
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
for t := range tickets(ctx, 3000) {
fmt.Println("got ticket", t)
break
}
fmt.Println("goroutines before cancel:", runtime.NumGoroutine())
cancel() // tell the generator to stop
time.Sleep(10 * time.Millisecond)
fmt.Println("goroutines after cancel: ", runtime.NumGoroutine())
}
Real output:
got ticket 1
goroutines before cancel: 2
goroutines after cancel: 1
runtime.NumGoroutine() counts the goroutines that exist: 2 (main and the stuck generator) before cancel, 1 after.
Pipelines and stages
| In Raju's kitchen | In Go |
|---|---|
| orders come in | the source stage: a generator |
| one person chops, the next cooks | stages: in <-chan T → work → out <-chan U |
| plates pass along the line, everyone works at once | a pipeline: stages joined by channels |
| the waiter serves | the sink: the last stage, often main |
package main
import (
"fmt"
"strings"
)
func orders(dishes ...string) <-chan string { // stage 1: the source
out := make(chan string)
go func() {
defer close(out)
for _, d := range dishes {
out <- d
}
}()
return out
}
func chop(in <-chan string) <-chan string { // stage 2
out := make(chan string)
go func() {
defer close(out)
for d := range in {
out <- "chopped " + d
}
}()
return out
}
func cook(in <-chan string) <-chan string { // stage 3
out := make(chan string)
go func() {
defer close(out)
for d := range in {
out <- strings.ToUpper(d) + " (cooked)"
}
}()
return out
}
func main() {
for plate := range cook(chop(orders("paneer", "dal", "aloo"))) {
fmt.Println(plate) // the last stage: serve
}
}
Real output:
CHOPPED PANEER (cooked)
CHOPPED DAL (cooked)
CHOPPED ALOO (cooked)
Each stage closes its output when its input runs out (defer close(out) after the range), so the close flows down the pipeline and the final range in main ends. Because every stage has the same shape, you can add, remove or reorder stages without changing the others.
Fan-out and fan-in
The bottleneck
package main
import (
"fmt"
"time"
)
func orders(n int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for i := 1; i <= n; i++ {
out <- i
}
}()
return out
}
func cook(in <-chan int) <-chan string { // the slow stage: 50ms a dish
out := make(chan string)
go func() {
defer close(out)
for o := range in {
time.Sleep(50 * time.Millisecond)
out <- fmt.Sprint("dish ", o)
}
}()
return out
}
func main() {
start := time.Now()
for d := range cook(orders(8)) {
fmt.Print(d, " | ")
}
fmt.Println("\none cook, 8 dishes:", time.Since(start).Round(10*time.Millisecond))
}
Real output:
dish 1 | dish 2 | dish 3 | dish 4 | dish 5 | dish 6 | dish 7 | dish 8 |
one cook, 8 dishes: 400ms
8 dishes × 50 ms = 400 ms. A pipeline runs only as fast as its slowest stage: a bottleneck (like the narrow neck of a bottle that limits how fast it pours).
More cooks, one counter
| In Raju's kitchen | In Go |
|---|---|
| four cooks take orders from one rail | fan-out: many goroutines receive from one channel |
| all plates go to one serving counter | fan-in / merge: many channels into one |
| fast dishes come out first | the order is lost |
A channel delivers each value to exactly one receiver, so with fan-out no order is cooked twice.
package main
import (
"fmt"
"sync"
"time"
)
func cook(in <-chan int) <-chan string { // one cook: 50ms a dish
out := make(chan string)
go func() {
defer close(out)
for o := range in {
time.Sleep(50 * time.Millisecond)
out <- fmt.Sprint("dish ", o)
}
}()
return out
}
func merge(chans ...<-chan string) <-chan string { // fan-in: many in, one out
out := make(chan string)
var wg sync.WaitGroup
wg.Add(len(chans))
for _, c := range chans {
go func() { // one forwarder per input channel
defer wg.Done()
for v := range c {
out <- v
}
}()
}
go func() { wg.Wait(); close(out) }() // close when ALL are done
return out
}
func main() {
start := time.Now()
in := make(chan int, 8)
for i := 1; i <= 8; i++ {
in <- i
}
close(in)
for d := range merge(cook(in), cook(in), cook(in), cook(in)) { // fan-out
fmt.Print(d, " | ")
}
fmt.Println("\nfour cooks:", time.Since(start).Round(10*time.Millisecond))
}
Real output:
dish 4 | dish 2 | dish 1 | dish 3 | dish 5 | dish 8 | dish 6 | dish 7 |
four cooks: 100ms
How merge works:
- One forwarder goroutine per input channel copies every value to
out. - A
sync.WaitGroupcounts the forwarders. - One more goroutine waits for all of them, then closes
outonce. If a forwarder closedoutitself, the others would panic withsend on closed channel(see the mistakes table). Only the code that knows every sender is done may close.
Worker pools and ordered results
| In Raju's kitchen | In Go |
|---|---|
| order slips on a rail, each numbered | a jobs channel; each job carries its position |
| a fixed team of 3 cooks | a worker pool: N goroutines started once |
| plates go back to their slip number | a results channel; each result carries the same index |
A worker is a goroutine that loops: take the next job, do it, send the result. A worker pool is a fixed number of workers sharing one jobs channel.
package main
import (
"fmt"
"strings"
"sync"
"time"
)
type job struct{ i int; dish string } // i: position in the order
type result struct{ i int; plate string }
func worker(id int, jobs <-chan job, results chan<- result, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs { // take the next job until the channel closes
time.Sleep(time.Duration(len(j.dish)) * 10 * time.Millisecond)
results <- result{j.i, fmt.Sprintf("%s by cook %d", strings.ToUpper(j.dish), id)}
}
}
func main() {
dishes := []string{"biryani", "dal", "paneer tikka", "roti", "kheer", "aloo"}
jobs := make(chan job)
results := make(chan result)
var wg sync.WaitGroup
for id := 1; id <= 3; id++ { // a pool of 3 workers
wg.Add(1)
go worker(id, jobs, results, &wg)
}
go func() {
for i, d := range dishes {
jobs <- job{i, d}
}
close(jobs) // no more jobs: workers' range loops end
}()
go func() { wg.Wait(); close(results) }()
plates := make([]string, len(dishes))
for r := range results {
fmt.Println("done:", r.plate)
plates[r.i] = r.plate // put it back in order
}
fmt.Println("served in order:", strings.Join(plates, ", "))
}
Real output:
done: DAL by cook 2
done: ROTI by cook 2
done: BIRYANI by cook 1
done: ALOO by cook 1
done: PANEER TIKKA by cook 3
done: KHEER by cook 2
served in order: BIRYANI by cook 1, DAL by cook 2, PANEER TIKKA by cook 3, ROTI by cook 2, KHEER by cook 2, ALOO by cook 1
Three things close in the right order: the feeder closes jobs when every job is sent (so each worker's range ends), the workers call wg.Done(), and a goroutine closes results after wg.Wait() (so main's range ends). The results arrive in any order, but plates[r.i] = r.plate puts each one back in its place: ordered results.
Another way to keep order is to give each job its own result channel and read them in order; the index-in-a-slice way is the simplest when you know how many jobs there are.
Bounded parallelism and semaphores
| On Raju's stove | In Go |
|---|---|
| only 3 burners | a limited resource: database connections, CPU cores, an outside service's limit |
| 100 cooks rush the stove | one goroutine per task, no limit |
| 3 burner keys on a hook | a semaphore: make(chan struct{}, 3) |
The obvious way: one goroutine per task
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var mu sync.Mutex
active, most := 0, 0 // cooks on a burner now, and the most at once
var wg sync.WaitGroup
for i := 1; i <= 100; i++ { // 100 orders, one goroutine each
wg.Add(1)
go func() {
defer wg.Done()
mu.Lock()
active++ // takes a burner
most = max(most, active)
mu.Unlock()
time.Sleep(20 * time.Millisecond) // cooking
mu.Lock()
active--
mu.Unlock()
}()
}
wg.Wait()
fmt.Println("burners on the stove: 3")
fmt.Println("most cooking at once:", most)
}
Real output:
burners on the stove: 3
most cooking at once: 100
max is the built-in function (Go 1.21) that returns the larger value. With a real database, 97 of those requests could fail with "too many connections".
The fix: a semaphore from a buffered channel
A semaphore is a counter of free slots: take a slot before the work, give it back after, and wait if none is free. A buffered channel of capacity N is exactly that: a send takes a slot (and blocks when all N are full), a receive gives it back. struct{} values take no memory.
package main
import (
"fmt"
"sync"
"time"
)
func main() {
start := time.Now()
var mu sync.Mutex
active, most := 0, 0 // cooks on a burner now, and the most at once
sem := make(chan struct{}, 3) // a semaphore: 3 slots = 3 burners
var wg sync.WaitGroup
for i := 1; i <= 100; i++ { // 100 orders, one goroutine each
wg.Add(1)
go func() {
defer wg.Done()
sem <- struct{}{} // take a slot: blocks while all 3 are taken
defer func() { <-sem }() // give the slot back when done
mu.Lock()
active++ // takes a burner
most = max(most, active)
mu.Unlock()
time.Sleep(20 * time.Millisecond) // cooking
mu.Lock()
active--
mu.Unlock()
}()
}
wg.Wait()
fmt.Println("burners on the stove: 3")
fmt.Println("most cooking at once:", most)
fmt.Println("100 orders took:", time.Since(start).Round(10*time.Millisecond))
}
Real output:
burners on the stove: 3
most cooking at once: 3
100 orders took: 690ms
100 orders / 3 burners × 20 ms ≈ 700 ms. Bounded parallelism means doing lots of work with a fixed limit on how much runs at the same time.
Worker pool or semaphore?
| Worker pool | Semaphore | |
|---|---|---|
| goroutines | N, long-lived | one per task |
| limit | the number of workers | the channel's capacity |
| best for | a steady, possibly endless stream of jobs | a known list of tasks |
| results | a results channel | write to a slice by index, or a channel |
Next episode shows ready-made versions: errgroup with SetLimit, and semaphore.Weighted.
Mini project: the thali line
A generator of 20 orders that stops on ctx.Done(); stages for wash, cook (fanned out to 4 workers) and plate, all written with one stage helper; a merge; the original order kept with an index; at most 2 dishes in the tandoor (a semaphore); and the bonus: stop after 10 plates, cancel, and check that no goroutine leaked.
package main
import (
"context"
"fmt"
"runtime"
"sync"
"time"
)
type dish struct {
i int
name string
}
func orders(ctx context.Context, n int) <-chan dish { // generator that can stop
out := make(chan dish)
go func() {
defer close(out)
for i := 0; i < n; i++ {
select {
case out <- dish{i, fmt.Sprint("thali ", i+1)}:
case <-ctx.Done():
return
}
}
}()
return out
}
// stage builds a pipeline stage that applies f to every dish
func stage(ctx context.Context, in <-chan dish, f func(dish) dish) <-chan dish {
out := make(chan dish)
go func() {
defer close(out)
for d := range in {
select {
case out <- f(d):
case <-ctx.Done():
return
}
}
}()
return out
}
func merge(ctx context.Context, chans ...<-chan dish) <-chan dish {
out := make(chan dish)
var wg sync.WaitGroup
wg.Add(len(chans))
for _, c := range chans {
go func() {
defer wg.Done()
for d := range c {
select {
case out <- d:
case <-ctx.Done():
return
}
}
}()
}
go func() { wg.Wait(); close(out) }()
return out
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
tandoor := make(chan struct{}, 2) // at most 2 dishes in the tandoor
wash := func(d dish) dish { d.name = "washed " + d.name; return d }
cook := func(d dish) dish {
tandoor <- struct{}{}
defer func() { <-tandoor }()
time.Sleep(20 * time.Millisecond)
d.name += " cooked"
return d
}
plate := func(d dish) dish { d.name += " plated"; return d }
washed := stage(ctx, orders(ctx, 20), wash)
var cooks []<-chan dish
for w := 0; w < 4; w++ { // fan out the slow stage
cooks = append(cooks, stage(ctx, washed, cook))
}
plated := stage(ctx, merge(ctx, cooks...), plate)
served := make([]string, 20)
count := 0
for d := range plated {
served[d.i] = d.name // keep the original order
count++
if count == 10 { // the bonus: stop after 10
break
}
}
cancel()
time.Sleep(50 * time.Millisecond)
for i, s := range served[:10] {
if s != "" {
fmt.Println(i+1, s)
}
}
fmt.Println("served:", count, "goroutines left:", runtime.NumGoroutine())
}
Real output:
1 washed thali 1 cooked plated
2 washed thali 2 cooked plated
3 washed thali 3 cooked plated
4 washed thali 4 cooked plated
5 washed thali 5 cooked plated
6 washed thali 6 cooked plated
7 washed thali 7 cooked plated
8 washed thali 8 cooked plated
9 washed thali 9 cooked plated
10 washed thali 10 cooked plated
served: 10 goroutines left: 1
Every send in every stage selects on ctx.Done(), so after cancel() every goroutine in the pipeline returns and only main is left.
Common mistakes (and the real messages)
| Mistake | Real message / result | Why | Fix |
|---|---|---|---|
a generator that only does out <- v |
a goroutine blocked forever (goroutines: 2) |
the consumer stopped early | select with ctx.Done() |
| a stage that never closes its output | fatal error: all goroutines are asleep - deadlock! |
the next range never ends |
defer close(out) |
| every forwarder closes the merged channel | panic: send on closed channel / panic: close of closed channel |
other forwarders still send | close once, after wg.Wait() |
| expecting fan-out results in order | dish 4 \| dish 1 \| ... |
fast ones finish first | carry an index |
| one goroutine per task with no limit | most cooking at once: 100 |
unbounded | a worker pool or a semaphore |
sem := make(chan struct{}) |
fatal error: all goroutines are asleep - deadlock! on the first send |
capacity 0 | make(chan struct{}, N) |
closing results before the workers finish |
panic: send on closed channel |
workers still send | close after wg.Wait() |
The real runs behind three of these rows:
package main
import "fmt"
func double(in <-chan int) <-chan int {
out := make(chan int)
go func() {
// forgot: defer close(out)
for v := range in {
out <- v * 2
}
}()
return out
}
func main() {
in := make(chan int, 3)
in <- 1
in <- 2
in <- 3
close(in)
for v := range double(in) {
fmt.Println(v)
}
}
Real output:
2
4
6
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
main.go:22 +0xd4
exit status 2
package main
import "fmt"
func main() {
sem := make(chan struct{}) // no capacity!
sem <- struct{}{} // take a slot
fmt.Println("cooking")
<-sem
}
Real output:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
main.go:7 +0x2b
exit status 2
package main
import (
"fmt"
"sync"
)
func main() {
out := make(chan int)
var wg sync.WaitGroup
for _, c := range [][]int{{1, 2}, {3}} {
wg.Add(1)
go func() {
defer wg.Done()
defer close(out) // WRONG: each forwarder closes
for _, v := range c {
out <- v
}
}()
}
for v := range out {
fmt.Println(v)
}
wg.Wait()
}
Real output (the exact lines vary between runs):
3
panic: send on closed channel
panic: close of closed channel
goroutine 18 [running]:
main.main.func1()
main.go:17 +0x9a
created by main.main in goroutine 1
main.go:13 +0xed
exit status 2
Practice
- Fix the
doublestage above so the program prints 2, 4, 6 and exits. - Write a generator
squares(ctx, n)and a stageaddOne, and printaddOne(squares(ctx, 5)). - Change the worker pool to 2 workers. Does the served order change? Does the total time?
- In the semaphore program, change the capacity to 10. Predict "most cooking at once" and the time.
Solutions
1. Add defer close(out) as the first line of the goroutine: when in is closed, the range ends, out is closed, and main's range ends too.
2.
package main
import (
"context"
"fmt"
)
func squares(ctx context.Context, n int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for i := 1; i <= n; i++ {
select {
case out <- i * i:
case <-ctx.Done():
return
}
}
}()
return out
}
func addOne(ctx context.Context, in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for v := range in {
select {
case out <- v + 1:
case <-ctx.Done():
return
}
}
}()
return out
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for v := range addOne(ctx, squares(ctx, 5)) {
fmt.Print(v, " ")
}
fmt.Println()
}
Real output:
2 5 10 17 26
3. The served order stays the same (the index puts every plate back). The total time goes up, because two cooks share six dishes.
4. At most 10 at once, and about 100 / 10 × 20 ms ≈ 200 ms.
Quick quiz
1. Fifty goroutines share sem := make(chan struct{}, 4), and each does sem <- struct{}{} before work() and <-sem after. At most how many run work() at once? (From the video.)
A. 50 · B. 4 · C. 1 · D. 5
Answer
B. All fifty goroutines exist, but a send blocks once the 4 slots are taken.
2. In merge, who closes the output channel, and when?
Answer
One extra goroutine, after wg.Wait() returns, i.e. after every forwarder has finished sending.
3. Four goroutines receive from the same channel. Can two of them get the same value?
Answer
No. Each value sent on a channel is received by exactly one receiver.
4. What makes a generator leak?
Answer
The consumer stops receiving early, and the generator's send blocks forever because it doesn't also select on a done channel.
Interview questions
- What is a generator in Go? A function that returns a receive-only channel and produces values into it from its own goroutine, closing it at the end.
- What is a pipeline? A series of stages connected by channels; each stage receives from an inbound channel, works, and sends on an outbound channel.
- Explain fan-out and fan-in. Fan-out: several goroutines read from one channel to parallelise a slow stage. Fan-in: merge several channels into one, closed when all inputs are done.
- How do you avoid goroutine leaks in pipelines? Every send selects on
ctx.Done()(or a done channel), and the consumer cancels when it stops early. - How do you keep results in order with a worker pool? Tag each job with its index and write results into a slice at that index (or use per-job result channels).
- How do you limit concurrency? A fixed-size worker pool, or a semaphore (a buffered channel of capacity N); later,
errgroup.SetLimitorsemaphore.Weighted. - Who should close a channel with many senders? Only code that knows all senders are done, typically a goroutine that waits on a
WaitGroupand then closes. - Why use
struct{}for semaphore and signal channels? It carries no data and takes no memory; only the send or receive matters.
Glossary
| Term | Meaning | Example |
|---|---|---|
| Pattern | a reusable shape of code for a common problem | a worker pool |
| Stream | values that arrive one after another | tokens 1, 2, 3 ... |
| Generator | a function that returns a channel and fills it from its own goroutine | tickets(n) <-chan int |
| Producer | the code that sends values | the generator's goroutine |
| Consumer | the code that receives values | the range loop in main |
| Receive-only channel | a channel you can only receive from | <-chan int |
| Goroutine leak | a goroutine that never ends, holding memory | a generator blocked on a send |
| Done channel pattern | every blocking send or receive also selects on a stop signal | case <-ctx.Done(): return |
runtime.NumGoroutine |
the number of goroutines that currently exist | 2 → 1 |
| Stage | a goroutine that receives from an input channel, works, sends to an output channel | chop, cook |
| Source | the first stage, which only sends | orders |
| Sink | the last stage, which only receives | serving in main |
| Pipeline | stages connected by channels | cook(chop(orders(...))) |
| Downstream / upstream | the stages after / before this one | cook is downstream of chop |
| Bottleneck | the slowest stage, which limits the whole pipeline | 50 ms cooking |
| Fan-out | several goroutines receiving from one channel | four cook(in) |
| Fan-in / merge | combining several channels into one | merge(...) |
| Forwarder | a goroutine that copies values from one channel to another | inside merge |
| Worker | a goroutine that loops taking jobs and producing results | worker(id, jobs, results, &wg) |
| Worker pool | a fixed number of workers sharing a jobs channel | 3 cooks |
| Jobs channel | the channel workers take work from | chan job |
| Results channel | the channel workers send results to | chan result |
| Ordered results | results put back in input order, e.g. by index | plates[r.i] = r.plate |
| Parallelism | work truly running at the same time | 4 cooks at once |
| Unbounded parallelism | no limit on how much runs at once | one goroutine per task |
| Bounded parallelism | a fixed limit on how much runs at once | at most 3 |
| Semaphore | a counter of free slots: take before, give back after, wait if none | 3 burner keys |
| Buffered channel | a channel with capacity: sends don't block until it's full | make(chan struct{}, 3) |
struct{} |
the empty struct type; its values take no memory | sem <- struct{}{} |
max |
the built-in that returns the largest argument (Go 1.21) | max(most, active) |
Summary
- Generator: return
<-chan Tat once, send from a goroutine,closeat the end. - Done channel pattern: every send selects on
ctx.Done(), or an early stop leaks a goroutine. - Pipeline: stages
in <-chan→ work →out <-chan; each closes its output; closing flows downstream. - Fan-out: many goroutines on one channel for a slow stage. Fan-in:
merge, closing only afterwg.Wait(). - Worker pool: jobs + results channels; results carry an index to keep the order.
- Semaphore:
make(chan struct{}, N); send = take a slot, receive = give it back: bounded parallelism.
Next episode: concurrency patterns part 2: errgroup, errgroup.WithContext, SetLimit, semaphore.Weighted, singleflight, rate limiting with time.Ticker, token buckets with golang.org/x/time/rate, backpressure, pub/sub, and graceful shutdown with signal.NotifyContext.