Senior
What is a worker pool in Go and when would you use it?
sobes.tech AI
Answer from AI
Worker pool in Go is a pattern for organizing parallel task processing with a limited number of goroutines (workers), which take tasks from a shared queue and process them.
Reasons to use:
- Control over the number of concurrently running goroutines to avoid creating too many and overwhelming the system.
- Increase performance through reuse of goroutines.
- Simplify task management and distribution.
Example of usage:
package main
import (
"fmt"
"sync"
)
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
// Process task
fmt.Printf("Worker %d processing job %d\n", id, j)
results <- j * 2
}
}
func main() {
jobs := make(chan int, 5)
results := make(chan int, 5)
var wg sync.WaitGroup
// Launch 3 workers
for w := 1; w <= 3; w++ {
wg.Add(1)
go worker(w, jobs, results, &wg)
}
// Send 5 tasks
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
wg.Wait()
close(results)
// Read results
for r := range results {
fmt.Println("Result:", r)
}
}
In this example, three workers process tasks from the jobs channel in parallel. This approach is effective for limited parallelism and resource management.