A Worker Pool That Actually Shuts Down: Go Concurrency Patterns Revisited
Worker pools are one of those patterns that looks trivial in a blog post and becomes surprisingly difficult in production. The version I keep coming back to is built around three rules: a single owner, explicit cancellation, and a bounded queue. This post walks through the version I actually paste into projects. The naive version Most worker pool examples on the internet look like this: jobs := make(chan Job, 100) for i := 0; i < 8; i++ { go func() { for j := range jobs { process(j) } }() } This works for the happy path and falls apart everywhere else. There’s no way to wait for the workers to finish, no way to stop them early, and no way to know if process panicked. If the producer crashes, the workers stay alive forever. ...