go
46 lines · 7 steps
A graceful worker pool in Go
A worker pool that processes jobs from a channel and shuts every goroutine down cleanly with a broadcast signal.
Explained by
highlit
1package worker
2
3import (
4 "fmt"
5 "sync"
6 "time"
7)
8
9type Pool struct {
10 done chan struct{}
11 once sync.Once
12 wg sync.WaitGroup
13}
14
15func NewPool() *Pool {
16 return &Pool{done: make(chan struct{})}
17}
18
19func (p *Pool) Spawn(id int, jobs <-chan string) {
20 p.wg.Add(1)
21 go func() {
22 defer p.wg.Done()
23 for {
24 select {
25 case <-p.done:
26 fmt.Printf("worker %d: shutting down\n", id)
27 return
28 case job, ok := <-jobs:
29 if !ok {
30 return
31 }
32 select {
33 case <-p.done:
34 return
35 case <-time.After(50 * time.Millisecond):
36 fmt.Printf("worker %d: processed %s\n", id, job)
37 }
38 }
39 }
40 }()
41}
42
43func (p *Pool) Shutdown() {
44 p.once.Do(func() { close(p.done) })
45 p.wg.Wait()
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Closing a channel broadcasts to every receiver at once, which makes it the idiomatic Go shutdown signal.
- 2A sync.Once guards close so Shutdown is safe to call repeatedly without panicking on a double close.
- 3Pairing a WaitGroup with the done channel lets shutdown both signal and block until all workers actually exit.
Related explainers
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
rust
use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration;
Building a thread pool in Rust
concurrency
channels
thread-pool
Advanced
9 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
go
package model import ( "encoding/json"
Custom JSON marshaling in Go
json
serialization
interfaces
Intermediate
5 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-graceful-worker-pool-in-go-explained-go-da8b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.