Skip to content

Latest commit

 

History

History

03-worker-pool

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Worker Pool

It implements the worker pool. I combine generator and fan-in pattern to achieve it. Besides, I add a lot of <-ctx.Done() to make sure all goroutine will be closed, except main goroutine.

Refer Worker Pool Example Code.

func main() {
wg := &sync.WaitGroup{} // for detecting worker all closed
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
jobs := genJobs(ctx)
results := make([]<-chan int, workerSize)
for i := 0; i < workerSize; i++ {
wg.Add(1)
results[i] = worker(ctx, i, jobs, wg)
}
flow := fanIn(ctx, results...)
for data := range flow {
log.Printf("Result is :%d", data)
}
wg.Wait()
fmt.Printf("expected 1 goroutine, got goroutine: %d\n", runtime.NumGoroutine())
}

You could call close() early by replacing L105 with time.AfterFunc(2*time.Second, cancel). You still see the got goroutine 1.