Search code examples
goconcurrencygoroutine

Limit number of goroutine picking task in loop


I am running a loop - 24 times which calls a downstream that is not able to handle all the work supplied concurrently by the go-routines spawned, I want to limit that such that only a specific number (3 or 4) of go-routine gets executed. Sample code looks like below, if anyone can point me to the right pattern to fulfil this would be a great help

for i:=0; i<24; i++ {
   go callSomeDownStream()
}

Solution

  • You can use the channel of empty structs to control the number of concurrent worker goroutines

    const MAX_CONCURRENT_JOBS = 3
    
    func main() {
        waitChan := make(chan struct{}, MAX_CONCURRENT_JOBS)
    
        for i:=0; i < 24; i++ {
            waitChan <- struct{}{}
            go func() {
                callSomeDownStream()
                <-waitChan
            }()
        }
    }