Go Concurrency Patterns (Deep Dive)
Structured concurrency
- Every goroutine has an owner
- errgroup + context
- No fire‑and‑forget
Cancellation
- Check ctx.Done in loops
- select on send/recv
Backpressure
- Bounded queues
- Semaphores
- Load shedding
Worker pools
- Fixed workers
- Producer closes jobs
- Aggregator closes results
Fan‑out / Fan‑in
- Single writer aggregation
- Explicit ordering
Safe publication
- Channels / mutex / atomic.Value
- Happens‑before
Contention control
- Shard state
- Batch updates
- Immutable + swap
Timers
- Avoid time.After in loops
- Reuse timers
sync.Pool
- Not a cache
- Short‑lived buffers only
Testing
- -race
- stress + cancellation checks
1 / 1