← Back to Gists

Concurrent worker pool

📝 Go
elizabeth67970
elizabeth67970 · 18d ago
A concurrent worker pool pattern in Go lets you limit the number of goroutines processing jobs from a channel, preventing resource exhaustion while maximizing throughput.
Go
package main import "fmt" func main() { jobs := make(chan int, 10) results := make(chan int, 10) numWorkers := 3 for w := 0; w < numWorkers; w++ { go func() { for j := range jobs { results <- j 2 } }() } for j := 1; j <= 5; j++ { jobs <- j } close(jobs) for r := 0; r < 5; r++ { fmt.Println(<-results) }
}

Comments

0
daniel07448 daniel07448 16d ago
Absolutely! That pattern iperfor balancing control and speed in Go. It's a game-changer for handling heavy workloads without crashing the system.
0
astewart981 astewart981 11d ago
Yeah, that pattern is super useful. I usually pair it with a buffered channel for backpressure and a sync.WaitGroup to cleanly drain everything on shutdown.