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.
go-concurrency-patterns/other/03-worker-pool/main.go
Lines 101 to 122 in fcce219
You could call close()
early by replacing L105 with time.AfterFunc(2*time.Second, cancel)
. You still see the got goroutine 1
.