0xBF
0xBF

0xBF

Go Cheatsheet - Worker pool pattern

0xBF's photo
0xBF

Published on Sep 9, 2021

1 min read

We can use channels to easily simulate a worker pool:

package main

import (
    "fmt"
    "time"
)

func main() {
    numWorkers := 5
    numJobs := 100

    // make sure these 2 channels have buffer size of numJobs
    jobChan := make(chan int, numJobs)
    completeChan := make(chan int, numJobs)

    fmt.Printf("Spawn %d workers\n", numWorkers)

    for i := 0; i < numWorkers; i++ {
        go worker(i, jobChan, completeChan)
    }

    fmt.Printf("Add %d jobs\n", numJobs)
    for i := 0; i < numJobs; i++ {
        jobChan <- i
    }
    close(jobChan)

    for i := 0; i < numJobs; i++ {
        id := <-completeChan
        fmt.Printf("Job %d is done\n", id)
    }
}

func worker(id int, jobChan <-chan int, completeChan chan<- int) {
    for job := range jobChan {
        fmt.Printf("Processing job %d\n", job)
        time.Sleep(time.Second)
        completeChan <- job
    }
}
 
Share this