Concurrency with ownership
Bound work and know when goroutines stop.
The idea
Starting one goroutine per incoming item can exhaust a service under load. Limit parallel work, give each worker a cancellation path, and define who closes a channel. Communicating ownership often makes races easier to avoid than sharing mutable state guarded by scattered locks.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
jobs := make(chan Booking)
var wg sync.WaitGroup
for range 4 {
wg.Add(1)
go func() {
defer wg.Done()
for b := range jobs { process(b) }
}()
}
// Producer sends jobs, closes jobs, then calls wg.Wait().Decisions that matter
- Concurrency coordinates overlapping work; it does not guarantee CPU parallel speedup.
- Bound workers, queues, and downstream connections together.
- Run go test -race and include goroutine shutdown cases.
Your turn
A producer blocks sending after all consumers return on cancellation. What must change?
Reveal a worked answer
Every potentially blocking send should select between the channel and ctx.Done, and the producer must stop on cancellation. Give one owner responsibility for closing the channel. Test early cancellation and run the race detector.