Concurrency

Goroutines are cheap, and a program that starts them owns their lifetime. These lessons cover the scheduler, channels buffered and unbuffered, select, the sync package and its atomics, context for cancellation, and the patterns built on them: worker pools, pipelines, fan-in and fan-out, rate limits. The part ends on the races, deadlocks and leaks a run can hide.

063 Goroutines go f(), a goroutine that finishes after main, time.Sleep and the reason a program needs a wait Open 064 The scheduler, GOMAXPROCS and the runtime G, M and P, GOMAXPROCS, a blocked goroutine and what the runtime does next Open 065 Channels an unbuffered channel, send and receive, close, a range over a channel and the zero value of a channel Open 066 Buffered channels and closing make(chan T, n), len and cap, close, ranging until closed and the send on a closed channel panic Open 067 select, timeouts and default select over two channels, time.After, a default branch and a done channel Open 068 sync.Mutex and shared state Mutex, Lock and Unlock, RWMutex, a counter that stops losing updates and the -race report Open 069 sync.WaitGroup and structured waits Add, Done and Wait, a worker per item, errgroup in words and a wait that cannot be forgotten Open 070 sync.Once, sync.Pool and sync.Map Once for a lazy value, Pool for buffers, Map for a concurrent cache and when not to use each Open 071 sync/atomic and lock-free counters atomic.Int64, Add, Load, CompareAndSwap and the counter without a mutex Open 072 Context: cancellation and deadlines context.Background, WithCancel, WithTimeout, Done and the error a cancelled call returns Open 073 Context values and where they stop WithValue, a request id, the rule that a value is not a parameter and why keys are typed Open 074 Worker pools a jobs channel, a fixed number of workers, a results channel and a pool that closes cleanly Open 075 Pipelines a stage as a function, a channel between stages, cancellation and a pipeline that drains Open 076 Fan-out, fan-in and ordering fan-out to several workers, fan-in with a merge, and an order kept by an index Open 077 Rate limiting and backpressure time.Ticker as a limiter, a semaphore channel, a bounded queue and a client that slows down Open 078 Races, deadlocks and leaks a data race found by -race, a deadlock from two locks, a leaked goroutine and a stack dump Open

Lessons